我想在我的Project中显示两个Listview。我正在使用一个适配器来设置两个listviews的内容。如果我将两个listviews的相同长度的数组传递给适配器,那么它工作正常,但如果我通过适配器中不同listview的不同长度数组,我的应用程序强制关闭错误: -
>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 :(得分: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)
尝试在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);
}