如何使用ListView显示图像列表?我在运行时下载图像.图像总数不固定.
我会从这样的事情开始(如果我的代码有问题,我当然感谢任何评论):
public class ItemsList extends ListActivity { private ItemsAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.items_list); this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems()); setListAdapter(this.adapter); } private class ItemsAdapter extends ArrayAdapter- { private Item[] items; public ItemsAdapter(Context context, int textViewResourceId, Item[] items) { super(context, textViewResourceId, items); this.items = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.items_list_item, null); } Item it = items[position]; if (it != null) { ImageView iv = (ImageView) v.findViewById(R.id.list_item_image); if (iv != null) { iv.setImageDrawable(it.getImage()); } } return v; } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { this.adapter.getItem(position).click(this.getApplicationContext()); } }
例如,使用自己的Items类型(保存有关图片的信息)和overriden getView()
方法扩展ArrayAdapter,为列表中的项目准备视图.add()
ArrayAdapter上还有一个方法可以将项添加到列表的末尾.
R.layout.items_list
布局简单 ListView
R.layout.items_list_item
是表示列表中的一个项目的布局
package studRecords.one; import java.util.List; import java.util.Vector; import android.app.Activity; import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.net.ParseException; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class studRecords extends ListActivity { static String listName = ""; static String listUsn = ""; static Integer images; private LayoutInflater layoutx; private VectorlistValue; RowData rd; static final String[] names = new String[] { "Name (Stud1)", "Name (Stud2)", "Name (Stud3)","Name (Stud4)" }; static final String[] usn = new String[] { "1PI08CS016","1PI08CS007","1PI08CS017","1PI08CS047" }; private Integer[] imgid = { R.drawable.stud1,R.drawable.stud2,R.drawable.stud3, R.drawable.stud4 }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainlist); layoutx = (LayoutInflater) getSystemService( Activity.LAYOUT_INFLATER_SERVICE); listValue = new Vector (); for(int i=0;i { public CustomAdapter(Context context, int resource, int textViewResourceId, List objects) { super(context, resource, textViewResourceId, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; TextView title = null; TextView detail = null; ImageView i11=null; RowData rowData= getItem(position); if(null == convertView) { convertView = layoutx.inflate(R.layout.list, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } holder = (ViewHolder) convertView.getTag(); i11=holder.getImage(); i11.setImageResource(imgid[rowData.mId]); title = holder.gettitle(); title.setText(rowData.mNames); detail = holder.getdetail(); detail.setText(rowData.mUsn); return convertView; } private class ViewHolder { private View mRow; private TextView title = null; private TextView detail = null; private ImageView i11=null; public ViewHolder(View row) { mRow = row; } public TextView gettitle() { if(null == title) { title = (TextView) mRow.findViewById(R.id.title); } return title; } public TextView getdetail() { if(null == detail) { detail = (TextView) mRow.findViewById(R.id.detail); } return detail; } public ImageView getImage() { if(null == i11) { i11 = (ImageView) mRow.findViewById(R.id.img); } return i11; } } } }
//mainlist.xml