I have an arrayoutofboundsexception error in my 2 column listview,
please check my code below:
private static ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.meal_result);
BreakFastLog info = new BreakFastLog(this);
info.open();
String data1 = info.getMealNameData();
String data2 = info.getServingData();
String[] inputData = data1.split( " " );
String[] inputData2 = data2.split( " " );
lv = (ListView) findViewById (R.id.lvBF);
/*
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();
int[] columnIds = new int[] {
R.id.tvBreakfastMealContainer, R.id.tvBreakfastServingContainer};
HashMap<String, String> map = new HashMap<String, String>();
//initialize row data
map.put( "Meal", data1 );
map.put( "Serving", data2 );
mylistData.add( map );
//lv.setAdapter( new ArrayAdapter<String>( this,android.R.layout.simple_list_item_1, inputData ) );
//SimpleAdapter arrayAdapter =
// new SimpleAdapter(this, mylistData, R.layout.breakfast_listview_header,
// new String[] {"Meal", "Serving"}, columnIds);
//lv.setAdapter(arrayAdapter); */
lv.setAdapter( new MyCustomAdapter( inputData, inputData2 ) );
lv.setTextFilterEnabled( true );
info.close();
}
class MyCustomAdapter extends BaseAdapter
{
String[] data_meal;
String[] data_serving;
MyCustomAdapter()
{
data_meal = null;
data_serving = null;
}
MyCustomAdapter( String[] meal, String[] serving )
{
data_meal = meal;
data_serving = serving;
}
public int getCount()
{
return data_meal.length;
}
public String getItem( int position )
{
return null;
}
public long getItemId( int position )
{
return position;
}
public View getView( int position, View convertView, ViewGroup parent )
{
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate( R.layout.breakfast_listview_header, parent, false );
TextView txtMeal = ( TextView ) row.findViewById ( R.id.tvBreakfastMealContainer );
TextView txtServing = ( TextView ) row.findViewById ( R.id.tvBreakfastServingContainer );
txtMeal.setText( data_meal[ position ] );
txtServing.setText( data_serving[ position ] );
return (row);
}
}
please help me figure out what went wrong. Thanks.
While it will not fix the error this code should help you find out where its going wrong.
Try tweaking your getView() method to something like this: