在android studio中创建LISTVIEW但无法运行应用程序(“不停地停止”)?

eqzww0vc  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(200)

我在android studio中做了一个列表视图,表达式是:List your menu,这是我在Menu.java上的代码

package com.example.listviewassigment;

public class Menu {
    String name,nodishes,sale;
    int image;

    public Menu(String name, String nodishes, String sale, int image) {
        this.name = name;
        this.nodishes = nodishes;
        this.sale = sale;
        this.image = image;
    }
}

字符串
这是MenuAdapter.java。我认为这里有问题,因为我找不到android.widget.ArrayAdapter上的getView方法,我只是在没有建议的情况下编写代码:

package com.example.listviewassigment;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MenuAdapter extends ArrayAdapter<Menu> {
    private int position;
    @Nullable
    private View view;
    private ViewGroup parent;

    public MenuAdapter(@NonNull Context context, @NonNull ArrayList<Menu> dataArrayList) {
        super(context, R.layout.item_dish, dataArrayList);
    }

    @Nullable
    @Override
    public View getView(int position, @Nullable View View, @NonNull ViewGroup parent) {
        Menu listmenu = getItem(position);

        if(view==null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_dish,parent,false);
        }

        ImageView imagedish = view.findViewById(R.id.imagedish);
        TextView titledish = view.findViewById(R.id.titledish);
        TextView nodishes = view.findViewById(R.id.nodishes);
        TextView nodiscounteddishes = view.findViewById(R.id.nodiscounteddishes);

        imagedish.setImageResource(listmenu.image);
        titledish.setText(listmenu.name);
        nodishes.setText(listmenu.nodishes);
        nodiscounteddishes.setText(listmenu.sale);

        return view;
    }
}


在MainActivity.java中:

package com.example.listviewassigment;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.listviewassigment.databinding.ActivityMainBinding;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    MenuAdapter menuAdapter;
    ArrayList<Menu> dataArrayList = new ArrayList<>();
    Menu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setContentView(binding.getRoot());

        int[] imagedish = {R.drawable.pizza,R.drawable.desert,R.drawable.beverage};
        String[] titledish = {"Pizza","Desert","Beverage"};
        String[] nodishes = {"5 sản phẩm","3 sản phẩm","6 sản phẩm"};
        String[] nodiscounteddishes = {"5 đang giảm giá","3 đang giảm giá","6 đang giảm giá"};

        for (int i=0;i< imagedish.length;i++) {
            menu = new Menu(titledish[i],nodishes[i],nodiscounteddishes[i],imagedish[i]);
            dataArrayList.add(menu);
        }
        menuAdapter = new MenuAdapter(MainActivity.this,dataArrayList);
        binding.lsMenu.setAdapter(menuAdapter);
        binding.lsMenu.setClickable(true);
    }
}


我不知道这个应用程序有什么问题。当我运行它时,它一直停止。请,我需要尽快解决这个问题,所以请帮助我。对不起,因为我的英语不好,解释我的问题。

jvlzgdj9

jvlzgdj91#

您在MainActivity和MenuAdapter中有2个问题。

  1. MainActivity
    替换此代码
setContentView(R.layout.activity_main);
setContentView(binding.getRoot());

字符串

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());


1.固定菜单适配器

public class MenuAdapter extends ArrayAdapter<Menu> {
public MenuAdapter(Context context, ArrayList<Menu> dataArrayList) {
    super(context, R.layout.item_dish, dataArrayList);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.item_dish, parent, false);
    }

    Menu listmenu = getItem(position);

    ImageView imagedish = view.findViewById(R.id.imagedish);
    TextView titledish = view.findViewById(R.id.titledish);
    TextView nodishes = view.findViewById(R.id.nodishes);
    TextView nodiscounteddishes = view.findViewById(R.id.nodiscounteddishes);

    if (listmenu != null) {
        imagedish.setImageResource(listmenu.image);
        titledish.setText(listmenu.name);
        nodishes.setText(listmenu.nodishes);
        nodiscounteddishes.setText(listmenu.sale);
    }

    return view;
}


这段代码也可以进行优化,但是可以在检查其性能之后进行优化。

相关问题