如何在相同布局中为两个Gridview设置onclick侦听器?

时间:2019-01-17 23:26:05

标签: android listview android-gridview

我有两个自定义GridView,我想为第一个GridView设置Onclick监听器,我在其适配器内完成了它,工作正常...但是当我在第二个GridView中生成项目时,当我按on时,onclick监听器会从第二个GridView读取值第一个GridView中的项目!

如果第二个GridView为空,则onclick侦听器可以正常工作

我不知道该如何解决

holder.tvgroup1和holder.tvgroup_delete_icon的问题 谢谢

第一个GridView适配器

public class GroupGeneratedAdapter extends BaseAdapter {


ArrayList<String> result;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;

GroupGeneratedAdapter(GroupMakerG groupMakerG, ArrayList<String> UserNameinput) {
    // TODO Auto-generated constructor stub
    result = UserNameinput;
    context = groupMakerG;
    //     imageId=prgmImages;
    inflater = (LayoutInflater) context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder {
    TextView tvgroup, tvgroup1;
    ImageView tvgroup_delete_icon;
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder = new Holder();
    //  View rowView;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.group_generated_original, parent, false);
    }
    holder.tvgroup = (TextView) convertView.findViewById(R.id.tvgroup);
    holder.tvgroup1 = (TextView) convertView.findViewById(R.id.tvgroup1);
    holder.tvgroup_delete_icon = (ImageView) convertView.findViewById(R.id.tvgroup_delete_icon);

    //    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tvgroup.setText(String.valueOf(position + 1) + "-");
    holder.tvgroup1.setText(result.get(position));

    //   holder.img.setImageResource(imageId[position]);





    holder.tvgroup_delete_icon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GroupMakerG.GroupOfNames.remove(position);
            GroupMakerG.MainGroup.invalidateViews();
            if (GroupMakerG.arraySugestion.size() > 0) {
                GroupMakerG.arraySugestion.clear();
                GroupMakerG.makeSuggestions();

            } else {
               GroupMakerG.makeSuggestions();
            }

            GroupMakerG.GeneratedGroup.setAdapter(null);
            GroupMakerG.No_Of_Groups.setText("");
            GroupMakerG.numoftries = 0;
            GroupMakerG.Group_num_tries.setText("0");
            GroupMakerG.GroupNameLength.setText(String.valueOf(GroupMakerG.GroupOfNames.size()));

        }
    });


    holder.tvgroup1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {


            AlertDialog.Builder builder = new AlertDialog.Builder(context );
            builder.setTitle("Modify Item No." + " " + (position + 1));
            // I'm using fragment here so I'm using getView() to provide ViewGroup
            // but you can provide here any other instance of ViewGroup from your Fragment / Activity
            View viewInflated = LayoutInflater.from(context).inflate(R.layout.modify_name_mainlist, (ViewGroup) parent.findViewById(android.R.id.content), false);
            // Set up the input
            final EditText input = (EditText) viewInflated.findViewById(R.id.client_name_input);
            input.setText(GroupMakerG.GroupOfNames.get(position));
            // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
            builder.setView(viewInflated);

            // Set up the buttons
            builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();


                    String m_Text = input.getText().toString();

                    if (m_Text.trim().equals("")) {
                        //   Toast.makeText(context, "You should enter name", Toast.LENGTH_SHORT).show();

                        Toast toast = Toast.makeText(context,context.getString(R.string.group_you_should_enter_name), Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();


                    } else {


                        GroupMakerG.GroupOfNames.set(position, m_Text);
                        GroupMakerG.MainGroup.invalidateViews();
                        GroupMakerG.GeneratedGroup.setAdapter(null);
                        GroupMakerG.numoftries = 0;
                        GroupMakerG.Group_num_tries.setText("0");
                        Toast toast = Toast.makeText(context, context.getString(R.string.group_item_changed), Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();


                    }

                }
            });
            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();

            return false;

        }
    });



    convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked "+ result.get(position), Toast.LENGTH_LONG).show();
        }
    });


    return convertView;
}

}

第二个GridView适配器

public class GroupGeneratedAdapterG extends BaseAdapter {
private ArrayList<String> resultg;
int[] imageId;
private static LayoutInflater inflaterg = null;
private static int generated_group_counter;
Context contextg;

GroupGeneratedAdapterG(GroupMakerG groupMaker, ArrayList<String> UserNameoutput) {
    // TODO Auto-generated constructor stub
    resultg = UserNameoutput;
  contextg = groupMaker;
    //     imageId=prgmImages;
    inflaterg = (LayoutInflater) contextg.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    generated_group_counter = 0;
}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return resultg.size();
}

@Override
public Object getItem(int positiong) {
    // TODO Auto-generated method stub
    return positiong;

}

@Override
public long getItemId(int positiong) {
    // TODO Auto-generated method stub
    return positiong;
}

public class Holder {
    TextView tvgroupg, tvgroup1g;
    //    ImageView img;
}

@Override
public View getView(final int positiong, View convertViewg, ViewGroup parentg) {
    // TODO Auto-generated method stub
    generated_group_counter = generated_group_counter + 1;
    Holder holderg = new Holder();
    //  View rowView;
    if (convertViewg == null) {
        convertViewg = inflaterg.inflate(R.layout.group_generated_generated, parentg, false);
    }

    holderg.tvgroupg = (TextView) convertViewg.findViewById(R.id.tvgroupg);
    holderg.tvgroup1g = (TextView) convertViewg.findViewById(R.id.tvgroup1g);

    //    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);


    String[] ary = resultg.get(positiong).split("!@#%");


    if (ary[1].length() > 4) {

        if (ary[1].substring(0, 5).equals("Group")) {

            holderg.tvgroupg.setText("");
            holderg.tvgroup1g.setText(ary[1]);


        } else {

            holderg.tvgroupg.setText(ary[0]);
            holderg.tvgroup1g.setText(ary[1]);

        }
    } else {


        holderg.tvgroupg.setText(ary[0]);
        holderg.tvgroup1g.setText(ary[1]);
    }


    convertViewg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
   //          Toast.makeText(contextg, "You Clicked "+ String.valueOf(resultg.get(positiong)), Toast.LENGTH_LONG).show();
        }
    });

    return convertViewg;
}

}

0 个答案:

没有答案