2

I try to use ListView with two ImageVies. First is some photo and second delete photo button.

I try to handle wich item I click but always get same id that different from both of ImageViews. I also try to do this in Adapter class, but found that is not whery nice solution.

May be you can say, where am I wrong?

List update/init part

public void updateList(final ArrayList<String> arrayList) {
        CustomList adapter = new
                CustomList(FullInfoActivity.this, arrayList);
        HorizontalListView list = (HorizontalListView) findViewById(R.id.HorizontalListView);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
               Log.e("Parent", String.valueOf(parent.getId()) +" " +R.id.img + " "+R.id.image_delete + " "+parent.getItemAtPosition(position));
                switch (parent.getId()) {
                    case R.id.img:
                        Toast.makeText(getApplicationContext(), "111111111", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.image_delete:
                        Toast.makeText(getApplicationContext(), "222222222", Toast.LENGTH_SHORT).show();
                        break;
                }

ListView item

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" >
    <ImageView
        android:id="@+id/img"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:scaleType="centerCrop"
        android:visibility="invisible"
        android:layout_marginRight="8dp"
        android:clickable="true"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:contentDescription="image" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/img"
        android:layout_alignStart="@+id/img"
        android:layout_alignRight="@+id/img"
        android:layout_alignEnd="@+id/img"
        android:visibility="visible" />

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/image_delete"
        android:src="@drawable/ic_action_cancel_light"
        android:layout_alignTop="@+id/img"
        android:layout_alignRight="@+id/img"
        android:layout_alignEnd="@+id/img"
        android:background="#7f989e9a"
        android:clickable="true"
        android:contentDescription="Cancel" />

</RelativeLayout>

Custom List Adapter

public class CustomList extends ArrayAdapter<String> {
    private final Activity context;
    private final ArrayList<String> arrayList;

    public CustomList(Activity context, ArrayList<String> arrayList) {
        super(context, R.layout.list_single, arrayList);
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);

        final ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        final ProgressBar pb = (ProgressBar) rowView.findViewById(R.id.progressBar);
        final Animation show = AnimationUtils.loadAnimation(context, R.anim.appear_animation);
        ImageView delete = (ImageView) rowView.findViewById(R.id.image_delete);
            final Timer myTimer = new Timer(); // Создаем таймер
        myTimer.schedule(new TimerTask() { // Определяем задачу
                @Override
                public void run() {
                    File f = new File(arrayList.get(position));
                    if (f.exists()) {
                        final Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
                        if (myBitmap != null) {
                            myTimer.cancel();
                            context.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    pb.setVisibility(View.GONE);
                                    imageView.setImageBitmap(Bitmap.createScaledBitmap(myBitmap, 120, 120, false));
                                    imageView.setVisibility(View.VISIBLE);
                                    imageView.startAnimation(show);
                                }
                            });
                        }
                    }
                }
            }, 100, 100);
        return rowView;
    }

}
Anton A.
  • 1,718
  • 15
  • 37
  • you will need to set onClick listener inside `CustomList` for both button because `onItemClick` method fire only when ListView row clicked instead of any child view click from row. example [Multi Touch List View : Multi Click List View Demo in Android : Click button in List View in Android](http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html) – ρяσѕρєя K Sep 28 '14 at 17:56
  • Just as a note please reuse your views that is important for the performance. – rekire Sep 28 '14 at 17:57
  • @ρяσѕρєяK I cant, becauae then I cant update listview in my activity. Or I dont now how to do this. And with setonclick listview become not scrollable – Anton A. Sep 28 '14 at 17:58
  • What you are getting in this String.valueOf(parent.getId()).? – Chitrang Sep 28 '14 at 18:15

1 Answers1

1

get same id that different from both of ImageViews.

because you got row id view and it is different from their children's views. so in your getView use below code:

ImageView delete = (ImageView) rowView.findViewById(R.id.image_delete);
delete.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // do your work
    }
});
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • I do this but found that I cant call method updateList that is in my activity. – Anton A. Sep 28 '14 at 18:10
  • why do you want to call that method? – mmlooloo Sep 28 '14 at 18:12
  • Also whole ListView become not dragable – Anton A. Sep 28 '14 at 18:12
  • for updating listView you do not need create adapter again simply change the adapter data and call `notifyDataSetChanged` for your `dragable` you must ask another question because the button I think prevent the touch event but i am sure it has solution, at the end i think have to take my approach. – mmlooloo Sep 28 '14 at 18:18
  • Sorry, I mean not dragable but scrollable. Without onclick it is scroll normal, but in other case it is scroll only in palces without ImageView – Anton A. Sep 28 '14 at 18:21
  • ok I am sure you made a mistake because i have created a listview with a button to delete each row. can you elaborate "Without onclick it is scroll normal, but in other case it is scroll only in palces without ImageView" – mmlooloo Sep 28 '14 at 18:24
  • When I scroll listview without onclcik on image i can scroll anywhere. But whrn I add oncklick and try scroll same listview just dont want to scroll. But if I try to scroll it in place without image it is sctolls like normal. – Anton A. Sep 28 '14 at 18:37
  • 1
    try adding `android:descendantFocusability="blocksDescendants"` to your listView and `android:focusable="false"` to your `imageView` – mmlooloo Sep 28 '14 at 18:47
  • Still have scroll problems( – Anton A. Sep 29 '14 at 16:41
  • sorry I do not know you can reject my answer(the tick beside it) to see if other can help you! – mmlooloo Sep 29 '14 at 18:31