0

比方说,我有两个文本视图,TextViewAB.

  • 当我单击时A,我需要更改两个文本视图;
  • 当我点击 时B,我需要它来做其他事情。

我使用了setTag(key,A);setTag(key,B);textViewA更改正确,但 textViewBnull. 当我点击B它的作品。

这是我的代码:

public class CustomeAdapterHowComment extends ArrayAdapter<ItemsHowComment> {
Context context;
ItemsHowComment items;

List<LikersComment> likeItems;

List<ItemsHowComment> Items1;

Bitmap bitmap;
String imageUser = "";
String filePath_Image = "/Pictures/jehad/joj/";

String user_id = "1510";
String secret_id = "789654120";
View v;
String onclick = " ";

int x;
String table = "10";
String target = "";
String type = "";
DataBaseHandler dbobj;
public static String server_List_of_Comments = "https://www.ashabe.comt";
public static String server_Comment_like = "https://www.ashabe.com/";
public static String server_Comment_like_remove = "https://www.ashabe.com/";
public static String server_Comment_Delte = "https://www.ashabe.com/";
public static String server_Comment_likers = "https://www.ashabe.com/";
String response;

public CustomeAdapterHowComment(Context context, int textViewResourceId,
        List<ItemsHowComment> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.context = context;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    Log.d("View    ", "View>>>>>>>>");
    commentholder h = null;
    v = convertView;

    items = getItem(position);

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

    if (convertView == null) {
        v = vi.inflate(R.layout.how_comment_list, null);
        h = new commentholder();
        h.img = (ImageView) v.findViewById(R.id.CommentImg);
        h.name = (TextView) v.findViewById(R.id.CommentName);
        h.commentBody = (TextView) v.findViewById(R.id.CommentBody);

        h.like = (TextView) v.findViewById(R.id.CommentLike);

        h.numoflike = (TextView) v.findViewById(R.id.CommentNumOfLike);

        v.setTag(h);
    } else {
        h = (commentholder) v.getTag();
    }

    imageUser = items.getUserId() + items.getRand();
    boolean flag_ImagePath = Methods.checkIfImage_DirExists(filePath_Image
            + imageUser);
    if (flag_ImagePath == true) {
        Log.v("flag_ImagePath", String.valueOf(flag_ImagePath));
        bitmap = Methods.displayBitmapImage(imageUser);
        h.img.setImageBitmap(bitmap);
    }

    h.commentBody.setText(items.getCommentBody());
    h.name.setText(items.getName());
    if (items.getiLike().equals("0")) {
        h.like.setText("Like");
    }
    if (items.getiLike().equals("1")) {
        h.like.setText("Unlike");
    }

    if (items.getNumOfLike().equals("0")) {
        h.numoflike.setText("0");
    } else {
        h.numoflike.setText(items.getNumOfLike());
    }

    h.name.setOnClickListener(listener);
    h.name.setTag(R.id.CommentName, h.name);
    h.name.setTag(position);

    h.numoflike.setOnClickListener(listener);
    h.numoflike.setTag(R.id.CommentNumOfLike, h.numoflike);
    h.numoflike.setTag(position);

    h.like.setOnClickListener(listener);
    h.like.setTag(R.id.CommentLike, h.like);
    h.like.setTag(position);

    h.img.setOnClickListener(listener);
    h.img.setTag(R.id.CommentImg, h.img);
    h.img.setTag(position);

    return v;

}

private OnClickListener listener = new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View view) {

        final int id = view.getId();
        type = " ";
        CommentEvents t = new CommentEvents();
        t.execute();

        switch (id) {

        case R.id.CommentImg:
            dbobj = new DataBaseHandler(getContext());
            Items1 = dbobj.select_HowComment();
            dbobj.CloseDataBase();
            Collections.reverse(Items1);
            x = (Integer) view.getTag();

            Log.v("value", x + Items1.get(x).getName());
            Toast.makeText(getContext(), Items1.get(x).getName(),
                    Toast.LENGTH_SHORT).show();

            break;
        case R.id.CommentName:
            // ///profile

            try {
                dbobj = new DataBaseHandler(getContext());
                Items1 = dbobj.select_HowComment();
                dbobj.CloseDataBase();
                Collections.reverse(Items1);
                x = (Integer) view.getTag();
                TextView tv = (TextView) view.getTag(R.id.CommentName);

                Log.v("value", x + Items1.get(x).getName());
                Toast.makeText(getContext(), Items1.get(x).getName(),
                        Toast.LENGTH_SHORT).show();

                // String user = Items1.get(x).getUserId();
            } catch (IndexOutOfBoundsException e) {
                // TODO: handle exception
            }
            break;

        case R.id.CommentLike:

            dbobj = new DataBaseHandler(getContext());
            Items1 = dbobj.select_HowComment();
            dbobj.CloseDataBase();
            Collections.reverse(Items1);
            x = (Integer) view.getTag();
            Log.v("value", x + "");
            TextView like = (TextView) view.getTag(R.id.CommentLike);

            target = Items1.get(x).getCommentId().toString();

            type = "like";
            CommentEvents s = new CommentEvents();
            s.execute(user_id, secret_id, table, target);

            if (Items1.get(x).getiLike().equals("0")) {

                like.setText("Unlike");
                onclick = "true";
            } else if (Items1.get(x).getiLike().equals("1")) {

                like.setText("Like");
                onclick = "true";
            }

        case R.id.CommentNumOfLike:
            // ////howlike

            if (onclick.equals("true")) {
                x = (Integer) view.getTag();
                TextView numofLike = (TextView) view
                        .getTag(R.id.CommentNumOfLike);
                if (numofLike != null) {
                    if (Items1.get(x).getiLike().equals("0")) {

                        Log.e("getNumOfLike____unlike", Items1.get(x)
                                .getNumOfLike());
                        int num = Integer.parseInt(Items1.get(x)
                                .getNumOfLike().toString());
                        num = num + 1;

                        Log.e("numberunlike", num + "");
                        numofLike.setText(num + "");
                    } else if (Items1.get(x).getiLike().equals("1")) {

                        Log.e("getNumOfLike_______like", Items1.get(x)
                                .getNumOfLike());
                        int num = Integer.parseInt(Items1.get(x)
                                .getNumOfLike().toString());
                        num = num - 1;
                        Log.e("numberlike", num + "");
                        numofLike.setText(num + "");

                    }

                    onclick=" ";    }
            } else {

                try {
                    dbobj = new DataBaseHandler(getContext());
                    Items1 = dbobj.select_HowComment();
                    dbobj.CloseDataBase();
                    Collections.reverse(Items1);
                    x = (Integer) view.getTag();
                    Log.v("value", x + "");
                    target = Items1.get(x).getCommentId().toString();

                    type = "numoflike";
                    CommentEvents r = new CommentEvents();
                    r.execute(user_id, secret_id, table, target);

                } catch (IndexOutOfBoundsException e) {
                    // TODO: handle exception

                }

            }
        }

    }

};
4

1 回答 1

0

利用

     View view = v.getParent();

然后尝试使用viewin初始化视图onClick

您可以设置标签

  h.name.setOnClickListener(listener);
  h.name.setTag(h.numoflike); // use setTag only once
  h.numoflike.setOnClickListener(listener); 
  h.numoflike.setTag(h.name); // use setTag only once

然后点击

   public void onClick(View v) {
            switch(v.getId())
            {
            case R.id.CommentName :
                TextView tv1 = (Button) v.getTag();
                TextView tv2 = (TextView) v;
                tv1.setText("clicked");
                tv2.setText("clicked");
                break;
            case R.id.CommentNumOfLike :
                 //do something
                break;  
            }

        }

或者在 onClick 初始化你的视图

       View view = v.getParent();
       TextView tv = view.findVIewById(R.id.CommentName);
       TextView tv1 = view.findVIewById(R.id.CommentNumOfLike);
       tv.setText("clicked");
       tv1.setText("clicked"); 
于 2013-09-17T14:18:44.360 回答