我有一个自定义适配器,它的“getView”没有被调用。那是我的主要活动:
public class MainActivity extends Activity {
RelativeLayout mainRelativeLayout;
ListView listViewOne;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values1 = new String[] { " ", "Android", "iPhone", "WindowsMobile" };
ListArr listArr = new ListArr(this, 3);
listArr.AddListToListArr(values1);
}
}
那是我的适配器:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private TextView textView;
private ImageView icon;
public MyAdapter(Context context, String[] values) {
super(context, R.layout.first_row, values);
this.context = context;
this.values = values;
}
@Override
public int getCount() {
return values.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
rowView = inflater.inflate(R.layout.first_row, parent, false);
textView = (TextView) rowView.findViewById(R.id.row_text_view);
icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
textView.setText(values[position]);
return rowView;
}}
这就是创建列表的类:
public class ListArr {
int indexOfEmptyCellInArr;
ListView[] listArr;
Context context;
LinearLayout mainRelativeLayout;
public ListArr(Context context, int numOFLists) {
this.listArr = new ListView[numOFLists];
this.indexOfEmptyCellInArr = 0;
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null);
this.mainRelativeLayout = (LinearLayout) v.findViewById(R.id.main_relative_layout);
}
public void AddListToListArr(String[] stringsOfNewList) {
ArrayAdapter<String> arrayAdapter = new MyAdapter(context, stringsOfNewList);
ListView listview = new ListView(context);
listview.setAdapter(arrayAdapter);
listArr[indexOfEmptyCellInArr] = listview;
mainRelativeLayout.addView(listArr[indexOfEmptyCellInArr]);
indexOfEmptyCellInArr++;
}
public void RemoveLastList(){
int indexOfListToRemove = indexOfEmptyCellInArr - 1;
listArr[indexOfListToRemove] = null;
indexOfEmptyCellInArr--;
}
为什么 getView 没有被调用?谢谢!