单击文本在列表项内查看未获得正确值

时间:2014-05-13 05:21:31

标签: android android-listview android-asynctask

我有一个ListView,每个ListItem都有一些项目(textViews),其中一个textView有click事件。我已经放入我的自定义adater,而我在其cLick事件上调用asynctask,为此当我点击那个textView时,我想要一个值" queID"我想要,但目前我得到的是null ..请看我的代码,请帮助我..! " qoute"是textView.On,我点击了event.how来设置" buyer_req_Id"并获得标签?我在每个" ListItem"上成功地获得了标签。点击事件,我想在ListItem的textView.Inside java类中设置相同的标签我只放了适配器,我想要" buyer_request_id"。 的 Adapter.java

package com.epe.yehki.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.epe.yehki.ui.BuyingreqActivity;
import com.epe.yehki.ui.BuyingreqActivity.GetQuoteList;
import com.epe.yehki.util.Const;
import com.example.yehki.R;

public class BuyingRequestAdapter extends BaseAdapter {
    public ArrayList<HashMap<String, String>> BuyingRequestArray;
    private Context mContext;

    public BuyingRequestAdapter(Context paramContext, ArrayList<HashMap<String, String>> productList) {
        this.mContext = paramContext;
        this.BuyingRequestArray = productList;

    }

    public int getCount() {
        return this.BuyingRequestArray.size();
    }

    public Object getItem(int paramInt) {
        return BuyingRequestArray.get(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    @SuppressWarnings("static-access")
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
        Viewholder localViewholder;
        if (paramView == null) {
            paramView = localLayoutInflater.inflate(R.layout.raw_buying_req, paramViewGroup, false);
            localViewholder = new Viewholder();
            localViewholder.sub = ((TextView) paramView.findViewById(R.id.sub));
            localViewholder.expDate = ((TextView) paramView.findViewById(R.id.exp_date));
            localViewholder.quote = ((TextView) paramView.findViewById(R.id.quote));
            localViewholder.status = ((TextView) paramView.findViewById(R.id.status));
            localViewholder.lastUpdate = ((TextView) paramView.findViewById(R.id.last_updated));

            paramView.setTag(localViewholder);

        } else {

            localViewholder = (Viewholder) paramView.getTag();
        }

        System.out.println(":::::::::::::::values:::::::::::::::" + BuyingRequestArray.get(paramInt).get(Const.TAG_PRODUCT_NAME));
        localViewholder.sub.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_PRODUCT_NAME));
        localViewholder.expDate.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_EXPIRY_DATE));
        localViewholder.lastUpdate.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_DATE_MODIFIED));
        localViewholder.quote.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_QUOTE_COUNT));
        localViewholder.quote.setTextColor(Color.parseColor("#0000ff"));
        localViewholder.status.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_BUYING_REQUEST_STATUS));

        localViewholder.quote.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("::::::::::::::::::quote clicked...!!");

                GetQuoteList getQuoteList = ((BuyingreqActivity) mContext).new GetQuoteList();
                getQuoteList.execute();

            }
        });
        return paramView;

    }

    static class Viewholder {
        TextView sub;
        TextView lastUpdate;
        TextView expDate;
        TextView quote;
        TextView status;

    }

}

java class(asynctask)

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(BuyingreqActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            scr_post.setVisibility(View.GONE);
            scr_view.setVisibility(View.GONE);
            quote_view.setVisibility(View.VISIBLE);
            pDialog.show();

        }

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

            // String query = "?customer_id=" +
            // Pref.getValue(BuyingreqActivity.this, Const.PREF_CUSTOMER_ID, "")
            // + "&buyer_request_id=23";
            String query = "?customer_id=" + Pref.getValue(BuyingreqActivity.this, Const.PREF_CUSTOMER_ID, "") + "&buyer_request_id=" + buyer_request_id;
            query = query.replace(" ", "%20");
            viewURL = Const.API_QUOTE_RECIEVED + query;
            BackendAPIService sh = new BackendAPIService();

            System.out.println(":::::::::::::::::::ADDRESS URL:::::::::::::::::" + viewURL);
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(viewURL, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);

                    if (jsonObj.has(Const.TAG_BUYING_REQUEST)) {
                        System.out.println("::::::::::::::::true::::::::::::::::" + jsonObj.has(Const.TAG_ADDRESS_LIST));
                        requestes = jsonObj.getJSONArray(Const.TAG_BUYING_REQUEST);

                        if (requestes != null && requestes.length() != 0) {
                            // looping through All Contacts

                            System.out.println(":::::::::::FLAG IN SUB:::::::::::" + flag);
                            for (int i = 0; i < requestes.length(); i++) {
                                JSONObject c = requestes.getJSONObject(i);

                                buyerID = c.getString(Const.TAG_BUYING_REQUEST_ID);
                                System.out.println(":::::::::::::::MY buying request:::::::::::::" + buyerID);
                                String product_name = c.getString(Const.TAG_PRODUCT_NAME);
                                String quote_id = c.getString(Const.TAG_QUOTE_ID);
                                String supplier_name = c.getString(Const.TAG_SUPPLIER_NAME);
                                String status = c.getString(Const.TAG_STATUS);

                                HashMap<String, String> quote = new HashMap<String, String>();

                                quote.put(Const.TAG_BUYING_REQUEST_ID, buyerID);
                                quote.put(Const.TAG_PRODUCT_NAME, product_name);
                                quote.put(Const.TAG_QUOTE_ID, quote_id);
                                quote.put(Const.TAG_EXPIRY_DATE, supplier_name);
                                quote.put(Const.TAG_QUOTE_COUNT, status);
                                queList.add(quote);
                                System.out.println(":::::::::::::Buyer request ID:" + buyerID);
                            }
                        }
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * 
             * */

            quoteAdapter = new QuoteAdapter(BuyingreqActivity.this, queList);

            quoteList.setAdapter(quoteAdapter);

        }

    }

2 个答案:

答案 0 :(得分:0)

//试试这种方式,希望这会对你有帮助......

localViewholder.quote.setTag(BuyingRequestArray.get(paramInt)获得(Const.TAG_BUYING_REQUEST_ID)); localViewholder.quote.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
      System.out.println("::::::::::::::::::quote clicked...!!");
      String buyerId = v.getTag().toString(); // user this buyerId for your requirement 
      System.out.println("Buyer Id Is "+buyerId);
      GetQuoteList getQuoteList = ((BuyingreqActivity) mContext).new GetQuoteList();
      getQuoteList.execute();

   }
});

答案 1 :(得分:0)

我通过将asynctask更改为void to string解决了我的问题: 的 adapter.java

package com.epe.yehki.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.epe.yehki.ui.BuyingreqActivity;
import com.epe.yehki.ui.BuyingreqActivity.GetQuoteList;
import com.epe.yehki.util.Const;
import com.example.yehki.R;

public class BuyingRequestAdapter extends BaseAdapter {
    public ArrayList<HashMap<String, String>> BuyingRequestArray;
    private Context mContext;

    public BuyingRequestAdapter(Context paramContext, ArrayList<HashMap<String, String>> reqList) {
        this.mContext = paramContext;
        this.BuyingRequestArray = reqList;

    }

    public int getCount() {
        return this.BuyingRequestArray.size();
    }

    public Object getItem(int paramInt) {
        return BuyingRequestArray.get(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    @SuppressWarnings("static-access")
    public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup) {
        LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
        Viewholder localViewholder;
        if (paramView == null) {
            paramView = localLayoutInflater.inflate(R.layout.raw_buying_req, paramViewGroup, false);
            localViewholder = new Viewholder();

            localViewholder.sub = ((TextView) paramView.findViewById(R.id.sub));
            localViewholder.expDate = ((TextView) paramView.findViewById(R.id.exp_date));
            localViewholder.quote = ((TextView) paramView.findViewById(R.id.quote));
            localViewholder.status = ((TextView) paramView.findViewById(R.id.status));
            localViewholder.lastUpdate = ((TextView) paramView.findViewById(R.id.last_updated));

            paramView.setTag(localViewholder);
            /* localViewholder.quote.setTag(localViewholder); */

        } else {

            localViewholder = (Viewholder) paramView.getTag();
            /* localViewholder.quote = ((TextView) paramView.getTag()); */
        }
        final String reqId = BuyingRequestArray.get(paramInt).get(Const.TAG_BUYING_REQUEST_ID);
        System.out.println("::::::::::::::My reqId:::::::::::" + reqId);
        localViewholder.sub.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_PRODUCT_NAME));
        localViewholder.expDate.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_EXPIRY_DATE));
        localViewholder.lastUpdate.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_DATE_MODIFIED));
        localViewholder.quote.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_QUOTE_COUNT));
        localViewholder.quote.setTextColor(Color.parseColor("#0000ff"));
        localViewholder.status.setText(BuyingRequestArray.get(paramInt).get(Const.TAG_BUYING_REQUEST_STATUS));

        localViewholder.quote.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out.println("::::::::::::::::::quote clicked...!!");

                System.out.println("Buyer Id Is " + reqId);
                GetQuoteList getQuoteList = ((BuyingreqActivity) mContext).new GetQuoteList();
                getQuoteList.execute(reqId);

            }
        });
        return paramView;

    }

    static class Viewholder {
        TextView sub;
        TextView lastUpdate;
        TextView expDate;
        TextView quote;
        TextView status;

    }

}