2012-02-18 4 views
0

에서 내가 가진 :의 setText() 텍스트 뷰의 list_row.xml

  1. MyListActivity class extends ListActivitysetContentView(R.layout.mylist);
  2. 두 xml 파일 : mylist.xmllist_row.xml.
  3. mylist.xml은 레이아웃이며 list_row.xml은 각 행의 모양입니다.
  4. list_row.xml은 3 TextViews(t1,t2,t3)을 포함한다.

클래스에서 t2의 일부 텍스트를 변경하고 싶습니다. 콘텐츠보기가 mylist.xml이기 때문에 단순히 findViewById을 사용할 수 없습니다. 그래서 LayoutInflater을 사용했습니다.

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.list_row, null); 

    textView2 = (TextView) v.findViewById(R.id.t2); 
    textView2.setText("ccccc"); 

문제는 t2의 텍스트가 절대로 변경되지 않는다는 것입니다. 나는 여러 번 시도했으며 텍스트는 내가 list_row.xml에서 설정 한 텍스트로 남아 있습니다. 나는 이유를 알 수 없다. 누군가 도와 줄 수 있었습니까. 감사!

===== 솔루션 : =====

내 자신의 SimpleCursorAdapter 클래스를 생성하고 getView() 메소드를 오버라이드 (override). 나는 그 라인이없는 경우, textView1은 항상 첫 번째 행의 값을 표시하기 때문에

private class MySimpleCursorAdapter extends SimpleCursorAdapter { 
    Cursor c; 
    public MySimpleCursorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     this.c = c; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     super.getView(position, convertView, parent); 
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.list_row, parent, false); 
     TextView textview1 = (TextView) rowView.findViewById(R.id.text1); 
     textView1.setText(c.getString(1));// whatever should be in textView1 
     textView2 = (TextView) rowView.findViewById(R.id.text2); 
     textView2.setText("ccccc"); 
     return rowView; 
    } 
} 

super.getView(position, convertView, parent);은 (textView1이 ID를 보여줍니다 경우 예 :.,이 항상 1) 매우 중요하다.

답변

1

TextView 텍스트를 변경하려면 ListView의 어댑터에서 텍스트를 변경해야합니다.

+0

안녕하세요. 답장을 보내 주셔서 감사합니다. 나는 SimpleCursorAdaper를 가지고 있는데, "지정된 XML 파일의 뷰를 Inflate하는 public view newView (컨텍스트 컨텍스트, 커서 커서, ViewGroup parent)"및 public abstract View getView (int position, View convertView, ViewGroup 부모)'XML 파일에서 부 풀릴 수 있습니다. 그러나 API에서는 팽창하는 법을 말하지 않습니다. 예제에 대한 링크를 제공 할 수 있습니까? 고마워요 – Dongminator

+0

또 다른 질문입니다. 루트보기가 목록보기이고 각 행이 목록보기의 하위 항목입니까? – Dongminator

+0

예, 문제가 해결되었습니다! 그냥'MySimpleCursorAdapter'를 추가하고'getView()'를 덮어 쓰고 거기에있는 텍스트를 변경하십시오. 'getView()'는 각 행에 대해 어댑터에 의해 호출된다는 것이 밝혀졌습니다. 감사! – Dongminator

0

텍스트 뷰를 변경하는 가장 좋은 방법은 getView()입니다. 더 많은 통찰력을 얻으려면 예제를 this을보십시오.