简体   繁体   English

如何使用ArrayAdapter显示列表视图?

[英]How do I use ArrayAdapter to display a list view?

I want to display a list view in my application. 我想在我的应用程序中显示一个列表视图。 Each row should contain a text and an image. 每行应包含一个文本和一个图像。 I have used ArrayAdapter to display this but how to insert an image after the text. 我已经使用ArrayAdapter来显示它,但是如何在文本后插入图像。 Here is the code: 这是代码:

String lvr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list1 = (ListView) findViewById(R.id.cg_listview1);
list1.setAdapter(new ArrayAdapter<String>(this,R.layout.alerts , lvr));

Here alerts is my layout which contains only a textView. 这里的警报是我的布局,其中仅包含textView。

You would need to create a custom adapter to achieve this. 您将需要创建一个自定义适配器来实现此目的。

public class ResultAdapter extends BaseAdapter {

    private final Context context;
    private final List<Result> results;

    public ResultAdapter(Context context, List<Result> results) {
        this.context = context;
        this.results = results;
    }    

    @Override
    public int getCount() {
        return this.results.size();
    }

    @Override
    public Object getItem(int position) {
        return this.results.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    }
}

Create a Result class for storing the text and the image. 创建一个Result类,用于存储文本和图像。 In the getView method create your View either programmatically or by loading it from an XML layout using LayoutInflater . 在getView方法中,以编程方式或通过使用LayoutInflater从XML布局加载视图来创建视图。

If you want to have control over how the layout of items look in a list view, you should make your own custom implementation of ArrayAdapter : 如果要控制项目布局在列表视图中的外观,则应该对ArrayAdapter进行自定义实现:

A ListAdapter that manages a ListView backed by an array of arbitrary objects. 一个ListAdapter,用于管理由任意对象数组支持的ListView。 (...) To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want. (...)要在数组显示中使用TextViews以外的其他内容(例如ImageViews),或者要使toString()结果之外的一些数据填充视图,请重写getView(int,View,ViewGroup)以返回查看您想要的。

If you just google for custom arrayadapter example , you should find enough examples showing you how to implement this. 如果您只是谷歌搜索自定义arrayadapter示例 ,则应该找到足够的示例来展示如何实现此方法。 Two such examples are: 两个这样的示例是:

Good luck :) 祝好运 :)

Code complete with text view changes. 代码完成,并带有文本视图更改。 The ArrayList is generic but you must use ArrayList constuctors for the add, insert and remove to work. ArrayList是通用的,但是必须使用ArrayList构造函数才能进行添加,插入和删除。

public class ListViewLearn extends Activity {

    public Button btnAddData;
    public ListView listShow;
    public listRayAdapter adapter;
    public ArrayList<Integer> tmpStrRay;
    public Resources mainRes;

    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.mainlistlayout);

        tmpStrRay = new ArrayList<Integer>();
        tmpStrRay.add(1);
        tmpStrRay.add(2);
        tmpStrRay.add(3);
        mainRes= getApplicationContext().getResources();
        adapter = new listRayAdapter(getApplicationContext(),R.layout.listitem, tmpStrRay);

        btnAddData = (Button) findViewById(R.id.btnAddData);

        btnAddData.setOnClickListener(clickit);
        listShow= (ListView) findViewById(R.id.listShow);
        listShow.setAdapter(adapter);

        super.onCreate(savedInstanceState);

    }

    final OnClickListener clickit = new OnClickListener(){

        @Override
        public void onClick(View v) {
            for (int xx=4; xx<=50; xx++){ 
                adapter.add(new Integer(xx));
            }
        }};

    class listRayAdapter extends ArrayAdapter<Integer>
    {
        private ArrayList<Integer> items;
        Bitmap tmpImg;

        public listRayAdapter(Context context, int textVwId, ArrayList<Integer> txtRay)
        {
            super(context,textVwId,txtRay);
            this.items = txtRay;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View newv= convertView;
            if (newv == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                newv = vi.inflate(R.layout.listitem, null);
            }
            if ((position%2)==0){
                tmpImg = BitmapFactory.decodeResource(mainRes, R.drawable.even);
            }else{
                tmpImg = BitmapFactory.decodeResource(mainRes, R.drawable.odd);
            }

            ImageView img = (ImageView) newv.findViewById(R.id.img);
            img.setImageBitmap(tmpImg);
            TextView tt= (TextView) newv.findViewById(R.id.listedItem);
            Integer tmpInt = new Integer(items.get(position));
            tt.setText(tmpInt.toString());
            return newv;
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM