2012-12-08 31 views
0

我有一個arrayoutofboundsexception錯誤在我的2列的列表視圖,arrayoutofboundsexception的Android定製適配器列表視圖

請檢查下面我的代碼:

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

    } 
} 

請幫我找出什麼地方出了錯。謝謝。

+3

確保'data_meal'和'data_serving'數組的大小相同,或確保您的適配器採用此考慮。 – Luksprog

+0

請閱讀listview優化 – Mikhaili

+0

@Luksprog嗨,它不顯示數組元素。 –

回答

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; 
} 
+0

你好,它顯示丟失;(所以一定有什麼問題,我的適配器 –

+0

對不起,我錯過了你的答覆顯示丟失將表明數據的問題更具體地,我希望看到inputData []比inputData2 []更多的元素 –