ImageView khi click vào không hiển thị được?

Mình có 1 ImageView từ 1 layout khác khi gọi trong activity_main layout thì lại không cho ra kết quả khi click vào. Làm sao để khắc phục ?

MainActivity.java

public class MainActivity extends AppCompatActivity {
ImageView imageViewXoa;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
GoiView();
}
private void GoiView() {
    LayoutInflater layoutInflater = getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.activity_line, null);
    imageViewXoa = (ImageView) view.findViewById(R.id.imageView_Xoa);
    imageViewXoa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "aaa", Toast.LENGTH_SHORT).show();
        }
    });
}

Đây là layout chứa ImageView

Bạn mới Inflat view nhưng đã add view vào main activity đâu
Gửi luôn code của main activity layout lên.

2 Likes

Add bằng cách nào nhỉ ? Mình ko rõ lắm ^^!

Bạn gửi luôn code của main activity lên đi

package com.example.sotay;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

`public class MainActivity extends AppCompatActivity {`

    ListView listViewGhiChu;
    ImageView imageViewThem, imageViewXoa;

    DBManager dbManager;
    SimpleCursorAdapter simpleCursorAdapter;

    final String[] from = new String[] {DatabaseHelper._ID, DatabaseHelper.TIEUDE, DatabaseHelper.NOIDUNG};
    final int[] to = new int[] {R.id.textViewId, R.id.textViewTieuDe, R.id.textViewNoiDung};

    long _id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AnhXa();

        listViewGhiChu.setEmptyView(findViewById(R.id.textViewRong));

        dbManager = new DBManager(this);
        dbManager.open();

        // Lấy toàn bộ nội dung hiện có
        Cursor cursor = dbManager.fetch();

        simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.line_layout, cursor, from, to, 0);
        simpleCursorAdapter.notifyDataSetChanged();
        listViewGhiChu.setAdapter(simpleCursorAdapter);

        imageViewThem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ActivityAdd.class));
            }
        });

        listViewGhiChu.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textViewId = (TextView) view.findViewById(R.id.textViewId);

                String strId = textViewId.getText().toString();
                _id = Long.parseLong(strId);
                dbManager.delete(_id);
                // Reload lại activity
                recreate();

                return false;
            }
        });

        GoiView();
    }

    private void AnhXa() {
        listViewGhiChu = (ListView) findViewById(R.id.listViewGhiChu);
        imageViewThem = (ImageView) findViewById(R.id.imageViewThem);
    }

    private void GoiView() {
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.line_layout, null, false);
        imageViewXoa = view.findViewById(R.id.imageView_Xoa);
        imageViewXoa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "aaa", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Ý mình là cái file XML ấy

Theo mình đoán là bạn đang muốn nhận sự kiện click vào nút xóa của 1 dòng trong 1 list các item trong list view phỏng

oh. Đây, 2 file activity_main.xml và line_layout.xml:

activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextView
    android:id="@+id/textViewRong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chưa có nội dung"
    android:textColor="#000"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:id="@+id/listViewGhiChu"
    android:layout_width="0dp"
    android:layout_height="448dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="13dp"
    app:layout_constraintBottom_toTopOf="@+id/imageViewThem"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/imageViewThem"
    android:layout_width="65dp"
    android:layout_height="57dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="15dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/button_add" />

<Button
    android:id="@+id/buttonXoaTatCa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="191dp"
    android:layout_marginRight="191dp"
    android:layout_marginBottom="16dp"
    android:text="Xóa tất cả"
    android:textAllCaps="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/imageViewThem"
    app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

line_layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/textViewTieuDe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="178dp"
    android:layout_marginRight="178dp"
    android:text="Tiêu đề"
    android:textColor="#000"
    android:textSize="24sp"
    app:layout_constraintEnd_toStartOf="@+id/imageView_Xoa"
    app:layout_constraintStart_toEndOf="@+id/textViewId"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/imageViewSua"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageView_Xoa"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/ic_menu_edit" />

<ImageView
    android:id="@+id/imageView_Xoa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/imageViewSua"
    app:layout_constraintStart_toEndOf="@+id/textViewTieuDe"
    app:layout_constraintTop_toTopOf="@+id/imageViewSua"
    app:srcCompat="@android:drawable/ic_delete" />

<TextView
    android:id="@+id/textViewId"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="4dp"
    android:text="ID"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="@+id/textViewTieuDe"
    app:layout_constraintEnd_toStartOf="@+id/textViewTieuDe"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textViewTieuDe" />

<TextView
    android:id="@+id/textViewNoiDung"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:text="Nội dung"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="@+id/textViewTieuDe"
    app:layout_constraintTop_toBottomOf="@+id/textViewTieuDe" />
</android.support.constraint.ConstraintLayout>

Đúng rồi bạn :slight_smile:

Bạn đang bind event sai rồi. Hàm inflate sẽ sinh ra 1 view từ file XML mẫu. Nên mỗi lần gọi sẽ ra 1 view khác nhau. Nên bạn set listenner vào cũng như không.
Để custom TH này thì bạn phải extends từ SimpleCursorAdapter, override lại method getView để inflate custom view của bạn, set listenner ở trong method đó luôn.
Chi tiết bạn search theo từ khóa “custom listview adapter” là ra. Cả tiếng anh và tiếng Việt đều có. SimpleCursorAdapter cũng cùng 1 cách custom view giống ArrayAdapter nên bạn có thể copy code cho nhau được.
Ngoài ra bây giờ dev android thường dùng RecycleView thay cho ListView rồi. Sau khi tìm hiểu xong về các loại AdapterView cũ này thì bạn nghiên cứu thêm RecycleView

2 Likes

Mình có cần phải tạo 1 class rồi extends SimpleCursorAdapter vô ko ?
Mình cũng mới tìm hiểu Android được khoảng 3 tháng, để hôm nào làm thử RecycleView xem thế nào.

Sorry bạn. Mình mới tìm được cách hay hơn cho bạn đó là bạn sử dụng method setViewBinder của cái SimpleCurrsorAdapter. Bạn set 1 custom ViewBinder vào là bạn đã có thể can thiệp vào quá trình setup view của từng view trong từng item trong list rồi.

2 Likes

Ok. Để mình thử, bạn có fb ko ? Thêm bạn với mình ? Có gì trao đổi thêm :blush:

Mình không, có gì vướng mắc bạn chỉ đặt câu hỏi trên DNH, sẽ có người khác trả lời cho bạn. Ngày bình thường mình bận việc sẽ không check thường xuyên được

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?