android - gridview 点击item的时候 SetImageResource 更换imageview失败
PHP中文网
PHP中文网 2017-04-17 17:47:57
0
1
528

如题, 我想通过点击gridview下的item而更改他的imageview。 不过点击了没有反应。 有没有大神知道怎么回事?

public class table extends AppCompatActivity implements AdapterView.OnItemClickListener{

GridView gridView;
my_adapter my_adapter;
String table_names[]={"1a","1b","1c","1d","2a","2b","2c","2d","3a","3b","3c","3d"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_table);

    gridView=(GridView)findViewById(R.id.gridview);
    my_adapter=new my_adapter(this,table_names);
    gridView.setAdapter(my_adapter);
    gridView.setOnItemClickListener(this);
}

@Override
public void onItemClick(final AdapterView<?> adapterView, final View view, final int i, long l) {

    new AlertDialog.Builder(this)
            .setTitle("Table number: "+adapterView.getItemAtPosition(i).toString())
            .setMessage("Open Table?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    my_adapter.change_img(); // 更换新的图片

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    my_adapter.change_img2(); // 更换回原来的图片
                }
            })
            .show();
}
}

class my_adapter extends BaseAdapter{

ImageView imageView;
LayoutInflater inflater=null;
Context ctx;
String table_names[];
ArrayList store_table_no;

my_adapter(Context ctx, String table_names[]){
    this.ctx=ctx;
    this.table_names=table_names;
    store_table_no=new ArrayList<Integer>();
    for (int i=0;i<table_names.length;i++){
        store_table_no.add(table_names[i]);
    }
}

@Override
public int getCount() {
    return store_table_no.size();
}

@Override
public Object getItem(int i) {
    return store_table_no.get(i);
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    View row=view;

    if(row==null){
        inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.single,null);
    }

    TextView tv_table_no=(TextView)row.findViewById(R.id.table_no);
    imageView=(ImageView)row.findViewById(R.id.imageView);
    tv_table_no.setText(""+store_table_no.get(i));

    return row;
}

public void change_img(){
    imageView.setImageResource(R.drawable.table_full);
}

public void change_img2(){
    imageView.setImageResource(R.drawable.table3d2);
}
}

single.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="101"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/table_no"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="22sp" />

<ImageView
    app:srcCompat="@drawable/table"
    android:id="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="130dp"
    android:layout_height="100dp" />
</RelativeLayout>
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
左手右手慢动作
public void change_img(){
    imageView.setImageResource(R.drawable.table_full);
}

public void change_img2(){
    imageView.setImageResource(R.drawable.table3d2);
}

Which of your two methodsimageView不是当前item对应的ImageView

The correct approach should be

@Override
public void onItemClick(final AdapterView<?> adapterView, final View view, final int i, long l) {
    // 找到被点击的item中的iamgeView
    final View imageView = view.findViewById(R.id.imageView);
    new AlertDialog.Builder(this)
            .setTitle("Table number: "+adapterView.getItemAtPosition(i).toString())
            .setMessage("Open Table?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(imageView != null && (imageView instanceOf ImageView)) {// 检查防止报错
                        ((ImageView)imageView).setImageResource(R.drawable.table_full);
                    }
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //同上
                }
            })
            .show();
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template