istView通过自定义BaseAdapter传递多个的Arra

istView通过自定义BaseAdapter传递多个的Arra

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

问题描述

解决通过自定义适配器,它被传递多的ArrayList定义见下文填充ListView的神秘面纱。

主要活动:
我们声明的ArrayList

 私有静态的ArrayList<整数GT; img_challengeicon_values​​;
静态的 {
    img_challengeicon_values​​ =新的ArrayList<整数GT;();
    img_challengeicon_values​​.add(R.drawable.actionbar_hello);
    img_challengeicon_values​​.add(R.drawable.actionbar_world);
}
私有静态的ArrayList<整数GT; img_challengerpic_values​​;
静态的 {
    img_challengerpic_values​​ =新的ArrayList<整数GT;();
    img_challengerpic_values​​.add(R.drawable.actionbar_look);
    img_challengerpic_values​​.add(R.drawable.actionbar_down);
}

我们宣布适配器:

  arrayAdapter适配器=新arrayAdapter(这一点,
            img_challengeicon_values​​,
            img_challengerpic_values​​);

适配器活动:
我们扩展了BaseAdapter和放大器;&安培;设置变量:

 延伸BaseAdapter {
私人最终上下文的背景下;
私人的ArrayList<整数GT; img_challengeicon_values​​;
私人的ArrayList<整数GT; img_challengerpic_values​​;

我们称之为构造函数:

 公共arrayAdapter(上下文的背景下,
        ArrayList的<整数GT; img_challengeicon_values​​,
        ArrayList的<整数GT; img_challengerpic_values​​){
this.context =背景;
    this.img_challengeicon_values​​ = img_challengeicon_values​​;
    this.img_challengerpic_values​​ = img_challengerpic_values​​;
}

最后,我们呼吁getView,充气布局,从传递变量定义分配imageviews;像这样:

  imgChallengeIcon.setImageResource(img_challengeicon_values​​.get(位置));
imgChallengerPic.setImageResource(img_challengeicon_values​​.get(位置));


解决方案

  lstdata =(ListView控件)findViewById(R.id.inboxlist);
DB_listAdapter适配器=新DB_listAdapter(这一点,inboxdatalist);
lstdata.setAdapter(适配器);公共类DB_listAdapter延伸BaseAdapter {    私人活动活动;
    ArrayList的<对象> Object_Datas;
    私有静态LayoutInflater吹气= NULL;
    ViewHolder持有人;
    串strurl;
    公共DB_listAdapter(活动,INT标志,ArrayList的<对象> inboxdatalist {
        // TODO自动生成构造函数存根
        活性=一个;
        this.Object_Datas = inboxdatalist;        吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ImageLoader的=新FB_ImageLoader(activity.getApplicationContext());
    }
    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回this.Object_Datas.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    公共类ViewHolder {
        公共TextView的用户名;
        公共TextView的消息;
        公众形象ImageView的;
        公众的ImageButton imgaddbtn;
    }
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        视图V = convertView;        如果(V == NULL)
        {
            // LayoutInflater VI =(LayoutInflater)activity.getSystemService(myContext.LAYOUT_INFLATER_SERVICE);            V = inflater.inflate(R.layout.listitemfb,NULL);        }        TextView的文本=(TextView的)v.findViewById(R.id.username);
        TextView的文本2 =(TextView中)v.findViewById(R.id.message);
        ImageView的图像=(ImageView的)v.findViewById(R.id.avatar);        返回伏;    }}

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));
解决方案
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;

    }

}

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

09-02 19:10