onItemClick不适用于适配器(线性布局)

时间:2018-08-25 07:56:21

标签: android xml android-layout

我有我的自定义适配器:

allergy2_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical">

        <LinearLayout
        android:layout_width="320dp"
        android:layout_height="100dp"
        android:orientation="vertical"
        >
        <TextView
        android:id="@+id/task_item_name"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
            />

        <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/task_subitem_name"
        android:textSize="15dp"
            />



    </LinearLayout>

<CheckBox
android:id="@+id/task_item_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

我需要创建适配器,以便它像这样: adapter view

我尝试了不同的布局来制作-2个线性布局,相对布局,约束布局。但是它们不可点击。 (当只有一个线性布局(带有textview和复选框)时,它可以正常工作。) 因此,当我在其中添加另一个线性布局时,单击开始不起作用。

Allergy2.java

public class Allergy2 extends AppCompatActivity {
    private Realm realm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allergy2);
        realm = Realm.getDefaultInstance();
        RealmResults<Allergy2Task> tasks = realm.where(Allergy2Task.class).findAll();
        tasks = tasks.sort("timestamp");
        final Allergy2Adapter adapter = new Allergy2Adapter(this, tasks);

        ListView listView = (ListView) findViewById(R.id.task_list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Context context = view.getContext();
                LinearLayout layout = new LinearLayout(context);
                layout.setOrientation(LinearLayout.VERTICAL);


                final Allergy2Task task = (Allergy2Task) adapterView.getAdapter().getItem(i);
                final EditText taskEditText = new EditText(Allergy2.this);
                final EditText task2EditText = new EditText(Allergy2.this);
                taskEditText.setSingleLine(true);
                taskEditText.setHint("Type ingredient");
                taskEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

                task2EditText.setSingleLine(true);
                task2EditText.setHint("Type synonyms(if any) separated by commas");
                task2EditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

                layout.addView(taskEditText);
                layout.addView(task2EditText);

                AlertDialog dialog = new AlertDialog.Builder(Allergy2.this)
                        .setTitle("Edit allergen")
                        .setView(layout)
                        .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String ed_text = taskEditText.getText().toString().trim();
                                if (ed_text.isEmpty()) {
                                    Toast toast = Toast.makeText(getApplicationContext(),
                                            "The field cannot be empty", Toast.LENGTH_SHORT);
                                    toast.show();

                                } else {
                                    changeTaskName(task.getId(), String.valueOf(taskEditText.getText()));
                                }
                            }
                        })
                        .setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                deleteTask(task.getId());
                            }
                        })
                        .create();
                dialog.show();
            }
        });
}

这是此类的适配器: Allergy2Adapter.java

public class Allergy2Adapter extends RealmBaseAdapter<Allergy2Task> implements ListAdapter {

    private Allergy2 activity;
    private Realm realm;

    private static class ViewHolder {
        TextView taskName;
        CheckBox isTaskDone;
    }

    Allergy2Adapter(Allergy2 activity, OrderedRealmCollection<Allergy2Task> data) {
        super(data);
        this.activity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.allergy2_row, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.taskName = (TextView) convertView.findViewById(R.id.task_item_name);
            viewHolder.isTaskDone = (CheckBox) convertView.findViewById(R.id.task_item_done);
            viewHolder.isTaskDone.setOnClickListener(listener);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (adapterData != null) {
            Allergy2Task task = adapterData.get(position);
            viewHolder.taskName.setText(task.getName());
            viewHolder.isTaskDone.setChecked(task.isDone());
            viewHolder.isTaskDone.setTag(position);
        }


        return convertView;
    }

    private View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            int position = (Integer) view.getTag();
            if (adapterData != null) {
                Allergy2Task task = adapterData.get(position);
                activity.changeTaskDone(task.getId());
            }

        }

    };



}

1 个答案:

答案 0 :(得分:1)

尝试

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.allergy2_row, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.taskName = (TextView) convertView.findViewById(R.id.task_item_name);
            viewHolder.isTaskDone = (CheckBox) convertView.findViewById(R.id.task_item_done);
            viewHolder.isTaskDone.setOnClickListener(listener);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (adapterData != null) {
            Allergy2Task task = adapterData.get(position);
            viewHolder.taskName.setText(task.getName());
            viewHolder.isTaskDone.setChecked(task.isDone());
            viewHolder.isTaskDone.setTag(position);
        }
             converView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

            if (adapterData != null) {
                Allergy2Task task = adapterData.get(position);
                activity.changeTaskDone(task.getId());
            } 
                }
            });

        return convertView;
    }