onClick不在Fragment中工作

时间:2016-11-04 06:12:01

标签: java android android-fragments onclick onclicklistener

基本上我的片段布局中有listview。另一个名为item_todo.xml的布局包含我的按钮。 单击布局中的delete_task按钮时,onClick()无法正常工作。 如果你能帮帮我的话会很棒!

这是我的Class ToDoFragment

public class ToDoFragment extends Fragment {

    private static final String TAG = "MainActivity";
    private TaskDbHelper mHelper;
    private ListView mTaskListView;
    private ArrayAdapter<String> mAdapter;
    Button myButton;
    FloatingActionButton btnadd;
    //Overriden method onCreateView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_two, container, false);



        mHelper = new TaskDbHelper(getActivity());
        mTaskListView = (ListView) view.findViewById(R.id.list_todo);

        //Floating action button

        btnadd = (FloatingActionButton) view.findViewById(R.id.btnadd);
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText taskEditText = new EditText(getActivity());
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        .setTitle("Add a new task")
                        .setMessage("What do you want to do next?")
                        .setView(taskEditText)
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String task = String.valueOf(taskEditText.getText());
                                SQLiteDatabase db = mHelper.getWritableDatabase();
                                ContentValues values = new ContentValues();
                                values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
                                db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
                                        null,
                                        values,
                                        SQLiteDatabase.CONFLICT_REPLACE);
                                db.close();
                                updateUI();
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .create();
                dialog.show();
            }
        });


        updateUI();
        return view;
    }

    @Override
    public void onActivityCreated(Bundle saved){
        super.onActivityCreated(saved);
        final LayoutInflater factory = getActivity().getLayoutInflater();
        final View textEntryView = factory.inflate(R.layout.item_todo, null);
        myButton = (Button) textEntryView.findViewById(R.id.task_delete);
        myButton.setOnClickListener(new View.OnClickListener() {
            // OnClick() Not Working !!

            @Override
            public void onClick(View view) {

                deleteTask(view);
            }
        });
    }

    public void deleteTask(View view) {
        View parent = (View) view.getParent();
        TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
        String task = String.valueOf(taskTextView.getText());
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.delete(TaskContract.TaskEntry.TABLE,
                TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
                new String[]{task});
        db.close();
        updateUI();
    }


    private void updateUI() {
        ArrayList<String> taskList = new ArrayList<>();
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
                new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
                null, null, null, null, null);
        while (cursor.moveToNext()) {
            int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
            taskList.add(cursor.getString(idx));
        }

        if (mAdapter == null) {
            mAdapter = new ArrayAdapter<>(getActivity(),
                    R.layout.item_todo,
                    R.id.task_title,
                    taskList);
            mTaskListView.setAdapter(mAdapter);
        } else {
            mAdapter.clear();
            mAdapter.addAll(taskList);
            mAdapter.notifyDataSetChanged();
        }

        cursor.close();
        db.close();
    }}

Fragment_two XML                     

        <ListView
            android:id="@+id/list_todo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:orientation="horizontal" >


            <android.support.design.widget.FloatingActionButton
                android:layout_width="50dp"
                android:id="@+id/btnadd"
                android:layout_height="50dp"
                app:fabSize="normal"
                android:clickable="true"
                android:src="@drawable/red"
                android:scaleType="center"
                />
        </LinearLayout>


    </RelativeLayout>

item_todo XML

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical">

    <TextView
        android:id="@+id/task_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="Hello"
        android:textSize="20sp" />

    <Button
        android:id="@+id/task_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="Done" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您在onActivityCreated中为新视图充气但是您从未使用过该视图! myButton指的是一个未使用其视图的按钮!

编辑:

您可以通过多种方式实现目标:

  

接口

您可以初始化界面并将其传递给片段,因此,只要您的活动中发生某些事情(例如按钮点击),您就会理解

  

BroadcastReceivers

您可以在片段中注册本地广播接收器,当用户点击按钮时,您发送该广播,因此片段将理解

我建议第二个并使用EVENTBUS