java - 如何在一个类中使用来自不同 xml 文件的按钮

标签 java android xml

我有一个包含多个项目的 ListView 和一个用于启动新 Activity 的按钮。我在运行我的应用程序时遇到空指针异常。我应该如何以及在何处设置 OnClickListener 以正常运行?

此外,我如何通过 Intent 传递名为 listaIngrediente 的数组列表?

这是我的代码 main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.radu.fridgecheck.MainActivity"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </ListView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/find_recipies"
        android:id="@+id/find"
        android:layout_weight="0"/>
</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox"
        android:layout_alignBottom="@+id/checkBox"
        android:layout_toRightOf="@+id/checkBox"
        android:text="TextView" />  
</RelativeLayout>

适配器

    public class Adapter extends ArrayAdapter<Ingredient> {

    public LayoutInflater inflater;
    public ArrayList<Ingredient> listaIngrediente;
    ArrayList<String> ingredienteDisponibile;
    private Activity activity;


    public Adapter(Activity activity, int textResourceId, ArrayList<Ingredient> ingrediente) {
        super(activity, textResourceId, ingrediente);
        this.activity=activity;
        this.listaIngrediente=ingrediente;

        try {
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int getCount() {
        return listaIngrediente.size();
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (convertView == null) {
            v = inflater.inflate(R.layout.list_item, null);
        }

        final Ingredient ingredients = getItem(position);

        final TextView display_ingredients = (TextView) v.findViewById(R.id.name);
        display_ingredients.setText(ingredients.getNameI());

        final CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);

        ListView listView = (ListView) v.findViewById(R.id.listView1);
        Button button = (Button) v.findViewById(R.id.button);
        listView.addFooterView(button);

        Button find = (Button) v.findViewById(R.id.find);
        find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getContext(), ShowRecipes.class);
                Bundle args = new Bundle(); /// nu reusesc sa transfer obiectele
                args.putSerializable("ARRAYLIST",listaIngrediente);
                i.putExtra("BUNDLE",args);
                activity.startActivity(i);
            }
        });

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!ingredients.isSelected()) {
                    checkBox.setChecked(true);
                    ingredients.setSelected(true);
                }
                else{
                    checkBox.setChecked(false);
                    ingredients.setSelected(false);                  
                }
            }
        });
        return v;
    }
}

最佳答案

你必须在 MainActivity 类中编写它,因为你的按钮在 ma​​in_activity.xml

在MainActivity中,写这个方法

public void showRecipes(View view) {

   Intent i = new Intent(getContext(), ShowRecipes.class);
            Bundle args = new Bundle(); /// nu reusesc sa transfer obiectele
            args.putSerializable("ARRAYLIST",listaIngrediente);
            i.putExtra("BUNDLE",args);
            startActivity(i);
}

在xml中添加onClick

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/find_recipies"
    android:id="@+id/find"
    android:onClick="showRecipes"
    />`

有关点击事件的更多信息 http://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents

关于java - 如何在一个类中使用来自不同 xml 文件的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218442/

相关文章:

xml - XSLT 和 XML - 将图像分组并显示到表格中

Java:枚举中的异常?

java - 是否有必要将 @JsonIgnoreProperties 放置到 Spring JPA 域中的所有非 JSON 类?

java - 在主类中使用另一个类的方法?

xml - 清理 XML 的正则表达式

Android布局问题,角落里有图片的文字

java - 如何设置 Web 组件和 Java 应用程序之间的连接

java - Android 触摸事件 - 具有向上向下和移动事件

java - 如何将异步任务与现有代码一起使用

android - 为什么我的RelativeLayout不使用选择器?