2012-07-26 138 views
0

我想在我的名称和组织的Android应用程序中创建TextView的列表。但是,整个列表是相同的,例如:在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

'getView()'为每个单独的列表项调用一次,而你是通过你的整个'items' ArrayList的每一次迭代。我假设'乔'是列表中的最后一项,所以他的信息显示在每个列表项上。您应该只在每个'getView()'调用中设置文本视图的文本。 – theisenp 2012-07-26 03:59:50

+0

只是删除你的'for循环'。 – 2012-07-26 04:10:20

回答

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; 
} 
+0

是啊,看起来我误解了getView函数是如何被Android调用的。 – kurtzbot 2012-07-26 15:48:32

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具有相同的内容,我认为这是你在说什么根本的问题是。

希望有帮助!