2

I want to fill a ListView with a LinkedList<String> of my own elements.

My class looks like this:

public class Foo {
    public String text;
}

this is my adapter and the list:

String[] fromColumns = {"X"};
int[] toViews = {android.R.id.text1};
mAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_list_item_1, null,
        fromColumns, toViews, 0);
list = (ListView)findViewById(R.id.list);
list.setAdapter(mAdapter);

and I also have a LinkedList<Foo> elements; which I want to display in my list

Now the question: how to solve this?

  1. I dont know what to write into the fromColumns to match my text value
  2. I dont know how to bind my elements List to the list so it will be displayed
4

2 回答 2

1

创建一个自定义适配器。您可以扩展BaseAdapterArrayAdapter. ArrayAdapter是一个泛型类,所以它适用于这个例子。

一个可能的实现:

public class CustomAdapter extends ArrayAdapter<Item> {

    public CustomAdapter(Context context, List<Item> items) {
        super(context, 0, items);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.some_layout, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(getItem(position).getText());
        return convertView;
    }

    private class ViewHolder {
        TextView textView;
    }
}

现在像这样设置适配器:

CustomAdapter customAdapter = new CustomAdapter(context, getList());
listView.setAdapter(customAdapter);
于 2013-05-28T10:11:06.483 回答
0

您需要为此创建自定义适配器..

喜欢:

public class AdapterCategoryList extends BaseAdapter {
    private Context context;
    private ArrayList<SetterCategoryNames> categoryNames;
    public AdapterCategoryList( Context context, ArrayList<SetterCategoryNames> categoryNames ){
        this.context    =   context;
        this.categoryNames = categoryNames;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if ( convertView == null ) {
            convertView = View.inflate(context, R.layout.listitem_category_list, null);
            holder = new Holder();
            holder.mTxtCategoryImage = (TextView) convertView.findViewById(R.id.txtCategoryListItem);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }        
        holder.mTxtCategoryImage.setBackgroundResource( categoryNames.get(position).getCategoryImage() );        
        return convertView;
    }

    class Holder {
        TextView mTxtCategoryImage;
    }

}
于 2013-05-28T10:08:06.527 回答