0

我很难弄清楚我要如何处理我的代码,请帮忙。

我已经得到了关于用户如何在 gridview 中为图像设置名称的答案,问题是,每次单击它并输入新数据时它都会不断变化。添加图像的名称和描述后,用户下次单击图像必须执行其他活动,例如查看其内容。

如何将其设置为图像的正式名称并在数据库中使用该名称作为参考?图像的名称或 id 将用于添加不同的信息。我将如何做?我应该使用 ViewHolder 吗?请帮忙?

这是我的代码:

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

}
4

0 回答 0