我正在尝试从phpmyadmin到Android获取listview数据库。进入食物页面时我一直在崩溃。我的代码没有错误,我检查我的android监视器,我看到错误。
04-25 00:12:17.273 1731-1731/com.fypunimas.fyp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxxx, PID: 1731
java.lang.ClassCastException: java.lang.String cannot be cast to com.xxxx.Food
at com.xxxx.CustomAdapter.getView(CustomAdapter.java:66)
at android.widget.AbsListView.obtainView(AbsListView.java:2486)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
at android.widget.ListView.layoutChildren(ListView.java:1744)
at android.widget.AbsListView.onLayout(AbsListView.java:2266)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:874)
at android.view.View.layout(View.java:17723)
at android.view.ViewGroup.layout(ViewGroup.java:5739)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2350)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2072)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6468)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:908)
at android.view.Choreographer.doCallbacks(Choreographer.java:720)
at android.view.Choreographer.doFrame(Choreographer.java:655)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:894)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6256)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:793)
从错误中,它声明“最终食物食物=(食物)getItem(位置);”有一些问题。我的customadapter.activity代码如下
class CustomAdapter extends BaseAdapter {
public ArrayList<String> stringlist;
private Context context;
CustomAdapter(Context context, ArrayList<String> stringlist) {
//super(context, R.layout.item_food, stringlist);
this.context = context;
this.stringlist = stringlist;
}
public int getCount(){
return stringlist.size();
}
public Object getItem(int position){
return stringlist.get(position);
}
public long getItemId(int position){
return 0;
}
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final ListViewHolder listViewHolder;
View customView;
if(convertView == null)
{
LayoutInflater menuInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater menuInflater = LayoutInflater.from(getContext());
customView = menuInflater.inflate(R.layout.item_food, parent, false);
listViewHolder = new ListViewHolder();
// final String singlemenuitem = (String) getItem(position);
listViewHolder.tvfoodname = (TextView) customView.findViewById(R.id.tvfoodname);
listViewHolder.btnadd = (ImageButton) customView.findViewById(R.id.btnadd);
listViewHolder.btnminus = (ImageButton) customView.findViewById(R.id.btnminus);
listViewHolder.etquantity = (EditText) customView.findViewById(R.id.etquantity);
customView.setTag(listViewHolder);
}
else
{
customView=convertView;
listViewHolder= (ListViewHolder) customView.getTag();
}
// listViewHolder.tvfoodname.setText(singlemenuitem);
final Food foods=(Food)getItem(position);
listViewHolder.tvfoodname.setText(foods.getFoodname());
listViewHolder.etquantity.setText(foods.cartQuantity);
listViewHolder.btnadd.setImageResource(R.drawable.plus);
listViewHolder.btnadd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Food.cartQuantity=Integer.parseInt(etquantity.getText().toString().trim());
// Food.cartQuantity = Food.cartQuantity + 1;
// etquantity.setText("" + Food.cartQuantity);
updateQuantity(position,listViewHolder.etquantity,1);
}
});
listViewHolder.btnminus.setImageResource(R.drawable.minus);
listViewHolder.btnminus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// Food.cartQuantity = Integer.parseInt(etquantity.getText().toString().trim());
// if ( Food.cartQuantity == 0) {
// Food.cartQuantity= 0;
// } else {
// Food.cartQuantity = Food.cartQuantity - 1;
// }
// etquantity.setText("" + Food.cartQuantity);
updateQuantity(position,listViewHolder.etquantity,-1);
}
});
return customView;
}
答案 0 :(得分:0)
您只是将foodname
数组作为数组传递给适配器,然后尝试将foodname
(string
)投射到您内部的Food
对象中适配器绝对不可能。
解决方案:使用Food
创建一个arraylist然后将此arraylist传递给您的适配器
或者如果您只需要在适配器中打印foodname
,只需添加如下
final String foodname=(String)getItem(position);
listViewHolder.tvfoodname.setText(foodname);
********* 更新 *********
像这样创建一个arraylist
ArrayList<Food> food_data=new ArrayList<>();
然后像这样填充
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
String name=obj.getString("foodname");
String quantity=obj.getString("add_json_key_for_quantity");
Food food=new Food();
food.setFoodname(name);
food.seCartQuantity(quantity);
food_data.add(food);
}
BaseAdapter ListAdapter = new CustomAdapter(this,food_data);
listView2.setAdapter(ListAdapter);
现在更改适配器构造函数
ArrayList<Food> food_data=new ArrayList<>();
private Context context;
CustomAdapter(Context context, ArrayList<Food> food_data) {
//super(context, R.layout.item_food, stringlist);
this.context = context;
this.food_data = food_data;
}
现在可以像这样访问
final Food foods=food_data.get(position);
listViewHolder.tvfoodname.setText(foods.getFoodname());
listViewHolder.etquantity.setText(foods.getCartQuantity);
答案 1 :(得分:0)
您正在尝试将String类型转换为不兼容的Food类型对象。 试试:
Food food = new Food();
food.setFoodName(getItem(position))
..等等食物模型的其他属性。 我的建议是在解析时创建Food类型的List,然后将它传递给CustomList适配器。
希望它有所帮助。