我正在尝试在名称和组织的Android应用中创建TextView列表。但是,整个列表是相同的,例如:
Joe's work for Construction Co.
15hrs of 60hrs
Joe's work for Construction Co.
15hrs of 60hrs
Joe's work for Construction Co.
15hrs of 60hrs
...
在应用列表中重复五次。以下是相关代码:
class IconicAdapter extends ArrayAdapter<Long> {
private ArrayList<Long> items;
private FakeIDO ido;
public IconicAdapter(Context context,int textViewResourceId, ArrayList<Long> ids, FakeIDO ido){
super(WorkList.this, R.layout.feed_list, ids);
this.items = ids;
this.ido = ido;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
for(long id : items){
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText(ido.getName(id) + "'s work for " + ido.getOrganization(id));
}
if(bt != null){
bt.setText( ido.totalWorked(id) + "hrs of " + ido.estimatedHours(id) + "hrs");
}
}
return v;
}
以下是此类正在处理的xml视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
现在,我明白当我调用“TextView tt =(TextView)v.findViewById(R.id.toptext);”时会返回相同的实例,但是我不知道要改变什么来获得一个新的每次都是实例对象。我错过了什么?
答案 0 :(得分:1)
像这样使用你的getView
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
// get the 'id' for position.
int id = items.get(position);
if (tt != null) {
tt.setText(ido.getName(id) + "'s work for " + ido.getOrganization(id));
}
if(bt != null){
bt.setText( ido.totalWorked(id) + "hrs of " + ido.estimatedHours(id) + "hrs");
}
return v;
}
答案 1 :(得分:0)
我会尝试更改
TextView tt = (TextView) v.findViewById(R.id.toptext);
到
TextView tt = new TextView(this);
CharSequence text = ((TextView)v.findViewById(R.id.toptext)).getText();
tt.setText(text);
这将实例化一个具有相同内容的新TextView,我认为这就是您所说的潜在问题。
希望有所帮助!