简体   繁体   English

在自定义适配器中传递arrayList

[英]passing arrayList in Custom adapter

I am calling an Arraylist in a CustomAdapter, with custom_row.xml which contains the desired row. 我在CustomAdapter中调用Arraylist,其中custom_row.xml包含所需的行。 I want first two elements of the arraylist side by side in a row, and then third and fourth element in the other row and so on. 我要让arraylist的前两个元素并排,然后在另一行中依次排列第三个和第四个元素,依此类推。 I wrote this code, just to print the first element from the Arraylist. 我编写了这段代码,只是为了打印Arraylist中的第一个元素。 if I remove the comments and then run, I get the same error as when putting comments. 如果我删除注释然后运行,则会出现与放置注释相同的错误。 I don't know where I am going wrong. 我不知道我要去哪里错了。

or maybe what should be the right way. 也许应该是正确的方法

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

I am getting this error- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.CustomAdapter.getView 我收到此错误-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