ListView 无法正确显示自定义数组适配器?
您好,我的 Android 应用程序有一个 listactivity,此活动从另一个具有 Intent 的活动调用,在调用 ListActivity 的 onCreate 方法后,创建一个新的 AsynchTask 并调用 Web 服务,并将所有内容放入我的 ArrayList 数据来自 Web 服务很好,但是当我尝试实现customArrayAdapter的getView,事情变得很奇怪,它有时显示列表的内容,有时不显示,有时当我滚动时。这是我的自定义适配器的getView方法。 (布局 = textview + listView)
class EntryDetailsAdapter extends ArrayAdapter<Entry>{
private ArrayList<Entry> entryDetails;
private Entry tempEntry;
public EntryDetailsAdapter(Context context, int textViewResourceId,
List<Entry> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
entryDetails = (ArrayList<Entry>) objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView = convertView;
if(mView == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.entry_rows, null);
}
tempEntry = entryDetails.get(position);
//data is fine but problem is while occur while showing data
if(tempEntry != null){
//get the textview from list row xml that i defined
TextView textView = (TextView) findViewById(R.id.entryRowTextView);
if(textView != null){
//i dont know why but this sometimes giving null so check
textView.setText(tempEntry.getEntryContent());
}
}
return mView;
}
}
[注意] 问题可能类似于 这个问题但我找不到解决方案
Hi my android app has a listactivity this activity called from another activity with Intent , after calling in onCreate method of the ListActivity create a new AsynchTask and call web service and put all stuff into my ArrayList data come from web service is fine , but when i try to implement getView of the customArrayAdapter things get wierd , it sometimes show content of the list sometimes not or sometimes when i scroll.Here is my getView method of custom adapter. (Layout = textview + listView)
class EntryDetailsAdapter extends ArrayAdapter<Entry>{
private ArrayList<Entry> entryDetails;
private Entry tempEntry;
public EntryDetailsAdapter(Context context, int textViewResourceId,
List<Entry> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
entryDetails = (ArrayList<Entry>) objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView = convertView;
if(mView == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.entry_rows, null);
}
tempEntry = entryDetails.get(position);
//data is fine but problem is while occur while showing data
if(tempEntry != null){
//get the textview from list row xml that i defined
TextView textView = (TextView) findViewById(R.id.entryRowTextView);
if(textView != null){
//i dont know why but this sometimes giving null so check
textView.setText(tempEntry.getEntryContent());
}
}
return mView;
}
}
[NOTE] Problem might similar to this question but i couldnt find a solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
在获取特定行的 TextView 字段时,您缺少 mView。
Try:
you were missing the mView when getting the particular row's TextView field.
既然您说列表中显示的数据是从 Web 服务汇集的,那么是否有可能数据尚未加载而您已经在尝试访问它?在设置列表适配器之前,您是否正在等待从服务加载数据?
Since you said that your data to be shown in the list is pooled from the web service, is it possible that data is not loaded yet and you are already trying to access it? Are you waiting for data to be loaded from service before setting list adapter?