I have a custom adapter and its “getView” is not being called.
Thats my main activity:
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);
}
}
thats my adapter:
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;
}}
and thats the Class that is creating the list:
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--;
}
Why is it that the getView is not being called?
Thanks!
You havent add the mainRelativeLayout to the contentview, of the activity. you should use, findViewById(), instead, applying an extra LayoutInflater. Using LayoutInflater.inflate() method will result an extra inflating of layout, and it will create a new view, who has not been added to the list view yet. so Change your code to following:
and ListAddr class to the below code