获取错误的onItemClick

获取错误的onItemClick

我尝试将ListView与两个ImageVies一起使用。首先是一些照片,然后是删除照片按钮。

我尝试处理我单击的项目,但总是获得与两个ImageView不同的相同ID。
我也尝试在Adapter类中执行此操作,但是发现这不是很好的解决方案。

也许你可以说,我哪里错了?

列表更新/初始化部分

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项目

<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>


自定义列表适配器

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;
    }

}

最佳答案

获得与两个ImageView不同的相同ID。


因为您获得了行ID视图,并且该视图与他们的孩子的视图不同。因此在您的getView中使用以下代码:

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

    @Override
    public void onClick(View arg0) {
        // do your work
    }
});

关于android - 获取错误的onItemClick ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26087746/

10-14 17:27