0

I made a list view with image and text view inside each view. what I'm trying to do is to make toast with the text from the text view every time I press on the image, and not when I press the rest of the view. I tried to use imageClick(View view) function that works when I press an image but I didn't find a way to get the text from the text view. another thing I tried was the onItemClick() function, so i can get the position of the view and get the text from the text view, but I can't check if the image was pressed or not.

anyway here's my code:

public class MainActivity extends AppCompatActivity {
    int[] images = {R.drawable.pic_a, R.drawable.pic_b, R.drawable.pic_c,R.drawable.pic_d };
    String[] names = {"aa","bb","cc","dd"};
    String[] descriptions = {"1a","2a","3a","4a"};
    List<ListViewItem> list = new ArrayList<>();
    int lastPosition = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        populateList();
        ListView listView = findViewById(R.id.listView);
        CustomAdapter customAdapter = new CustomAdapter(this,list);
        listView.setAdapter(customAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                toast(names[position]);

            }
        });
    }
    public void imageClick(View view) {

    }
    private void populateList() {
        int size = images.length;
        for(int i = 0;i<size;i++){
            ListViewItem item = new ListViewItem();
            item.imageSource = images[i];
            item.name = names[i];
            item.description = descriptions[i];
            list.add(item);
        }
    }
}

my listViewItem:

public class ListViewItem {
    int imageSource;
    String name;
    String description;
}

and the XML:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

the second XML file:

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="30dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="20dp"
    android:id="@+id/imageView" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    android:id="@+id/textView_name" />

<TextView
    android:text="TextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView"
    android:layout_alignLeft="@+id/textView_name"
    android:layout_alignStart="@+id/textView_name"
    android:id="@+id/textView_description" />

and my CustomAdapter:

class CustomAdapter extends BaseAdapter {
    private final Activity activity;
    List<ListViewItem> list;
    public CustomAdapter(Activity activity, List<ListViewItem> list){
        super();
        this.list = list;
        this.activity = activity;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = this.activity.getLayoutInflater().inflate(R.layout.customlayout, null);
        ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
        TextView textView_name = (TextView)view.findViewById(R.id.textView_name);
        TextView textView_description = (TextView)view.findViewById(R.id.textView_description);
        ListViewItem item = this.list.get(i);
        imageView.setImageResource(item.imageSource);
        textView_name.setText(item.name);
        return view;
    }
}
Hila Grossbard
  • 521
  • 7
  • 20

0 Answers0