通过自定义 BaseAdapter 传递多个 ArrayList 来填充 Listview [英] Populating Listview By Passing Multiple ArrayLists Through Custom BaseAdapter

查看:32
本文介绍了通过自定义 BaseAdapter 传递多个 ArrayList 来填充 Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决通过自定义适配器填充列表视图的奥秘,该适配器将传递如下定义的多个数组列表.

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

适配器活动:我们扩展 BaseAdapter &&设置变量:

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;

我们调用构造函数:

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,膨胀布局,并根据传递的变量定义分配图像视图;像这样:

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

推荐答案

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;

    }

}

这篇关于通过自定义 BaseAdapter 传递多个 ArrayList 来填充 Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆