How do I use ArrayAdapter to display a list view?
I wan开发者_JAVA技巧t to display a list view in my application. Each row should contain a text and an image. I have used ArrayAdapter to display this but how to insert an image after the text. Here is the code:
String lvr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list1 = (ListView) findViewById(R.id.cg_listview1);
list1.setAdapter(new ArrayAdapter<String>(this,R.layout.alerts , lvr));
Here alerts is my layout which contains only a textView.
If you want to have control over how the layout of items look in a list view, you should make your own custom implementation of ArrayAdapter
:
A ListAdapter that manages a ListView backed by an array of arbitrary objects. (...) To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.
If you just google for custom arrayadapter example, you should find enough examples showing you how to implement this. Two such examples are:
- http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
- http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
Good luck :)
You would need to create a custom adapter to achieve this.
public class ResultAdapter extends BaseAdapter {
private final Context context;
private final List<Result> results;
public ResultAdapter(Context context, List<Result> results) {
this.context = context;
this.results = results;
}
@Override
public int getCount() {
return this.results.size();
}
@Override
public Object getItem(int position) {
return this.results.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
}
}
Create a Result class for storing the text and the image. In the getView method create your View either programmatically or by loading it from an XML layout using LayoutInflater.
Code complete with text view changes. The ArrayList is generic but you must use ArrayList constuctors for the add, insert and remove to work.
public class ListViewLearn extends Activity {
public Button btnAddData;
public ListView listShow;
public listRayAdapter adapter;
public ArrayList<Integer> tmpStrRay;
public Resources mainRes;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.mainlistlayout);
tmpStrRay = new ArrayList<Integer>();
tmpStrRay.add(1);
tmpStrRay.add(2);
tmpStrRay.add(3);
mainRes= getApplicationContext().getResources();
adapter = new listRayAdapter(getApplicationContext(),R.layout.listitem, tmpStrRay);
btnAddData = (Button) findViewById(R.id.btnAddData);
btnAddData.setOnClickListener(clickit);
listShow= (ListView) findViewById(R.id.listShow);
listShow.setAdapter(adapter);
super.onCreate(savedInstanceState);
}
final OnClickListener clickit = new OnClickListener(){
@Override
public void onClick(View v) {
for (int xx=4; xx<=50; xx++){
adapter.add(new Integer(xx));
}
}};
class listRayAdapter extends ArrayAdapter<Integer>
{
private ArrayList<Integer> items;
Bitmap tmpImg;
public listRayAdapter(Context context, int textVwId, ArrayList<Integer> txtRay)
{
super(context,textVwId,txtRay);
this.items = txtRay;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View newv= convertView;
if (newv == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newv = vi.inflate(R.layout.listitem, null);
}
if ((position%2)==0){
tmpImg = BitmapFactory.decodeResource(mainRes, R.drawable.even);
}else{
tmpImg = BitmapFactory.decodeResource(mainRes, R.drawable.odd);
}
ImageView img = (ImageView) newv.findViewById(R.id.img);
img.setImageBitmap(tmpImg);
TextView tt= (TextView) newv.findViewById(R.id.listedItem);
Integer tmpInt = new Integer(items.get(position));
tt.setText(tmpInt.toString());
return newv;
}
}
}
精彩评论