2014-02-10 32 views
0

我有一個使用AsyncTask加載的CustomListView。 我有一個imageButton到每一行的末尾。如何在自定義ListView中獲取位置並設置ImageButton的偵聽器

我的問題:當我點擊我的imageButton時,沒有任何工作。 我試過很多的解決方案是這樣的:

Android : how to set listener and get position of imagebutton click in a custom adapter

我的ListView類誰打電話的AsyncTask:

public class listview extends Activity { 

private ListView maListViewPerso; 
ImageButton imgButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ActionBar actionBar = getActionBar(); 
    actionBar.setBackgroundDrawable(new ColorDrawable(Color 
      .parseColor("#000000"))); 

    maListViewPerso = (ListView) findViewById(R.id.listviewperso); 

    asyncTask asynchrone_task_tournee = new asyncTask(listview.this, maListViewPerso); 
    asynchrone_task_tournee.execute(); 

} 

這是我的ImageButton我想要得到的位置,並設置一個監聽.. enter image description here

EDIT 1(回答評論)這是我的onPostExecute()並有我的SimpleA dapter:

@Override 
protected void onPostExecute(JSONArray jArray) {   
    super.onPostExecute(jArray); 

    JSONObject json_data=null;  
    HashMap<String, String> map; 
    ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); 

    this.pDialog.dismiss();  



     Log.i("JSON_TOURNER",jArray.toString()); 

     for(int i=0;i < jArray.length();i++) 
     {    
      try { 

       json_data = jArray.getJSONObject(i); 

       map = new HashMap<String, String>(); 
       map.put("titre", json_data.getString("time")); 
       map.put("description", json_data.getString("date")); 
       map.put("begin_hour", json_data.getString("hour")); 
       map.put("end_hour", json_data.getString("hour")); 


       listItem.add(map); 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
     } 

     SimpleAdapter mSchedule = new SimpleAdapter (Mycontext, listItem, R.layout.list_type_tournee, 
      new String[] {"titre", "description","begin_hour","end_hour"}, new int[] {R.id.miscellaneous, R.id.infos,R.id.begin_hour,R.id.end_hour}); 

     listview.setAdapter(mSchedule); 


} 
+1

哪裏是你'ListAdapter'碼的我的父母直線?你可以在'getView()'方法中實現它。 – SMR

回答

0

好吧,如果你在一個ListView使用的控制一樣ImageButtonCheckBoxButton等,那麼你將面臨的問題討論herehere

要解決此問題,您需要在ImageButton的xml中添加android:focusable="false"

如果以上不工作,那麼你應該增加android:descendantFocusability="blocksDescendants"佈局您ImageButton的,然後實現onClickListener()ImageButton

更多詳情請見this

希望它有幫助。

+0

我試圖爲ImageButton實現onClickListener(),並將pet android:descendantFocusability =「blocksDescendants」實現爲父佈局。但是當我運行的應用程序,它直接崩潰 –

+0

你的錯誤是什麼?用日誌貓更新你的問題.. – SMR

0

我解決了我的問題。 最後,我刪除了imageButton,並且在我的線性佈局中寵了一個框架佈局,並且在我的listeView上打開一個監聽器來打開對話框。

我的寵物機器人:descendantFocusability =「blocksDescendants」也是如此,在框架佈局

相關問題