I am showing captured images in one layout using different gridView for each category (Shirts , Trousers e.t.c). The number of the gridViews is 6.
I am trying to select one image from each gridView and via onClick method in the button to show them in another activity but the code i wrote doesn't work.
I suppose something is wrong with the strings.
Can anyone help me to fix this? Thnak you in advance!
public class CreateOutfit extends Activity implements View.OnClickListener {
private DatabaseHandler handler;
GridView grid1;
GridView grid2;
ArrayList<Clothes> clothes = new ArrayList<Clothes>();
GridviewAdapter GridAdapter;
String image1;
String image2;
public static final String ITEM_IMAGE1 = "image1";
public static final String ITEM_IMAGE2 = "image2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clothes_grids);
handler = new DatabaseHandler(getApplicationContext());
grid1 = (GridView) findViewById(R.id.grid1);
grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
image1 = String.valueOf(view.findViewById(R.id.cloth_image_grid).getContext().toString());
}
});
fillGrid1();
grid2 = (GridView) findViewById(R.id.grid2);
grid2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
image2 = String.valueOf(view.findViewById(R.id.cloth_image_grid));
}
});
fillGrid2();
ImageButton btn_in = (ImageButton)findViewById(R.id.btn_insert);
btn_in.setOnClickListener(this);
}
public void fillGrid1() {
clothes = (ArrayList<Clothes>) handler.readGridShirts();
GridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
grid1.setAdapter(GridAdapter);
for (Clothes c : clothes) {
String record = "ID=" + c.getID() + " | Category=" + c.getCategory() + " | " + c.getSize();
Log.d("Record", record);
}
}
public void fillGrid2() {
clothes = (ArrayList<Clothes>) handler.readGridTrousers();
GridAdapter = new GridviewAdapter(this, R.layout.grid_row, clothes);
grid2.setAdapter(GridAdapter);
for (Clothes c : clothes) {
String record = "ID=" + c.getID() + " | Category=" + c.getCategory() + " | " + c.getSize();
Log.d("Record", record);
}
}
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(), ViewOutfit.class);
in.putExtra(ITEM_IMAGE1 , image1);
in.putExtra(ITEM_IMAGE2, image1);
startActivity(in);
}
}
public class ViewOutfit extends Activity {
DatabaseHandler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_outfit);
Intent i = getIntent();
handler = new DatabaseHandler(getApplicationContext());
ImageView im1 = (ImageView) findViewById(R.id.im1);
String image1 = i.getStringExtra("image1");
im1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra(image1)));
ImageView im2 = (ImageView) findViewById(R.id.im2);
String image2 = i.getStringExtra("image2");
im2.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra(image2)));
}
The main problem here is: the
string
's values which are not getting the image path.According to this line:
It'll never return the image's path... Did you try to display this value in Logcat? It should display the memory's address of the widget's
ImageView
, and not the image's path.You should override
Object getItem(int position)
inGridView
's adapter, get the item's model (known asClothes
) and retrieve its image's path. Thus, the adapter should has this:Then, in the class, when you set the listener, it should be as this:
Beside, you saw
adapterTShirt
in the above code, because you used the same variable of the adapterGridAdapter
and you set a new instance of the adapter on this line for bothGridView
s:This might be better to not re-initialize the adapter and display the same for two gridviews. You'd have two variables (two instances) of the adapter as:
With this, you will have two separate instances and you can't retrieve the image 's path in click listener from the right clicked item like my snippnet code above.