如何在gridview中设置图像名称并使用数据库android中的图像名称

时间:2015-01-15 19:32:03

标签: android gridview textview android-viewholder

我很难弄清楚我将如何处理我的代码,请提供帮助。

我已经得到了关于用户如何在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();
//      }
    }

}

0 个答案:

没有答案