问题
Solve the mystery of populating a listview through a custom adapter, which is being passed multiple arraylists as defined below.
MAIN ACTIVITY: we declare the arraylists:
private static ArrayList<Integer> img_challengeicon_values;
static {
img_challengeicon_values = new ArrayList<Integer>();
img_challengeicon_values.add(R.drawable.actionbar_hello);
img_challengeicon_values.add(R.drawable.actionbar_world);
}
private static ArrayList<Integer> img_challengerpic_values;
static {
img_challengerpic_values = new ArrayList<Integer>();
img_challengerpic_values.add(R.drawable.actionbar_look);
img_challengerpic_values.add(R.drawable.actionbar_down);
}
we declare our adapter:
arrayAdapter adapter = new arrayAdapter(this,
img_challengeicon_values,
img_challengerpic_values);
ADAPTER ACTIVITY: we extend BaseAdapter && set variables:
extends BaseAdapter {
private final Context context;
private ArrayList<Integer> img_challengeicon_values;
private ArrayList<Integer> img_challengerpic_values;
we call constructor:
public arrayAdapter(Context context,
ArrayList<Integer> img_challengeicon_values,
ArrayList<Integer> img_challengerpic_values) {
this.context = context;
this.img_challengeicon_values = img_challengeicon_values;
this.img_challengerpic_values = img_challengerpic_values;
}
lastly we call getView, inflate layout, and assign imageviews as defined from passed variables; like so:
imgChallengeIcon.setImageResource(img_challengeicon_values.get(position));
imgChallengerPic.setImageResource(img_challengeicon_values.get(position));
回答1:
lstdata = (ListView) findViewById(R.id.inboxlist);
DB_listAdapter adapter = new DB_listAdapter (this,inboxdatalist);
lstdata.setAdapter(adapter);
public class DB_listAdapter extends BaseAdapter {
private Activity activity;
ArrayList<Object> Object_Datas;
private static LayoutInflater inflater=null;
ViewHolder holder;
String strurl;
public DB_listAdapter (Activity a,int flag, ArrayList<Object> inboxdatalist{
// TODO Auto-generated constructor stub
activity=a;
this.Object_Datas=inboxdatalist;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new FB_ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.Object_Datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class ViewHolder{
public TextView username;
public TextView message;
public ImageView image;
public ImageButton imgaddbtn;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v=convertView;
if(v==null)
{
//LayoutInflater vi = (LayoutInflater)activity.getSystemService(myContext.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listitemfb, null);
}
TextView text=(TextView)v.findViewById(R.id.username);
TextView text2=(TextView)v.findViewById(R.id.message);
ImageView image=(ImageView)v.findViewById(R.id.avatar);
return v;
}
}
来源:https://stackoverflow.com/questions/10995840/populating-listview-by-passing-multiple-arraylists-through-custom-baseadapter