通过自定义BaseAdapter传递多个ArrayLists来填充Listview

时间:2012-06-12 11:36:36

标签: android listview arraylist baseadapter

解决通过自定义适配器填充列表视图的谜团,该适配器正在传递下面定义的多个arraylists。

主要活动: 我们宣布了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);
}

我们宣布我们的适配器:

arrayAdapter adapter = new arrayAdapter(this, 
            img_challengeicon_values,
            img_challengerpic_values);

适应人活动: 我们扩展BaseAdapter&amp;&amp;设置变量:

extends BaseAdapter {
private final Context context; 
private ArrayList<Integer> img_challengeicon_values;
private ArrayList<Integer> img_challengerpic_values;

我们称之为构造函数:

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;
}

最后我们调用getView,膨胀布局,并根据传递的变量分配imageview;像这样:

imgChallengeIcon.setImageResource(img_challengeicon_values.get(position));
imgChallengerPic.setImageResource(img_challengeicon_values.get(position));

1 个答案:

答案 0 :(得分:0)

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;

    }

}