单击Listview中的选定项目时如何在Textview中设置文本?

时间:2014-11-25 10:19:06

标签: android listview

我正在使用ListView和ViewHolder,然后在Textview中使用setText然后显示错误。如何在Listview中的Textview中设置文本?每当我点击Like按钮然后我得到Like的总数,但我无法在TextView上为每个项目设置文本。每当点击所选项目,然后其TextView增加1成功。获取strCount是成功的,但是当点击选中的图像和我的代码时,如何为选定的TextView设置文本,

我的截图是Listview并在Listview中设置多个项目, enter image description here

我的适配器类,

public class Adapter1 extends BaseAdapter {

    public ArrayList<HashMap<String, String>> arr = null;
    Context context = null;
    LayoutInflater layoutInflater = null;
    HashMap<String, String> getData = new HashMap<String, String>();

    String url = null, urlCount = null;
    String strId = null, strCount = null;

    ViewHolder viewHolder = null;

    public Adapter1(Context context, ArrayList<HashMap<String, String>> arr) {

        this.context = context;
        this.arr = arr;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {

        return arr.size();
    }

    @Override
    public Object getItem(int position) {

        return arr.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (convertView == null) {

            convertView = layoutInflater.inflate(R.layout.list_item, null);
            /** initialize ViewHolder */
            viewHolder = new ViewHolder();

            /** Initialize Widgets */
            /** Imageview */

            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);

            /** TextView */
            viewHolder.txtId = (TextView) convertView.findViewById(R.id.txtId);

            viewHolder.txt = (TextView) convertView.findViewById(R.id.txt);

            getData = arr.get(position);

            viewHolder.txtId.setText(getData.get(Fragment1.TAG_ID));

            viewHolder.img.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                    strId = arr.get(position).get(Fragment1.TAG_ID);
                                System.out.println("!!!!!!!!!! strId======"+ strId);
                                url = "http://example.com/createpost.php?id="+ strId + "&user_id="+ myDetail.getUserId();

                    new sendData().execute();
                        }
            });

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    /** ViewHolder Class */
    @SuppressLint("NewApi")
    public class ViewHolder {

        ImageView img = null, 
        TextView txtId = null
                txt = null;
    }

    public class sendData extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            ServiceHandler sh = new ServiceHandler();
            String jsonStr = sh.makeServiceCall(urlLike, ServiceHandler.GET);
            System.out.println("!!!!!!!!!!!!!!!jsonStr in Do in===" + jsonStr);
            Log.d("Response : Like", ">" + jsonStr);

            return null;
        }

        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
            urlCount = "http://example.com/getalldata.php?id="+ strId;
            new getCountData().execute();
        };
    }

    /** Count the Total Like for Selected Items. */
    private class getCountData extends AsyncTask<Void, Void, Void> {

        JSONObject jsonobject;

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            jsonobject = JSONFunctions.getJSONfromURL(urlCount);

            try {

                strCount = jsonobject.getString("countdata");
            } catch (JSONException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
            System.out.println("!!!!!!!!strCount====" + strCount);
            viewHolder.txt.setText(String.valueOf(strCount));
        }
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

在onClick()函数中,您必须获取textview的实例并将其传递给AsyncTask

 
// if text and img have same parent the you can find textview like this is 
TextView tv =(TextView)v.getParent().findViewById(R.id.txt);
// if not then apply call getParent() function multiple time as per your layout
new sendData(tv).execute();
//Create constructor in sendData
public class sendData extends AsyncTask {
   private TextView tv;
   public sendData(TextView v){
       tv=v;
   }
   ...
 protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            urlCount = "http://example.com/getalldata.php?id="
                    + strId;
            new getCountData(tv).execute();
        };
}
    private class getCountData extends AsyncTask {
          private TextView v;
          public sendData(TextView v){
              tv=v;
         }
       ....
       // in onPostExecute use 
       tv.setText(String.valueOf(strCount));
    }

编辑:您可以通过标记位置获取正确的位置以查看并将其置于onClick

// add this line before return 
viewHolder.img.setTag(position);
// get position inside onClick()
int position =(Integer)v.getTag();