0

I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program

LeadActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".LeadSalesActivity">

<LinearLayout
    android:background="#E9ECEB"
    android:layout_margin="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/b_menusamping2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="20dp"
            android:background="@drawable/menu_button"
            android:backgroundTint="#27D01B"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Welcome,"
            android:textSize="20sp"
            android:textStyle="italic"
            android:textColor="@android:color/black"/>
        <TextView
            android:id="@+id/t_username3"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Rizaldi"
            android:textColor="@android:color/black"
            android:textSize="25sp"
            android:textStyle="bold"
            android:fontFamily="@font/action_man_bold"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linear_refresh2"
        android:layout_margin="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white">
        <ImageView
            android:backgroundTint="#27D01B"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_margin="10dp"
            android:background="@drawable/icon_update"/>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_marginLeft="20dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lead"
        android:textColor="@android:color/black"
        android:textSize="20sp"/>
    <TextView
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="("
        android:textSize="20sp"
        android:textColor="@android:color/black"/>
    <TextView
        android:textStyle="bold"
        android:id="@+id/count_lead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100"
        android:textColor="@android:color/black"
        android:textSize="20sp"/>
    <TextView
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=")"
        android:textSize="20sp"
        android:textColor="@android:color/black"/>
    <Button
        android:id="@+id/btn_add"
        android:layout_marginRight="20dp"
        android:backgroundTint="@android:color/holo_blue_light"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tambah Lead"
        android:textAllCaps="false"/>
</LinearLayout>

<LinearLayout
    android:padding="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Tanggal"
                    android:textColor="@android:color/black"
                    android:textSize="20sp"/>
                <TextView
                    android:layout_marginLeft="30dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Lead"
                    android:textSize="20sp"
                    android:textColor="@android:color/black"/>
                <TextView
                    android:layout_marginLeft="40dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Telp"
                    android:textColor="@android:color/black"
                    android:textSize="20sp"/>
            </LinearLayout>

            <ListView
                android:id="@+id/listviewlead"
                tools:listitem="@layout/c_lead"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:layout_weight="1" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

<LinearLayout
    android:layout_marginTop="30dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/back6"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"/>
</LinearLayout>

c_lead.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/t_tgl"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Tgl"
        android:textSize="15sp"
        android:textStyle="italic"
        android:textColor="#000000" />
    <TextView
        android:id="@+id/t_lead4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Lead"
        android:textSize="15sp"
        android:textColor="#000000" />
    <TextView
        android:id="@+id/t_numberphone"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:gravity="right"
        android:text="No.Telp"
        android:textSize="15sp"
        android:textColor="@android:color/black"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <LinearLayout
        android:id="@+id/btn_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/kotak_abu"
        android:layout_marginTop="5dp">
        <ImageButton
            android:layout_margin="10dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/pencil_icon"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/btn_delete"
        android:layout_marginLeft="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/kotak_abu"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp">
        <ImageButton
            android:layout_margin="10dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/delete_icon"/>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/t_alamat"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Alamat"
        android:textColor="@android:color/black"
        android:textSize="15sp"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

MyAdapter

public View getView(final int _position, View _v, ViewGroup _container) {
        LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View _view = _v;
        if (_view == null) {
            _view = _inflater.inflate(R.layout.c_lead, null);
        }
        final HashMap<String, Object> hashMap = _data.get(_position);
        final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
        final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
        final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
        final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
        final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
        final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);

        String dateString = _data.get(_position).get("create_date").toString().replace("-","");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
        format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
        SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");

        try {
            Date date = format1.parse(dateString);
            String dateFinal = format2.format(date);
            t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        t_lead.setText(_data.get((int)_position).get("type").toString());
        t_alamat.setText(_data.get((int)_position).get("street").toString());
        if (_data.get((int)_position).get("mobile").toString().equals(true)){
            t_numberphone.setText("Tanpa Nomor");
        } else {
            t_numberphone.setText(_data.get((int)_position).get("phone").toString());
        }

        btn_edit.setFocusable(false);
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
                startActivity(i);
            }
        });

        btn_delete.setFocusable(false);
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
            }
        });

        return _view;
    }

Is there any mistake inside my program? How to solve this?

Rifaldi
  • 67
  • 6
  • // i need click button several time. Means onClickListener doesn't work? Did you try debugging? – Kamal Apr 21 '22 at 08:57
  • No, i mean onClickListener will work after several time button clicking. When i try look to logcat there no error. – Rifaldi Apr 21 '22 at 09:03
  • Try setting your parent layouts focusable as false, it seems they are taking the focus on click for the buttons, hence why your button clickListener is not firing – Smile Apr 21 '22 at 09:04
  • 1
    @Smile So i need put focusable as false in first linearlayout of c_lead.xml? CMIIW – Rifaldi Apr 21 '22 at 09:09
  • 1
    Yes in in first linearlayout of c_lead.xml. Also try `android:descendantFocusability="blocksDescendants"` – Kamal Apr 21 '22 at 09:15
  • @Kamal already tried it and give little issues, when i try click center of button (which there a image button, i wrap that inside linearlayout for set backgroundcolour), onClickListener not work – Rifaldi Apr 21 '22 at 09:39
  • 1
    You dont need to use additional layout to change background color, just add `android:background="@drawable/bg"` in ImageButton. To add image use `android:src="@drawable/ic_printer"` . To change image tint use `app:tint="@color/black"` – Kamal Apr 21 '22 at 10:33
  • @Kamal it works, thank you – Rifaldi Apr 22 '22 at 02:04

0 Answers0