This is about understanding listviews.
There are a few component to a listview in Android
1. The view
2. The arrayadapter
3. The data
4. The listener
5. A customised look
Standard declaration:
//base listview
Listview lview = (ListView) findViewById(R.id.AlistViewInXML);
lview.setAdapter(aAdapt);
//data-binding
We can use objects in binding data - since we can define objects - we can define values individually. We can also use ArrayList to house multiple objects.
For example, a object with three values - obj.V1, obj.V2 and obj.V3 each representing different values.
//customised look
We can design a view to be house into a item in a ListView. For instance, we can have 1 single layout view housing 3 separate textviews - tv1, tv2, tv3.
//declare new arrayadapter for our listview
// this arrayadapter will house 1. The data and the listener. But we can't use the standard listview if we wish to customise the listview. Thus, we will need to overload this standard call:
ArrayAdapter
aAdapt = new ArrayAdapter (context, ViewToShowAsASingleItem, Data){
@Override
public View getView(int position, View cv, ViewGroup parent){
//arrange your ListView appearance
View rw = cv; //This is the view per item
buildAHolder hold;
if(rw == null){
//Set item appearance - when null, the item is recycled
LayoutInflater inflater=getLayoutInflater();
hold = new buildAHolder();
//with a holder - we can do referencing
hold.H_A = (TextView)((View) rw).findViewById(R.id.tv1);
hold.H_B = (TextView)((View) rw).findViewById(R.id.tv2);
hold.H_C = (TextView)((View) rw).findViewById(R.id.tv3);
//Use Tag to hold the holder class for retrieval subsequently
rw.setTag(hold);
hold.H_A.setTag(position);
} else {
// Get the ViewHolder back to get fast access to the TextViews
hold = (buildAHolder) rw.getTag();
hold.H_A.setTag(position);
}//End row recycling
//Set Data items to views
hold.H_A .setText(super.getItem(position).V1.toString());
hold.H_B .setText(super.getItem(position).V2.toString());
hold.H_C .setText(super.getItem(position).V3.toString());
cv.setOnClickListener(CustomclickListener); //you can choose where to put this
return cv;
} //End getView
OnClickListener CustomclickListener = new OnClickListener(){
public void onClick(View v) {
//Actions on click
}
}; //End onclicklistener
class buildAHolder{
//house views
TextView H_A; TextView H_B; TextView H_C;
}//End class
}; //End aAdapt
That's it about ListView - adapters
Notice also that we can do references with objects
1. Setup method to store and return View in an object. eg - setView and getView.
2. With setView, we can then set the preferred Layout into the object
3. Then use getView within the arrayadapter to extract the relevant view
Example:
View SelectedView;
if(a==true){SelectedView = (Layout) findViewById(R.Layout.Layout1;}
else{SelectedView = (Layout) findViewById(R.Layout.Layout2;}
obj.get(0).setView = SelectedView;
//In arrayadapater overloading
ArrayAdapter
aAdapt = new ArrayAdapter (context, 1, Data){
public View getView(int position, View cv, ViewGroup parent){
if (position==0){//only reference view once first position
cv = obj.get(0)getView; // Note how normal textviewresourceid is any value (1 here)
}
View row = cv;
.... rest of code
}
}
No comments:
Post a Comment