1

我想在我的项目中显示两个列表视图。我正在使用单个适配器来设置两个列表视图的内容。如果我将两个列表视图的相同长度的数组传递给适配器,那么它工作正常,但是如果我传递不同的数组适配器中不同列表视图的长度,我的应用程序强制关闭并出现错误:-

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

请帮我...?

4

2 回答 2

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可能不会改变,因此它进入两个列表视图的同一部分(第二个)。那正确吗?

于 2013-09-24T12:15:13.437 回答
0

尝试mListItems.get(arg0)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);
}
于 2017-08-29T10:11:36.083 回答