问题
I have a webview but the getView function is never called. I have seen similar question but it the problems there where differenr. Can anyone help me?
Java code:
List<String[]> info=new ArrayList<String[]>();
String[] names_info=new String[5];
String[] phones_info=new String[5];
String[] map_info=new String[5];
String[] web_info=new String[5];
String[] photos_info=new String[5];
for(int i=0;i<names_info.length;i++)
{
info.add(new String[]{names_info[i],phones_info[i],map_info[i],web_info[i],photos_info[i]});
}
for (int i = 0; i < info.size(); i++) {
Log.d("TAG", "item " + i +
" name:" + info.get(i)[0] +
" info:" + info.get(i)[1]);
}
FacilitiesAdapter adapter = new FacilitiesAdapter(this,info,want_info_display,want_phone_display,want_photos_display);
//System.out.println(info[0]);
setListAdapter(adapter);
I see results in Log, so that means that my info is getting its result normally.
This is my adapter.
public class FacilitiesAdapter extends ArrayAdapter<String> {
private final Context context;
List <String[]> dataList;
public FacilitiesAdapter(Context context, List<String[]> dataList, int need_phone, int need_info, int need_photos) {
super(context, R.layout.expand_row);
this.context = context;
this.dataList = dataList;
//this.tracks_condition=tracks_condition;
//this.count=count;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.expand_row, parent, false);
System.out.println("I am here");
String[] data = dataList.get(position);
String name=data[0];
TextView textView = (TextView) rowView.findViewById(R.id.name);
System.out.println("I am in the adapter "+name);
textView.setText(name);
return rowView;
}
}
here I dont even see *System.out.println("I am here");*.
any help?
回答1:
change super(context, R.layout.expand_row);
with super(context, R.layout.expand_row, dataList);
or ovveride the getViewCount callback and let it return the number of item you want to show
回答2:
It looks like you are not using the data source anywhere inside the adapter .( A data source is any thing which provides data to be displayed , for ex an arraylist or a cursor.) If there is no data from a data source, getview() wont be called at all. In your case, change the call to super class constructor and pass "dataList". Then , it will be used automatically.
super(context, R.layout.expand_row, dataList);\
If you still want to see whats happening in the list, override the getCount() function.
public int getCount(){
return 1;
}
But once you start supplying the data, change the return type to the data size. Hope this helps .
回答3:
It turns out the problem with my getView() not being called is because it is not visible. May be in layout xml some other view is hiding the list view or you have changed the visibility to gone. If list view is not visible then its adapter's getView function will not be called.
Then Solution is : check the graphical view of the layout in you code to make sure the ListView is visible.
回答4:
If you extend ListActivity make sure your listview id is "@android:id/list"
来源:https://stackoverflow.com/questions/9774280/getview-function-never-called