The ModMyTM Family of Sites.
  
Go Back   Android Forums | G-Phone Forums | ModMyGphone > ModMyGPhone Wiki
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Personal tools

Customising Views - ListView

From ModMyGPhone Wiki

You might have seen this mod many times already. Its the most common customisation. We'll be adding different views to the ListView. The custom View (like any other View) can either be written programmatically or inflated (activated) through XML. Since XML is recommended, we'll proceed that way.


Image:List.png



public class MyListAdapter extends BaseAdapter
    {
		
	protected Context mContext;
	protected LayoutInflater inflater;
	private List<MyObj> mItems;		
		
	public MyListAdapter(Context context)
	{
		mContext = context;
		mItems  = new ArrayList<MyObj>();
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
	}
  • An Adapter basically controls what goes into the list.
  • mItems is a list which stores contents of each row of the list.
  • LayoutInflator will inflate(activate or instantiate) the layout XML file into its corresponding View objects.



public View getView(int position, View convertView, ViewGroup parent)
{
			
	if(convertView == null)
	{
				
		convertView = inflater.inflate(R.layout.item, parent, false);
				
	}	
				
	((TextView)convertView.findViewById(R.id.text1)).setText(mItems.get(position).getText1());
	
        ((TextView)convertView.findViewById(R.id.text2)).setText(mItems.get(position).getText2());
        
        ((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(mItems.get(position).getImage());
						
	return convertView;
			
}
  • getView returns all Views added to a single row of the ListView.
  • If there are no Views in the list, inflate from the item XML file.
  • Add two TextViews and an ImageView. Content of these Views comes from a getter class MyObj described below.



public MyObj (String text1, String text2, Drawable image) 
{
	mImage = image;
	mText1 = text1;
	mText2 = text2;
}

public String getText1() 
{
	return mText1;
}
	
public String getText2() 
{
	return mText2;
}
	
public Drawable getImage() 
{
	return mImage;
}
  • Store the values received and passes on to MyListAdapter through the getter methods.



The above snippets are mostly like backend of customisation. Now we'll see how to use the above code in a ListActivity.


public class MyList extends ListActivity 
{

    MyListAdapter adp;
	
    
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
                
        setContentView(R.layout.main);
          
        adp = new MyListAdapter(MyList.this);
        
                
        MyObj bt = new MyObj ("Text1", "Text2", getResources().getDrawable(R.drawable.icon));
        
        adp.addItem(bt);
                
        setListAdapter(adp);
        
    }
  • Instantiate MyObj to pass data to the adapter and add adapter to ListActivity.



______________

Download Source

______________


All times are GMT -6. The time now is 10:55 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Powered by vbWiki Pro 1.3 RC5. Copyright ©2006-2007, NuHit, LLC
Copyright © 2007-08, ModMy LLC