gosulove gosulove - 1 month ago 8
Android Question

Android SetImageResource failed to work in Gridview on button click

I try to display some items in a Gridview and set button click listener. When the item was clicked, it shows a dialog box, if clicking yes, it will change the Imageview.

java coding

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(); // change to new image

}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
my_adapter.change_img2(); // change back to original image
}
})
.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>


Here is a screenshot, please have a look

enter image description here

It simply doesn't change the imageview. Looks like something is wrong with my coding. anyone knows what's happening ?

Answer

You are implementing GridView's OnItemClickListener to show a dialog and getting decision from user for each item. When you are showing up a dialog it needs to update current clicked item of grid. But in your case you have change_img() in adapter. No position specification is given to the adapter.

To make it work, move your logic from activity to adapter. Handle the click events with the positions.

In your activity,

public class table extends AppCompatActivity{

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_grid);

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


class my_adapter extends BaseAdapter {

    LayoutInflater inflater = null;
    Context ctx;
    String table_names[];
    ArrayList<String> store_table_no;

    my_adapter(Context ctx, String table_names[]) {
        this.ctx = ctx;
        this.table_names = table_names;
        store_table_no = new ArrayList<>();
        store_table_no.addAll(Arrays.asList(table_names));
    }

    @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(final 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);
        }

        RelativeLayout layout = (RelativeLayout) row.findViewById(R.id.relative_layout);
        TextView tv_table_no = (TextView) row.findViewById(R.id.table_no);
        final ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
        tv_table_no.setText(store_table_no.get(i));


        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(GridActivity.this)
                        .setTitle("Table number: " + store_table_no.get(i))
                        .setMessage("Open Table?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                imageView.setImageResource(R.mipmap.ic_launcher);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                imageView.setImageResource(R.drawable.icon);

                            }
                        })
                        .show();
            }
        });
        return row;
    }

}
}

In your xml file add id to Parent RElative Layout as follows,

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:id="@+id/relative_layout"
android:clickable="true"
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>

I hope it will help you.

Comments