我试图将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());
UserAdapter
class
:
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" />
?
SELECT id, fixed_position, intvalue (calculated in a subquery)
FROM content_items
ORDER BY intvalue DESC
答案 0 :(得分:0)
使用ViewGroup
作为TextView
的父视图。在您的情况下,LinearLayout
或RelativeLayout
可能效果最佳。
以下是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/arrow"
android:orientation="horizontal"
android:padding="@dimen/pad_10dp"
android:text="TextView"
android:id="@+id/textView1"
android:textColor="@color/main_color_gray_dk"
android:textSize="@dimen/pad_30dp"
android:drawableLeft="@drawable/ic_online"
android:drawablePadding="@dimen/pad_10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/pad_10dp"
android:text="TextView2"
android:id="@+id/textView2"
android:textColor="@color/main_color_gray_dk"
android:textSize="@dimen/pad_30dp"
android:drawableLeft="@drawable/ic_online"
android:drawablePadding="@dimen/pad_10dp" />
</LinearLayout>
答案 1 :(得分:0)
@ user370305陈述了XML部分。我将描述Java部分。在夸大该XML文件后,然后:
// Grab a reference to the first TextView
TextView lbl1 = (TextView) v.findViewById(R.id.textview1);
// Grab a reference to the second TextView
TextView lbl2 = (TextView) v.findViewById(R.id.textview2);
// Do stuff with 'lbl1' and 'lbl2'