2012-07-26 4 views
0

안드로이드 앱에서 이름과 조직의 TextView 목록을 만들려고합니다. 그러나 전체 목록은 예를 들어 동일합니다 :안드로이드에서 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 
... 

앱의 목록에 5 회 반복한다. 여기

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()가'각 목록 항목에 대해 한 번 호출하여 getView처럼 사용하고, 당신은 전체'items' 때마다 ArrayList와 당신을 반복한다. 'Joe'가 목록의 마지막 항목이라고 가정하므로 모든 목록 항목에 대한 그의 정보가 표시됩니다. 각 getView() 호출에서 텍스트보기의 텍스트를 한 번만 설정해야합니다. – theisenp

+0

'for 루프'만 제거하면됩니다. –

답변

1

는이

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

예, Android에서 getView 함수를 호출하는 방법을 잘못 이해 한 것처럼 보입니다. – kurtzbot

0

난 당신이 근본적인 문제가 무슨 말을하는지 생각 같은 내용으로 새로운 텍스트 뷰의 인스턴스를

TextView tt = new TextView(this); 
CharSequence text = ((TextView)v.findViewById(R.id.toptext)).getText(); 
tt.setText(text); 

이에

TextView tt = (TextView) v.findViewById(R.id.toptext); 

을 변경하려고 할 것입니다.

희망 하시겠습니까?

관련 문제