2013-11-25 150 views
0

在以下代碼中,listView的setonitemclicklistener無法正常工作。我不知道缺少什麼。Setonitemclicklistener無法正常運行()

public class OffersActivity extends ListActivity { 
private ProgressDialog pDialog; 
JSONParser jsonParser = new JSONParser(); 
ArrayList<HashMap<String, String>> offersList; 
OfferAdapter offerAdapter; 
JSONArray offers = null; 
private static final String OFFER_URL = "My URL of PHP file"; 

static final String OFFER_ID = "offer_id"; 
static final String OFFER_CONTENT = "offer_content"; 
static final String OFFER_PHOTO = "offer_photo"; 
static final String OFFER_PHOTO_THUMB = "offer_photo_thumb"; 
static final String OFFER_INTERNAL_PHOTO = "offer_internal_photo"; 
static final String OFFER_INTERNAL_PHOTO_THUMB = "offer_internal_photo_thumb"; 
static final String OFFER_CREATED_DATE = "offer_created_date"; 
static final String OFFER_ORD = "offer_ord"; 
static final String OFFER_STATE = "offer_state"; 
static final String OFFER_LAST_UPDATE = "offer_last-update"; 
static final String OFFER_TITLE = "offer_title"; 
static final String OFFER_OLD_PRICE = "offer_old_price"; 
static final String OFFER_CURRENT_PRICE = "offer_current_price"; 
static final String OFFER_OCAZION = "offer_ocazion"; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.offers_list); 
    offersList = new ArrayList<HashMap<String, String>>(); 
    new LoadOffer().execute(); 
} 
class LoadOffer extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(OffersActivity.this); 
     pDialog.setMessage(getResources().getString(R.string.loadOffers)); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    protected String doInBackground(String... args) { 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     JSONArray jsonArray = jsonParser.makeHttpRequest(OFFER_URL, "GET",params); 
     JSONObject jsonObject = null; 
     Log.d("Offer JSON: ", jsonArray.toString()); 

     try { 
      for (int i = 0; i < jsonArray.length(); i++) { 
       jsonObject = jsonArray.getJSONObject(i); 

       String offer_id = jsonObject.getString(OFFER_ID); 
       String offer_content = jsonObject.getString(OFFER_CONTENT); 
       String offer_photo = jsonObject.getString(OFFER_PHOTO); 
       String offer_photo_thumb = jsonObject.getString(OFFER_PHOTO_THUMB); 
       String offer_internal_photo = jsonObject.getString(OFFER_INTERNAL_PHOTO); 
       String offer_internal_photo_thumb = jsonObject.getString(OFFER_INTERNAL_PHOTO_THUMB); 
       String offer_created_date = jsonObject.getString(OFFER_CREATED_DATE); 
       String offer_ord = jsonObject.getString(OFFER_ORD); 
       String offer_state = jsonObject.getString(OFFER_STATE); 
       String offer_last_update = jsonObject.getString(OFFER_LAST_UPDATE); 
       String offer_title = jsonObject.getString(OFFER_TITLE); 
       String offer_old_price = jsonObject.getString(OFFER_OLD_PRICE); 
       String offer_current_price = jsonObject.getString(OFFER_CURRENT_PRICE); 
       String offer_ocazion = jsonObject.getString(OFFER_OCAZION); 

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

       map.put(OFFER_ID, offer_id); 
       map.put(OFFER_CONTENT, offer_content); 
       map.put(OFFER_PHOTO, offer_photo); 
       map.put(OFFER_PHOTO_THUMB, offer_photo_thumb); 
       map.put(OFFER_INTERNAL_PHOTO, offer_internal_photo); 
       map.put(OFFER_INTERNAL_PHOTO_THUMB, offer_internal_photo_thumb); 
       map.put(OFFER_CREATED_DATE, offer_created_date); 
       map.put(OFFER_ORD, offer_ord); 
       map.put(OFFER_STATE, offer_state); 
       map.put(OFFER_LAST_UPDATE, offer_last_update); 
       map.put(OFFER_TITLE, offer_title); 
       map.put(OFFER_OLD_PRICE, offer_old_price); 
       map.put(OFFER_CURRENT_PRICE, offer_current_price); 
       map.put(OFFER_OCAZION, offer_ocazion); 

       offersList.add(map); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     pDialog.dismiss(); 
     runOnUiThread(new Runnable() { 
      public void run() { 

       offerAdapter = new OfferAdapter(OffersActivity.this, offersList); 
       setListAdapter(offerAdapter); 

       getListView().setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
          long arg3) { 
          // not work.. 
         Toast.makeText(OffersActivity.this, "Hello", Toast.LENGTH_SHORT).show(); 
         // not work too.. 
         System.out.println("Hello"); 
        } 

       }); 

      } 
     }); 

    } 

} 

} 
+1

你沒有必要使用runOnUiThread(新的Runnable()在onPostExecute()method..onPostExecute始終執行UI thread.the問題是不是這個帖子您的佈局文件也.. –

+0

是否拋出任何異常? –

+0

這可能有所幫助: http://stackoverflow.com/questions/6589416/listview-setonitemclicklistener-not-executing – jpm

回答

0

刪除運行線程..箱下面的代碼XML 寫一個列表視圖

  listView.setOnItemClickListener(this); 
      @Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 
    Toast toast = Toast.makeText(getApplicationContext(), 
      "Item " + (position + 1) + ": " + rowItems.get(position), 
      Toast.LENGTH_SHORT); 
    toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); 
    toast.show(); 
} 
+2

這不回答問題 – Raghunandan