Reputation: 160
I define this layout:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Content_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="44dp"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_pencil" /> </androidx.constraintlayout.widget.ConstraintLayout>
I want to add 3 items of this layout to a list view. They are displayed fine but when I click on each item. The listener doesn't seem to be invoked.
Here's the implementation inside main activity and the class "item_adapter" which extends ArrayAdapter:
Main_activity:
package com.example.selectedlist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
ListView list;
int selected;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arr = new ArrayList<>();
arr.add("ABC");
arr.add("MGH");
arr.add("EFG");
selected = 0;
list = findViewById(R.id.my_list);
item_adapter adapter = new item_adapter(this, arr);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView txt = (TextView)view.findViewById(R.id.Content_txt);
txt.setText("dasd");
}
});
}
}
item_adapter:
package com.example.selectedlist;
import android.animation.LayoutTransition; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.Nullable;
import java.util.ArrayList; import java.util.List;
public class item_adapter extends ArrayAdapter<String> {
Context main_context;
List<String> data;
public item_adapter(@NonNull Context context, @NonNull List<String> objects)
{
super(context, R.layout.edit_layout, objects);
main_context = context;
data = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
String str = getItem(position);
View view = LayoutInflater.from(main_context).inflate(R.layout.edit_layout, parent, false);
TextView txt = view.findViewById(R.id.Content_txt);
txt.setText(str);
return view;
} }
Nothing happens when I click on each item. The TextView in the layout is supposed to be changed as shown in the code. Any ideas on this ?
Upvotes: 0
Views: 95
Reputation: 160
Simply use the method setFocusable of the ImageButton for the listener to work.
ImageButton Edit_btn = view.findViewById(R.id.imageButton);
Edit_btn.setFocusable(false);
Upvotes: 0
Reputation: 292
Please use below code
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView txt = (TextView)view.findViewById(R.id.Content_txt);
txt.setText("dasd");
}
});
Upvotes: 2