I have found these 2 answers: https://stackoverflow.com/a/11787066/1489990 and https://stackoverflow.com/a/18868395/1489990 that I have been trying to follow to accomplish a listview with different layouts for each item. My array adapter looks like this:
public class MyArrayAdapter<T> extends ArrayAdapter<T> {
LayoutInflater mInflater;
int[] mLayoutResourceIds;
public MyArrayAdapter(Context context, int[] textViewResourceId, List<T> objects) {
super(context, textViewResourceId[0], objects);
mInflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
mLayoutResourceIds = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
// instead of if else you can use a case
if (row == null) {
if (type == TYPE_ITEM1) {
//infalte layout of type1
row = inflater.inflate(R.id.item1, parent, false); //*****************
}
if (type == TYPE_ITEM2) {
//infalte layout of type2
} else {
//infalte layout of normaltype
}
}
return super.getView(position, convertView, parent);
}
@Override
public int getItemViewType(int position) {
if (position== 0){
type = TYPE_ITEM1;
} else if (position == 1){
type = TYPE_ITEM2;
}
else
{
type= TYPE_ITEM3 ;
}
return type; //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public int getViewTypeCount() {
return 3; //To change body of overridden methods use File | Settings | File Templates.
}
}
and my onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = new String[] {"one", "two", "three", "four", "five", "six"};
List<String> list = new ArrayList<String>();
Collections.addAll(list, array);
ListView listView = new ListView(this);
listView.setAdapter(new MyArrayAdapter<String>(this, new int[] {android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_single_choice}, list));
setContentView(listView);
}
This issue is that in my array adapter the line:
row = inflater.inflate(R.id.item1, parent, false);
gives the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference
And I am a bit confused as to the structure for the xml layouts. Right now I have 1 xml for the listView and a seperate one for the first list layout, and I tried making it all in 1 xml layout but it still gave the null pointer exception. I need some guidance as to how to accomplish this. Thanks