基本适配器中的Android空对象引用
我正在使用一个基本适配器,并用不同数组列表的相应元素填充每个列表项的各个组件。我正在使用以下代码:基本适配器中的Android空对象引用,android,arraylist,baseadapter,Android,Arraylist,Baseadapter,我正在使用一个基本适配器,并用不同数组列表的相应元素填充每个列表项的各个组件。我正在使用以下代码: public class OfflineAdapter extends BaseAdapter { static Context context; static int layoutResourceId; ArrayList<String> qid = new ArrayList<String>(); ArrayList<String> author = ne
public class OfflineAdapter extends BaseAdapter {
static Context context;
static int layoutResourceId;
ArrayList<String> qid = new ArrayList<String>();
ArrayList<String> author = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> vote = new ArrayList<String>();
public OfflineAdapter(Context c,int layoutResourceId,ArrayList<String> qid,ArrayList<String> author,ArrayList<String> title,ArrayList<String> vote)
{
//super(c,layoutResourceId,qid,author,title,vote);
this.context = c;
this.layoutResourceId = layoutResourceId;
this.qid = qid;
this.author = author;
this.title = title;
this.vote = vote;
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return qid.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
QuestionHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new QuestionHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.questionTitle);
holder.txtTitle2 = (TextView)row.findViewById(R.id.questionAuthor);
holder.txtTitle3 = (TextView)row.findViewById(R.id.questionVotes);
holder.txtTitle4 = (TextView)row.findViewById(R.id.questionID);
row.setTag(holder);
}
else
{
holder = (QuestionHolder)row.getTag();
}
//Question hold = data[position];
//if(hold!=null) {
holder.txtTitle.setText(Html.fromHtml(title.get(position)));
holder.txtTitle2.setText(author.get(position));
holder.txtTitle3.setText(vote.get(position));
holder.txtTitle4.setText(qid.get(position));
//}
return row;
}
static class QuestionHolder
{
TextView txtTitle;
TextView txtTitle2;
TextView txtTitle3;
TextView txtTitle4;
}
原因可能是什么?
我已经初始化了所有数组列表,没有一个是空的
谢谢 首先,为了获得干净的代码和更好的编程实践,而不是像您那样创建4个字符串列表:
ArrayList<String> qid = new ArrayList<String>();
ArrayList<String> author = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> vote = new ArrayList<String>();
并将该对象的单个列表传递给适配器
ArrayList<YourObject> objList = new ArrayList<YourObject>();
ArrayList objList=new ArrayList();
然后在
getCount()
方法返回objList.size()
首先,为了获得干净的代码和更好的编程实践,而不是像您那样创建4个字符串列表:
ArrayList<String> qid = new ArrayList<String>();
ArrayList<String> author = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> vote = new ArrayList<String>();
并将该对象的单个列表传递给适配器
ArrayList<YourObject> objList = new ArrayList<YourObject>();
ArrayList objList=new ArrayList();
然后在
getCount()
方法返回objList.size()
首先,为了获得干净的代码和更好的编程实践,而不是像您那样创建4个字符串列表:
ArrayList<String> qid = new ArrayList<String>();
ArrayList<String> author = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> vote = new ArrayList<String>();
并将该对象的单个列表传递给适配器
ArrayList<YourObject> objList = new ArrayList<YourObject>();
ArrayList objList=new ArrayList();
然后在
getCount()
方法返回objList.size()
首先,为了获得干净的代码和更好的编程实践,而不是像您那样创建4个字符串列表:
ArrayList<String> qid = new ArrayList<String>();
ArrayList<String> author = new ArrayList<String>();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> vote = new ArrayList<String>();
并将该对象的单个列表传递给适配器
ArrayList<YourObject> objList = new ArrayList<YourObject>();
ArrayList objList=new ArrayList();
然后在
getCount()
method returnobjList.size()
上,您已经初始化了所有ArrayList,但是如果不使用它们,则使用构造函数中传递的任何内容。标题列表很可能为null,因为您在构造函数中传递了null。检查如何调用适配器的构造函数以及传递了哪些引用。您已经初始化了所有ArrayList,但不使用它们,而是使用构造函数中传递的任何内容。标题列表很可能为null,因为您在构造函数中传递了null。检查如何调用适配器的构造函数以及传递了哪些引用。您已经初始化了所有ArrayList,但不使用它们,而是使用构造函数中传递的任何内容。标题列表很可能为null,因为您在构造函数中传递了null。检查如何调用适配器的构造函数以及传递了哪些引用。您已经初始化了所有ArrayList,但不使用它们,而是使用构造函数中传递的任何内容。标题列表很可能为null,因为您在构造函数中传递了null。检查如何调用适配器的构造函数以及传递了哪些引用。您应该使用qid.size()作为getCount()方法的返回。如何调用此适配器?您能发布该代码并查看ArrayList标题是否有价值吗?因为您的异常说明您的标题列表
没有任何值。Offtopic:您可能在这里没有任何问题,但每当我看到“静态上下文”时,我就知道它会带来麻烦。请确保在创建适配器时不传递空的标题
对象。为了获得最佳实践,最好使用单个对象而不是4个列表(如“hrskrs”答案)来更改代码。您应该使用qid.size()作为getCount()方法的返回。如何调用此适配器?您能发布该代码并查看ArrayList标题是否有价值吗?因为您的异常说明您的标题列表
没有任何值。Offtopic:您可能在这里没有任何问题,但每当我看到“静态上下文”时,我就知道它会带来麻烦。请确保在创建适配器时不传递空的标题
对象。为了获得最佳实践,最好使用单个对象而不是4个列表(如“hrskrs”答案)来更改代码。您应该使用qid.size()作为getCount()方法的返回。如何调用此适配器?您能发布该代码并查看ArrayList标题是否有价值吗?因为您的异常说明您的标题列表
没有任何值。Offtopic:您可能在这里没有任何问题,但每当我看到“静态上下文”时,我就知道它会带来麻烦。请确保在创建适配器时不传递空的标题
对象。为了获得最佳实践,最好使用单个对象而不是4个列表(如“hrskrs”答案)来更改代码。您应该使用qid.size()作为getCount()方法的返回。如何调用此适配器?您能发布该代码并查看ArrayList标题是否有价值吗?因为您的异常说明您的标题列表
没有任何值。Offtopic:您可能在这里没有任何问题,但每当我看到“静态上下文”时,我就知道它会带来麻烦。请确保在创建适配器时不传递空的标题
对象。为了获得最佳实践,最好使用单个对象而不是4个列表(如“hrskrs”答案)来更改代码。这就是我在getCount()中返回20的原因。这是正确的方法吗?然后创建一个tmpList
,过滤20个项目并将它们添加到该列表中。然后发送到适配器
。您必须首先阅读适配器的工作原理,但将其设置为静态不是一个好的做法。我只想让列表只包含前20个元素。这就是我在getCount()中返回20的原因。这是正确的方法吗?然后创建一个tmpList
,过滤20个项目并将它们添加到该列表中。然后发送到适配器
。您必须首先阅读适配器如何工作,它将如何工作,但我