我想显示从listview1
(垂直)到listview2
(水平)中选择的项目。当我将项目添加到我的列表时,我收到错误:
12668-12668/com.example.vdovin.restaurantmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.vdovin.restaurantmenu.CustomBaseAdapter.getItem(CustomBaseAdapter.java:56)
at com.example.vdovin.restaurantmenu.MenuActivity$1.onItemClick(MenuActivity.java:230)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1130)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2818)
at android.widget.AbsListView$1.run(AbsListView.java:3498)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
我不明白为什么List的大小不会随着元素的添加而扩展。仅当我在列表中添加所有项目然后显示listview
来自MenuActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.variety_of_dishes);
listView2 = (ListView)findViewById(R.id.listView2);
horizontalListView = (HorizontalListView)findViewById(R.id.horizontalListView);
listObjects = new ArrayList<MenuItem>();
getDishesList();
ownAdapter = new CustomBaseAdapter(listObjects, MenuActivity.this);
horizontalListView.setAdapter(ownAdapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
testObject = ownAdapter.getItem(position);
final Object test2Object = ownAdapter.priceOfDishes(position);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MenuActivity.this);
alertDialogBuilder.setTitle("Заказ");
alertDialogBuilder.setMessage("Сколько порций вы хотите заказать?");
final EditText numberOfDishes = new EditText(MenuActivity.this);
numberOfDishes.setInputType(InputType.TYPE_CLASS_NUMBER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
numberOfDishes.setLayoutParams(lp);
alertDialogBuilder.setView(numberOfDishes);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Подтвердить", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
addToOrderList((MenuItem) testObject);
}
});
alertDialogBuilder.setNegativeButton("Отмена", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
public void addToOrderList(MenuItem object){
listObjects.add(object);
ownAdapter.notifyDataSetChanged();
}
CustomBaseAdapter
:
public class CustomBaseAdapter extends BaseAdapter {
private static LayoutInflater inflater=null;
Context context;
List<MenuItem> menuItems;
public CustomBaseAdapter(List<MenuItem> menuItems, Context context) {
this.menuItems = menuItems;
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder{
ImageView imageView;
TextView txtTitle;
TextView txtPrice;
}
public double priceOfDishes(int position){
MenuItem doubleItem = (MenuItem)getItem(position);
String priceItem = doubleItem.getItemPrice();
double finalPrice = Double.parseDouble(priceItem);
return finalPrice;
}
@Override
public int getCount() {
return menuItems.size();
}
@Override
public Object getItem(int position) {
return menuItems.get(position);
}
@Override
public long getItemId(int position) {
return menuItems.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater myInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView ==null || !(convertView.getTag() instanceof ViewHolder) ){
convertView = myInflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.image_View);
viewHolder.txtTitle = (TextView)convertView.findViewById(R.id.item_title);
viewHolder.txtPrice = (TextView) convertView.findViewById(R.id.price);
convertView.setTag(myInflater);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
MenuItem menuItem = (MenuItem)getItem(position);
viewHolder.imageView.setImageResource(menuItem.getImageId());
viewHolder.txtTitle.setText(menuItem.getItemTitle());
viewHolder.txtPrice.setText(menuItem.getItemPrice());
return convertView;
}}
答案 0 :(得分:1)
您的listView2
似乎没有连接到适配器。
因此,点击listView2
的第一项将由您的ItemClickListener处理,它与horizontalListView
的适配器一起使用。
问题就在这里:
testObject = ownAdapter.getItem(position);
您正在阅读的同一个列表中添加项目。我想你错过了另一个列表或另一个适配器...
答案 1 :(得分:0)
替换 MenuItem menuItem =(MenuItem)getItem(position);
带
MenuItem menuItem = menuItems.get(position);