0

我的 2 列列表视图中有一个 arrayoutofboundsexception 错误,

请在下面检查我的代码:

    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);

     }
 }

请帮我弄清楚出了什么问题。谢谢。

4

1 回答 1

0

虽然它不会修复错误,但此代码应该可以帮助您找出问题所在。

尝试将您的 getView() 方法调整为如下所示:

public View getView( int position, View convertView, ViewGroup parent )
{
    LayoutInflater inflater = getLayoutInflater();
    View 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 );

    if (position > data_meal.length)
    {
        txtMeal.setText(data_meal[position]);
    ]
    else
    {
        txtMeal.setText("MISSING!!");
    }

    if (position > data_serving.length)
    {
        txtServing.setText(data_serving[position]);
    ]
    else
    {
        txtServing.setText("MISSING!!");
    }

    return row;
}
于 2012-12-08T15:33:52.267 回答