【问题标题】:Populate listview from arraylist of objects从对象的arraylist填充listview
【发布时间】:2013-03-08 15:46:20
【问题描述】:

我有一个列表活动,它将显示人员姓名和地址列表以及对象数组列表中的数据。这是到目前为止填充列表视图的方法..

private void fillData(ArrayList<Person> messages) {
    //populating list should go here
}

person 类存储一个人的姓名和地址。

public class Person {
   String name;
   String address;
}

虽然我的列表视图的列表项由两个文本视图组成,如下所示:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
android:clickable="false"
android:paddingBottom="9dp"
android:paddingTop="5dp" >

<TextView
    android:id="@+id/display_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/display_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/display_name"
    android:layout_below="@+id/display_name"
    android:textAppearance="?android:attr/textAppearanceSmall" />

我希望列表视图中的每个项目都显示每个人的姓名和地址。有人可以帮忙吗?

提前致谢,抱歉我的英语不好...

【问题讨论】:

  • 在谷歌自定义列表视图中搜索...
  • 您可以使用onCreate 方法。在这种情况下,它可以在程序中使用。

标签: android listview listactivity


【解决方案1】:

在你的活动中

AdapterPerson adbPerson;
ArrayList<Person> myListItems  = new ArrayList<Person>();

//then populate myListItems  

adbPerson= new AdapterPerson (youractivity.this, 0, myListItems);
listview.setAdapter(adbPerson);

适配器

public class AdapterPerson extends ArrayAdapter<Person> {
    private Activity activity;
    private ArrayList<Person> lPerson;
    private static LayoutInflater inflater = null;

    public AdapterPerson (Activity activity, int textViewResourceId,ArrayList<Person> _lPerson) {
        super(activity, textViewResourceId, _lProducts);
        try {
            this.activity = activity;
            this.lPerson = _lPerson;

            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        } catch (Exception e) {

        }
    }

    public int getCount() {
        return lPerson.size();
    }

    public Product getItem(Product position) {
        return position;
    }

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

    public static class ViewHolder {
        public TextView display_name;
        public TextView display_number;             

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        final ViewHolder holder;
        try {
            if (convertView == null) {
                vi = inflater.inflate(R.layout.yourlayout, null);
                holder = new ViewHolder();

                holder.display_name = (TextView) vi.findViewById(R.id.display_name);
                holder.display_number = (TextView) vi.findViewById(R.id.display_number);


                vi.setTag(holder);
            } else {
                holder = (ViewHolder) vi.getTag();
            }



            holder.display_name.setText(lProducts.get(position).name);
            holder.display_number.setText(lProducts.get(position).number);


        } catch (Exception e) {


        }
        return vi;
    }
}

【讨论】:

  • 对了,为什么AdapterPerson的第二个参数是0?
  • @djargonforce 0 只是一个占位符,因为永远不会使用 LayoutId(通过自定义 getView() 使其使用变得多余)
【解决方案2】:

这是我对自定义列表适配器问题的解决方案。 首先是一个自定义的arrayadapter:

public class MemoListAdapter extends ArrayAdapter<MemoEntry> {

private int layoutResourceId;

private static final String LOG_TAG = "MemoListAdapter";

public MemoListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    layoutResourceId = textViewResourceId;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    try {
        MemoEntry item = getItem(position);
        View v = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v = inflater.inflate(layoutResourceId, null);

        } else {
            v = convertView;
        }

        TextView header = (TextView) v.findViewById(R.id.list_memo_item_header);
        TextView description = (TextView) v.findViewById(R.id.list_memo_item_text);

        header.setText(item.getHeader());
        description.setText(item.getValue());

        return v;
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error", ex);
        return null;
    }
}

}

在活动的onCreate方法中:

adapter = new MemoListAdapter(this, R.layout.list_memo_item_layout); // the adapter is a member field in the activity
setContentView(R.layout.activity_view_memo_layout);
ListView lv = (ListView) findViewById(R.id.view_memo_memo_list);
lv.setAdapter(adapter);

然后我用这样的调用填充适配器:

ArrayList<MemoEntry> memoList = new ArrayList<MemoEntry>(); //here you should use a list with some content in it
adapter.addAll(memoList);

因此,要使其适应您的解决方案,请创建您自己的 customadapter,而不是我的对象 MemoEntry,使用您的 Person 类。 在 getView 方法中,根据您的需要进行更改。这和我正在做的事情差不多,所以应该不会太难。

希望对你有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多