簡體   English   中英

在自定義適配器中傳遞arrayList

[英]passing arrayList in Custom adapter

我在CustomAdapter中調用Arraylist,其中custom_row.xml包含所需的行。 我要讓arraylist的前兩個元素並排,然后在另一行中依次排列第三個和第四個元素,依此類推。 我編寫了這段代碼,只是為了打印Arraylist中的第一個元素。 如果我刪除注釋然后運行,則會出現與放置注釋相同的錯誤。 我不知道我要去哪里錯了。

也許應該是正確的方法

class CustomAdapter extends ArrayAdapter {

List<String> names;
LayoutInflater inflater;
Context context;
public CustomAdapter(Context context,  List<String> names) {
    super(context,R.layout.custom_row ,names);
   this.names=names;
    this.context=context;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {


   inflater=LayoutInflater.from(getContext());
    View customview=inflater.inflate(R.layout.custom_row,parent,false);
    String data=names.get(position);
   //String data1=names.get(position+1);
    TextView tv=(TextView)customview.findViewById(R.id.TeamA);
    tv.setText(data);
   //TextView tv1=(TextView)customview.findViewById(R.id.TeamB);
    //tv1.setText(data1);
    return customview;
}

我收到此錯誤-java.lang.NullPointerException:嘗試在com.example.CustomAdapter.getView上的空對象引用上調用虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)'

問題是因為tv為null,即因為findViewById(R.id.TeamA)返回null,所以..檢查TeamA存在於R.layout.custom_row

暫無
暫無

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

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