从onClick(View v)中的onClick(View v)设置TextViews?

时间:2013-01-27 04:40:14

标签: android android-listview adapter

此行不会设置holder.t1.setText(NewItem);

如果我将它移动到父onClick,并使用硬编码字符串(用于测试),它会。

请注意,这是ArrayAdapter的getView方法。我正在尝试将文本设置为ListView行。

编辑:

EXPANDED,COM​​PLETE getView() - AS REQUESTED (没时间编辑,稍后会抱歉!)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.commentlayout, parent,
                false);
        holder = new ViewHolder();
        holder.t1 = (TextView) convertView.findViewById(R.id.labelComment);
        holder.t2 = (TextView) convertView.findViewById(R.id.labelDate);
        holder.t3 = (TextView) convertView.findViewById(R.id.labelUser);
        holder.t3.setTypeface(tf);
        holder.t4 = (TextView) convertView
                .findViewById(R.id.labelHelpfulCount);
        holder.t5 = (TextView) convertView
                .findViewById(R.id.labelCommentCount);
        holder.ib1 = (ImageView) convertView
                .findViewById(R.id.labelChatIcon);
        holder.ib2 = (ImageView) convertView
                .findViewById(R.id.labelCommentFlag);
        holder.rb1 = (RatingBar) convertView
                .findViewById(R.id.myCommentsRatingBarSmall);
        holder.b1 = (Button) convertView.findViewById(R.id.bReview1);
        holder.b2 = (Button) convertView.findViewById(R.id.bReview2);
        holder.b3 = (Button) convertView.findViewById(R.id.bReview3);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        convertView.setOnCreateContextMenuListener(null);
    }

    ReviewObject ro = getItem(position);
    final String item = ro.item;
    final String review = ro.review;
    final String username = ro.username;
    Long date = Long.valueOf(ro.date);
    String rating = ro.ratings;
    String voteCount = ro.voteCount;
    String chatcount = ro.chatCount;
    String cat = ro.cat;

    final ArrayList<String> passing = new ArrayList<String>();
    passing.add(item);
    passing.add(review);
    passing.add(cat);
    passing.add(username);

    String time = "";
    time = DateConvert.dateConvert(Long.valueOf(date));

    holder.t1.setText(review);
    holder.t2.setText(time);
    holder.t3.setText(username);
    holder.t4.setText(voteCount);
    holder.t5.setText(chatcount);
    holder.ib1.setImageResource(R.drawable.updown);
    holder.ib2.setImageResource(R.drawable.comment);
    holder.rb1.setRating(Float.valueOf(rating));

    if (rating.equals("0")) {
        holder.rb1.setEnabled(false);
    }

    holder.b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            String ReviewUser = holder.t3.getText().toString();
            String ReviewWords = holder.t1.getText().toString();

            Intent intent = new Intent(getContext(), Comments.class);
            intent.putExtra("comment", ReviewWords);
            intent.putExtra("user", ReviewUser);
            intent.putExtra("item", item);
            getContext().startActivity(intent);

        }
    });

    if (!Rateit.username.equals(username)) {

        holder.b2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                AlertDialog.Builder alertbox = new AlertDialog.Builder(getContext());
                alertbox.setMessage("Did you like this?");
                alertbox.setNegativeButton("Vote Up",
                        new DialogInterface.OnClickListener() {

                            @SuppressWarnings("unchecked")
                            public void onClick(DialogInterface arg0,
                                    int arg1) {

                                String VoteTally = holder.t4.getText()
                                        .toString();
                                int ReviewCountInt = Integer
                                        .valueOf(VoteTally) + 1;
                                VoteTally = String.valueOf(ReviewCountInt);
                                holder.t4.setText(VoteTally);

                                new HelpfulTask().execute(passing);
                            }
                        });

                alertbox.setPositiveButton("Vote Down",
                        new DialogInterface.OnClickListener() {

                            @SuppressWarnings("unchecked")
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                String VoteTally = holder.t4.getText()
                                        .toString();
                                int ReviewCountInt = Integer
                                        .valueOf(VoteTally) - 1;
                                VoteTally = String.valueOf(ReviewCountInt);
                                holder.t4.setText(VoteTally);

                                new UnHelpfulTask().execute(passing);

                            }
                        });

                alertbox.setNeutralButton("Report Spam",
                        new DialogInterface.OnClickListener() {

                            @SuppressWarnings("unchecked")
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                new SpamTask().execute(passing);

                            }
                        });

                alertbox.show();

            }

        });

    } else {
        holder.b2.setText("Edit");
        holder.b2.setOnClickListener(new OnClickListener() {



            public void onClick(View v) {

                final Dialog dialog = new Dialog(getContext());
                dialog.setContentView(R.layout.editreview);
                dialog.setTitle("Edit Review");
                dialog.show();

                final EditText etEdit = (EditText) dialog
                        .findViewById(R.id.etEditReview);

                etEdit.setText(review);

                Button bInsert = (Button) dialog.findViewById(R.id.bInsert);
                bInsert.setOnClickListener(new OnClickListener() {

                    @SuppressWarnings("unchecked")
                    public void onClick(View v) {


                        NewItem = etEdit.getText().toString();


                        if (NewItem.equals("")) {

                            Toast.makeText(getContext(),
                                    "Please add something first.",
                                    Toast.LENGTH_SHORT).show();

                        } else {

                            holder.t1.setText(NewItem);
                            passing.add(NewItem);

                            dialog.dismiss();

                            new EditCommentTask().execute(passing);


                        }

                    }
                });

            }
        });

    }

    if (!Rateit.username.equals(username)) {

        holder.b3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(getContext(), OtherProfile.class);
                i.putExtra("userprofile", username);
                getContext().startActivity(i);

            }
        });

    } else {
        holder.b3.setText("Delete");
        holder.b3.setOnClickListener(new OnClickListener() {
            @SuppressWarnings("unchecked")
            public void onClick(View v) {

                AlertDialog.Builder alertbox = new AlertDialog.Builder(
                        getContext());
                alertbox.setMessage("Are you sure you want to delete your review?");
                alertbox.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0,
                                    int arg1) {

                            }
                        });

                alertbox.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {

                                new DeleteReviewTask().execute(passing);


                            }
                        });

                alertbox.show();

            }
        });

    }

    return convertView;
}

1 个答案:

答案 0 :(得分:0)

根据您在上述评论中所说的内容,这是预期的功能。从那以后,

  

除了,对话框确实消失,我得到了祝酒,任务正确运行。

这是在你的onClick()编码:

bInsert.setOnClickListener(new OnClickListener() {

                @SuppressWarnings("unchecked")
                public void onClick(View v) {

                    NewItem = etEdit.getText().toString();

                    if (NewItem.equals("")) {

                        Toast.makeText(getContext(),
                                "Please add something first.",
                                Toast.LENGTH_SHORT).show();
                    } else {

                        holder.t1.setText(NewItem);
                        passing.add(NewItem);

                        dialog.dismiss();

                        new EditCommentTask().execute(passing);
                    }
                }
            });

具体来说,这是在执行:

 if (NewItem.equals("")) {
   Toast.makeText(getContext(),
      "Please add something first.",
      Toast.LENGTH_SHORT).show();
 }

所以我假设你的问题是NewItem,我从来没有看到你实际初始化它的地方,但我假设它.equals(""),因为这是执行。尝试在Log.d行下方println()NewItem = etEdit.getText().toString();投放,以查看此时NewItem的值是多少。