2014-06-20 121 views
0

我一直在面對一個問題......有五個不同的Java類MainActivity,Application,ApplicationAdapter,FetchData,FetchDataListener ..我綁定了來自ListView中的api的mysql數據......但我試圖火上的ListView點擊事件,但我得到錯誤....這裏是代碼...OnListItemClick事件不起作用

MainActivity.java

@Override 
public void onFetchComplete(List<Applicity> data) { 
    // dismiss the progress dialog 
    if(dialog != null) dialog.dismiss(); 
    // create new adapter 
    ApplicationAdapter adapter = new ApplicationAdapter(this, data); 
    // set the adapter to list 
    setListAdapter(adapter);   
} 
public void onListItemClick(ListView l, View v, int position, long id) { 
    Applicity app= new Applicity(); 
    int p =app.getPosition(position); 
    Toast.makeText(this,p,Toast.LENGTH_LONG).show(); 
} 

ApplicationAdapter.java

public class ApplicationAdapter extends ArrayAdapter<Applicity>{ 
private List<Applicity> items; 

public ApplicationAdapter(Context context, List<Applicity> items) { 
    super(context, R.layout.app_custom_list, items); 
    this.items = items; 
} 

@Override 
public int getCount() { 
    return items.size(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    if(v == null) { 
     LayoutInflater li = LayoutInflater.from(getContext()); 
     v = li.inflate(R.layout.app_custom_list, null);    
    } 

    Applicity app = items.get(position); 

    if(app != null) { 
     ImageView icon = (ImageView)v.findViewById(R.id.appIcon); 
     TextView titleText = (TextView)v.findViewById(R.id.titleTxt); 
     LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr); 
     TextView dlText = (TextView)v.findViewById(R.id.dlTxt); 

     if(icon != null) { 
      Resources res = getContext().getResources(); 
      String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon(); 
      icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null))); 
     } 

     if(titleText != null) titleText.setText(app.getTitle()); 

     if(dlText != null) { 
      NumberFormat nf = NumberFormat.getNumberInstance(); 
      dlText.setText(nf.format(app.getTotalDl())+" dl");    
     } 

     if(ratingCntr != null && ratingCntr.getChildCount() == 0) {   
      /* 
      * max rating: 5 
      */ 
      for(int i=1; i<=5; i++) { 
       ImageView iv = new ImageView(getContext()); 

       if(i <= app.getRating()) { 
        iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_checked)); 
       } 
       else {     
        iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_unchecked)); 
       } 

       ratingCntr.addView(iv); 
      } 
     } 
    } 

    return v; 
} 

}

Applicity.java

public class Applicity { 
private String title; 
private long totalDl; 
private int rating; 
public int position; 
private String icon; 

public String getTitle() { 
    return title; 
} 
public void setTitle(String title) { 
    this.title = title; 
} 
public long getTotalDl() { 
    return totalDl; 
} 
public void setTotalDl(long totalDl) { 
    this.totalDl = totalDl; 
} 
public int getRating() { 
    return rating; 
} 
public void setRating(int rating) { 
    this.rating = rating; 
} 
public String getIcon() { 
    return icon; 
} 
public void setIcon(String icon) { 
    this.icon = icon; 
} 
public int getPosition(int arg){ 
    return position; 
}  

}

+0

請從logcat中發表您的錯誤:d – raybaybay

+0

http://stackoverflow.com/q/24331855/3571822複製 –

回答

0

使用OnItemClickListener。您正在創建一個新的自定義方法,而不是OnItemClickListener。

lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     //Do something here. 
    } 
}); 
+0

嗨薩赫勒!我必須獲得行數據行單擊這就是爲什麼我使用OnListItemClick。是否有其他替代方法可以獲取有關行點擊的數據而不是點擊項目。 – user3754600

+0

這是完整的行本身就是這裏的'項目'。只要嘗試使用OnItemClickListener(如上圖所示),它肯定會起作用。一旦獲得了偵聽器函數中的行位置,您就可以查詢所有與該行相關的內部對象,即您的案例中的標題,評級,dl。 –

+0

謝謝親愛的!它現在工作,但添加getListView()後; – user3754600