簡體   English   中英

如何在listView的自定義行視圖中設置textView

[英]How to set textView in custom row view of listView

我試圖在listView的自定義行視圖中設置textView,如下所示,但應用程序崩潰並返回空指針異常

TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");

但是當我將其放在setOnClickListener下時,它可以工作

button.setOnClickListener(new View.OnClickListener() {      
        @Override
        public void onClick(View v) {
            TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
            txtrow.setText("text");
        }
    });

您應該在ListView的適配器中執行此操作。 無論適配器背面使用的是什么數據,都應包含所需的文本,以便在getView()實際創建行視圖時可以在適當的TextView上進行設置。

重要的是要注意, getChildAt(index) 從ListView的當前children返回給定索引處的孩子 換句話說, getChildAt(0) 不一定給你列表項目位置0的用戶可能已經滾動該列表,以便它現在示出了通過從15位的10個數據(例如),在這種情況下getChildAt(0)為您提供位置10而非位置0的視圖。

To set text in custom row in listview 
Please follow the following points 
1. custom arrayadapter by extending Arrayadpater (for simple)
2. and overwrite getview as following

 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    textView.setText(values[position]);
    return rowView;
  }

3. and set the adapter to the listview 
And for more detial please refer this site
[http://www.vogella.com/tutorials/AndroidListView/article.html][1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM