【发布时间】:2015-05-27 18:12:56
【问题描述】:
我正在尝试将解析查询中的TextView 传递给Adapter,并设置列表适配器。 UserAdapter 类扩展自 BaseAdapter。
我希望ListView 再显示一个String/int(使用另一个TextView)。
我该怎么做?
我的代码:
将UserAdapter 设置为ListView:
uList = new ArrayList<ParseUser>(li);
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new UserAdapter());
UserAdapterclass:
private class UserAdapter extends BaseAdapter {
@Override
public int getCount() {
return uList.size();
}
@Override
public ParseUser getItem(int arg0) {
return uList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
// How would I add another string when using `getView`?
@Override
public View getView(int pos, View v, ViewGroup arg2) {
if (v == null) {
v = getLayoutInflater().inflate(R.layout.chat_item, null);
}
// I need to add another 'TextView' here.
ParseUser c = getItem(pos);
TextView lbl = (TextView) v;
lbl.setText(c.getUsername());
lbl.setCompoundDrawablesWithIntrinsicBounds(
c.getBoolean("online") ? R.drawable.ic_online
: R.drawable.ic_offline, 0, R.drawable.arrow, 0);
return v;
}
}
下面是chat_item.xml。
这是.xml 和TextView 设置在getView
如何在此处添加另一个TextView?
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/arrow"
android:orientation="horizontal"
android:padding="@dimen/pad_10dp"
android:text="TextView"
android:textColor="@color/main_color_gray_dk"
android:textSize="@dimen/pad_30dp"
android:drawableLeft="@drawable/ic_online"
android:drawablePadding="@dimen/pad_10dp" />
【问题讨论】:
标签: android android-arrayadapter android-adapter