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

时间:2016-05-13 19:59:40

标签: java android xml

我有一个包含多个项目的列表视图和一个用于启动新活动的按钮。当我运行我的应用程序时,我得到Null Pointer Exception。我如何以及在哪里设置OnClickListener才能正常运行?

另外,我如何通过Intent传递名为listaIngrediente的arraylist?

这是我的代码 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;
    }
}

1 个答案:

答案 0 :(得分:1)

您必须在MainActivity类中编写,因为您的按钮位于 main_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