问题
I'm having a hard time to figure out on what am I going to do with my code please help.
I already got the answer on how will the user set a name for the image in gridview the problem is, it constantly changes every time I click it and input new data. After adding a name and description of the image, the next click of the user the image must do other activity like viewing its content.
How will I set it as the official name of the image and use the name as a reference in the database? The name or the id of the image will be used in adding different information. How will I do it? Should I use ViewHolder? Please help?
Here is my code:
public class Holder
{
TextView tv;
ImageView img;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
final Holder holder=new Holder();
View gridV;
gridV = inflater.inflate(R.layout.grid_content, null);
holder.img=(ImageView) gridV.findViewById(R.id.homeImage);
holder.img.setImageResource(imageID[position]);
gridV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCustomDialog(position);
}
});
return gridV;
}
protected void showCustomDialog(final int position) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
holder.tv=(TextView) gridV.findViewById(R.id.homeText);
// if (homeText[position] == null)
// {
final Dialog dialog = new Dialog(Grid.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.add_land);
final EditText lname = (EditText)dialog.findViewById(R.id.land_name);
final EditText ldesc = (EditText)dialog.findViewById(R.id.land_description);
Button ok_button = (Button)dialog.findViewById(R.id.ok);
ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String land_name = lname.getText().toString();
String land_desc = ldesc.getText().toString();
//Get the text view and set its value
if (land_name.matches(""))
{
Toast.makeText(getBaseContext(), "You did not enter a name!", Toast.LENGTH_LONG).show();
}
else
{
mydb.insertLand(land_name, land_desc);
Toast.makeText(getBaseContext(), land_name+" Land Added!", Toast.LENGTH_LONG).show();
homeText[position] = land_name;
holder.tv.setText(homeText[position]);
}
dialog.dismiss();
}
});
dialog.show();
// }
// else
// {
// Toast.makeText(getBaseContext(), homeText[position], Toast.LENGTH_SHORT).show();
// }
}
}
来源:https://stackoverflow.com/questions/27971513/how-to-set-the-image-name-in-gridview-and-use-the-image-name-in-the-database-and