0

单击它后,我必须更改 textView。我尝试通过 android:onclick 执行此操作,但它不起作用,然后我尝试执行此操作,但仍然出现错误。

这是我的适配器代码:

package howcomment;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.logintest.R;

import dataBase.DataBaseHandler;
import functions.Methods;


  public class CustomeAdapterHowComment extends ArrayAdapter<ItemsHowComment>{
Context context;
ItemsHowComment items;
List<ItemsHowComment> Items1;
Bitmap bitmap;
String imageUser = "";
String filePath_Image = "/Pictures/jehad/joj/";

String user_id = "1002";
String secret_id = "2143054018";
int x;
String table = "10";
String target = "";

DataBaseHandler dbobj;
public static String server_List_of_Comments = "https://www.ashabe.com/";
public static String server_Comment_like = "https://www.ashabe.com";
public static String server_Comment_like_remove = "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;
     View 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("");
    }else{
        h.numoflike.setText(items.getNumOfLike());
    }

这是 textview onclick 它工作正常,但 onItemclick dosnt 工作

        v.setTag(R.id.CommentLike, h.like);
    h.like.setTag(position);
     v.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

                 TextView like =(TextView)v.getTag(R.id.CommentLike);
                 TextView numoflike =(TextView)v.getTag(R.id.CommentNumOfLike);
                x =(Integer)like.getTag();
            //   Toast.makeText(getContext(), "like"+like.getTag(position).toString(), Toast.LENGTH_SHORT).show();  


                dbobj = new DataBaseHandler(getContext()); 
                Items1=dbobj.select_Howliketest();
                dbobj.CloseDataBase();
                Collections.reverse(Items1);
                 target=Items1.get(x).getCommentId().toString();

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

                if(Items1.get(x).getiLike().equals("0")){
                    like.setText("Unlike");
                }else if  (Items1.get(x).getiLike().equals("1")) {
                    like.setText("Like");   
                } 

             }

         });


        return v;
        }

这是我的 AsyncTask

    class CommentEvents extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        dbobj = new DataBaseHandler(getContext()); 
        Items1=dbobj.select_Howliketest();
        dbobj.CloseDataBase();
        Collections.reverse(Items1);

        Log.e("ffffff", (Items1.get(x).getiLike().toString()));
        if(Items1.get(x).getiLike().toString().equals("0")){
            response=Methods.HTTP_CommentLike(server_Comment_like,          table, target, user_id, secret_id); 


        }else  {
            response=Methods.HTTP_CommentLike(server_Comment_like_remove, table, target, user_id, secret_id);   

        }

        dbobj=new DataBaseHandler(getContext());
        dbobj.delete_Comment_TABLE();
        dbobj.CloseDataBase();
        response = Methods.HTTP_CommentList(server_List_of_Comments, "5",
                "6462", user_id, secret_id);
        response = "{" + response + "}";

        Methods.install_JSON_HowComment(getContext(), response);

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);



    }

}


    }

这是我的持有人

     class commentholder {
        ImageView img;
        TextView name;
        TextView commentBody;
        TextView like,numoflike;
    }

在我的活动中,单击该项目时它不起作用,我的文本视图工作而不是单击项目

        list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            Log.v("pos", "click");

        }


    });
4

1 回答 1

2

以下是您可以执行此操作的方法:在函数“getView()”中

view.setTag(R.id.your_id, yourTextViewWhichYouWantToChange);
view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView tv = (TextView)view.getTag(r.id.your_id);
            if(tv != null){
                //here you can change tv
                tv.setText("new text");
             }
        }
    });

并将您的 id 添加到

<item type="id" name="your_id"/>

========== 更新 =================

我认为这就是您需要的:在 getView() 函数中添加:

...
  //we are in getView() function
  //from holder we get 3 text view
  TextView tv1, tv2, tv3;   //of course it should be initialized etc..
  //every text has different id, I will be use id: tv_id1, tv_id2, tv_id3

  //sets listeners
  tv1.setOnClickListener(listener);
  tv2.setOnClickListener(listener);
  tv3.setOnClickListener(listener);

  //set values by tag
  tv1.setTag(tag_for_text_1);
  tv2.setTag(tag_for_text_2);
  tv3.setTag(tag_for_text_3);

   .... //it is all in getView()

现在我们必须创建监听器:

    private OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int id = view.getId();
                switch(id){
                    case R.id.tv_id1:  //here user clicked on text 1 
                        Integer value = (Integer)view.getTag(); //here you get value from textView1
                        //here do your stuff for text 1
                        break;

                    case R.id.tv_id2: //here user clicked on text 2 
                        Integer value = (Integer)view.getTag(); //here user clicked on text 2 
                        //here do stuff for text 2
                        break;

                    case R.id.tv_id3: //here user clicked on text 3 
                        Integer value = (Integer)view.getTag();//here you get value from textView3
                        //here do stuff for text 3
                        break;
                }
            }
        };

我希望这是你需要的:)

于 2013-09-09T08:39:01.497 回答