2013-09-24 38 views
1

我想在我的Projects中顯示兩個ListView。我通過使用單個適配器來設置兩個listviews的內容。如果我將兩個listviews的相同長度的數組傳遞給適配器,罰款,但如果我通過不同長度在適配不同的ListView陣,我的應用程序強制關閉與錯誤: -用於多個ListView的單個適配器Android

>java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 
>09-24 11:55:10.359: E/AndroidRuntime(4822): at com.dropdownlistdemo.DropDownListAdapter.getView(DropDownListAdapter.java:98) 
>09-24 11:55:10.359: E/AndroidRuntime(4822): at android.widget.AbsListView.obtainView(AbsListView.java:2189) 
>09-24 11:55:10.359: E/AndroidRuntime(4822): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 
09-24 11:55:10.359: E/AndroidRuntime(4822):  at android.widget.ListView.onMeasure(ListView.java:1155) 
09-24 11:55:10.359: E/AndroidRuntime(4822):  at android.view.View.measure(View.java:12775) 
09-24 11:55:10.359: E/AndroidRuntime(4822):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594) 

我的適配器代碼: -

public class DropDownListAdapter extends BaseAdapter { 

private ArrayList<String> mListItems; 
private LayoutInflater mInflater; 

private static int selectedCount = 0; 
private static String firstSelected = ""; 
private ViewHolder holder; 
private static String selected = ""; //shortened selected values representation 
String car_type; 
public static String getSelected() { 
    return selected; 
} 

public void setSelected(String selected) { 
    DropDownListAdapter.selected = selected; 
} 

public DropDownListAdapter(Context context, ArrayList<String> items) { 
    mListItems = new ArrayList<String>(); 
    mListItems.addAll(items); 
    mInflater = LayoutInflater.from(context); 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return mListItems.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.drop_down_list_row, null); 
     holder = new ViewHolder(); 
     holder.tv = (TextView) convertView.findViewById(R.DropDownList.SelectOption); 
     holder.chkbox = (CheckBox) convertView.findViewById(R.DropDownList.checkbox); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    //holder.tv.setText(mListItems.get(position)); 

    final int position1 = position; 

    //whenever the checkbox is clicked the selected values textview is updated with new selected values 
    holder.chkbox.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      setText(position1); 
     } 
    }); 


    if(DropDownListDemo.car_type) 
    { 
     if(DropDownListDemo.checkSelected_cartype[position]) 
      holder.chkbox.setChecked(true); 
     else 
      holder.chkbox.setChecked(false);  
    } 
    else 
    { 
     if(DropDownListDemo.checkSelected_transmissontype[position]) 
      holder.chkbox.setChecked(true); 
     else 
      holder.chkbox.setChecked(false);  
    } 


    return convertView; 
} 


/* 
* Function which updates the selected values display and information(checkSelected[]) 
*/ 
private void setText(int position1){ 

    if(DropDownListDemo.car_type) 
    { 
    if (!DropDownListDemo.checkSelected_cartype[position1]) { 
     DropDownListDemo.checkSelected_cartype[position1] = true; 
     selectedCount++; 
    } else { 
     DropDownListDemo.checkSelected_cartype[position1] = false; 
     selectedCount--; 
    } 

    if (selectedCount == 0) { 
     //mSelectedItems.setText(R.string.select_string); 
    } else if (selectedCount == 1) { 
     for (int i = 0; i <DropDownListDemo.checkSelected_cartype.length; i++) { 
      if (DropDownListDemo.checkSelected_cartype[i] == true) { 
       firstSelected = mListItems.get(i); 
       break; 
      } 
     } 
     //mSelectedItems.setText(firstSelected); 
     setSelected(firstSelected); 
    } else if (selectedCount > 1) { 
     for (int i = 0; i < DropDownListDemo.checkSelected_cartype.length; i++) { 
      if (DropDownListDemo.checkSelected_cartype[i] == true) { 
       firstSelected = mListItems.get(i); 
       break; 
      } 
     } 

    } 
    } 
    else 
    { 

     if (!DropDownListDemo.checkSelected_transmissontype[position1]) { 
      DropDownListDemo.checkSelected_transmissontype[position1] = true; 
      selectedCount++; 
     } else { 
      DropDownListDemo.checkSelected_transmissontype[position1] = false; 
      selectedCount--; 
     } 

     if (selectedCount == 0) { 
      //mSelectedItems.setText(R.string.select_string); 
     } else if (selectedCount == 1) { 
      for (int i = 0; i <DropDownListDemo.checkSelected_transmissontype.length; i++) { 
       if (DropDownListDemo.checkSelected_transmissontype[i] == true) { 
        firstSelected = mListItems.get(i); 
        break; 
       } 
      } 
      //mSelectedItems.setText(firstSelected); 
      setSelected(firstSelected); 
     } else if (selectedCount > 1) { 
      for (int i = 0; i < DropDownListDemo.checkSelected_transmissontype.length; i++) { 
       if (DropDownListDemo.checkSelected_transmissontype[i] == true) { 
        firstSelected = mListItems.get(i); 
        break; 
       } 
      } 



    } 

    //mSelectedItems.setText(firstSelected + " & "+ (selectedCount - 1) + " more"); 
     setSelected(firstSelected + " & "+ (selectedCount - 1) + " more"); 
    } 
} 

void getselected_checkboxes() 
{ 
    if(car_type != null && !car_type.equals("")) 
    { 
     car_type=""; 
    } 



    for (int i = 0; i < DropDownListDemo.checkSelected_cartype.length; i++) { 

     if (DropDownListDemo.checkSelected_cartype[i] == true){ 

      Log.w("checked items"," "+mListItems.get(i)); 


      if(car_type != null && !car_type.equals("")) 
      { 
       car_type=car_type+","+mListItems.get(i); 
      } 
      else 
      { 

       car_type=mListItems.get(i); 
      } 


     } 


    } 

    car_type = car_type.replaceAll(" ", "%20"); 


    Log.w("car_type",""+car_type); 



} 








private class ViewHolder { 
    TextView tv; 
    CheckBox chkbox; 
} 
    } 

我在Listview中將適配器設置爲: -

ArrayList<String> items_transmission = new ArrayList<String>(); 
     items_transmission.add("Automatic"); 
     items_transmission.add("Manual"); 

    adapter1 = new DropDownListAdapter(DropDownListDemo.this, items_transmission); 



     transmisson_type.setAdapter(adapter1); 

    ArrayList<String> items = new ArrayList<String>(); 
    items.add("Cars"); 
    items.add("Passenger Van"); 
    items.add("SUV"); 
    adapter = new DropDownListAdapter(DropDownListDemo.this, items); 


    list.setAdapter(adapter); 

請幫我...?

回答

0

我認爲問題出在這個部分。

if(DropDownListDemo.car_type) 
{ 
    if(DropDownListDemo.checkSelected_cartype[position]) 
     holder.chkbox.setChecked(true); 
    else 
     holder.chkbox.setChecked(false);  
} 
else 
{ 
    if(DropDownListDemo.checkSelected_transmissontype[position]) 
     holder.chkbox.setChecked(true); 
    else 
     holder.chkbox.setChecked(false);  
} 

當你要求連續2名列表視圖代碼中,DropDownListDemo.car_type值可能不會改變,因此它進入同一部分(第二)兩個列表視圖。是對的嗎?

+1

沒有也幫我... –

+0

哪條線是'DropDownListAdapter' 98返回mListItems.get(arg0)? – Szymon

+1

if(DropDownListDemo.checkSelected_transmissontype [position]) –

0

嘗試在getItem(arg0)方法

public DropDownListAdapter(Context context, ArrayList<String> items) { 
    mListItems = items; 
    mInflater = LayoutInflater.from(context); 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return mListItems.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return mListItems.get(arg0); 
} 
相關問題