简体   繁体   English

如何将ArrayAdapter设置为Listview - Android

[英]How to set ArrayAdapter to Listview - Android

I have a class that extends ArrayAdapter . 我有一个扩展ArrayAdapter的类。 I can not set the ListAdapter to my ListView because I don't know what the code should be when I try to create the ListAdapter. 我无法将ListAdapter设置为ListView,因为当我尝试创建ListAdapter时,我不知道代码应该是什么。 Here is my current code. 这是我目前的代码。

class Item {
                String username;
                String number;
                String content;

            }


             class ListAdapter extends ArrayAdapter<Item> {

                public ListAdapter(Context context, int textViewResourceId) {
                    super(context, textViewResourceId);
                    // TODO Auto-generated constructor stub
                }

                private List<Item> items;

                public ListAdapter(Context context, int resource, List<Item> items) {

                    super(context, resource, items);

                    this.items = items;

                }

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

                    View v = convertView;

                    if (v == null) {

                        LayoutInflater vi;
                        vi = LayoutInflater.from(getContext());
                        v = vi.inflate(R.layout.list_item, null);

                    }

                    Item p = items.get(position);

                    if (p != null) {

                        TextView tt = (TextView) v.findViewById(R.id.id);
                        TextView tt1 = (TextView) v.findViewById(R.id.categoryId);
                        TextView tt3 = (TextView) v.findViewById(R.id.description);

                        if (tt != null) {
                            tt.setText(p.getId());
                        }
                        if (tt1 != null) {

                            tt1.setText(p.getCategory().getId());
                        }
                        if (tt3 != null) {

                            tt3.setText(p.getDescription());
                        }
                    }

                    return v;

                }
             }

Here is where I get the problem, trying to declare the ListAdapter 这是我遇到问题的地方,试图声明ListAdapter

List <Item> tempList = new ArrayList <Item>();

                ListView yourListView = (ListView) findViewById(android.R.id.list);

                // Here is the problem
                ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, List<tempList> //the error occurs right here);

                yourListView .setAdapter(customAdapter);

File : res/layout/list_fruit.xml 文件:res / layout / list_fruit.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp" >
</TextView>

ListFruitActivity Class ListFruitActivity类

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListFruitActivity extends ListActivity {

    static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
            "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
            "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // no more this
        // setContentView(R.layout.list_fruit);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS));

        ListView listView = getListView();
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}

Sample Output: 样本输出:

样本输出

Change 更改

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item,List<tempList>)

to

ListAdapter customAdapter = new ListAdapter(this, R.layout.list_item, tempList); 
//...........

REmove list just add tempList 删除列表只需添加tempList

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

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