[英]Android action listener to call a method in another class
我有一個列表視圖,每行都有一個按鈕。 我制作了一個自定義 Adapter 類和一個 ItemModel 類來保存每一行的數據。 在 ItemModel 類中,我為按鈕定義了一個 ActionListener。 如何從按鈕的動作偵聽器內部調用另一個類中的方法?
現在,如果我說 Classname clsName = new Classname(); 在 actionlistener 里面做 clsName.methodName(variableToPass); <--- 這一切都編譯但當我點擊按鈕時崩潰了..有誰知道如何讓它工作?
MyListModel 類
public class MyListItemModel{ //that's our book
private String title; // the book's title
private String description; //the book's description
int id; //book owner id
String key; //book key
private Context context;
Shelf shelf = new Shelf(); //shelf class
public MyListItemModel(Context c){
this.context=c;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getKey() {
return key;
}
public void setKey(String key){
this.key = key;
}
OnClickListener listener = new OnClickListener(){ // the book's action
@Override
public void onClick(View v) {
//code for the button action
//THIS DOESN'T WORK PROPERLY AND CRASHES ON CLICK. However if i use a Toast to print the key on each click - it will print the right key to screen.
shelf.downloadBook(new String(key));
}
};
int getBookId(){
return title.hashCode();
}
}
MyListAdapter 類 - getView 的方法
public class MyListAdapter extends BaseAdapter {
View renderer;
List<MyListItemModel> items;
ArrayList<HashMap<String, String>> mylist;
private LayoutInflater mInflater;
private Context context;
public MyListAdapter(Context c){
this.context=c;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
//convertView = renderer;
convertView = mInflater.inflate(R.layout.shelfrow, null);
}
MyListItemModel item = items.get(position);
TextView label = (TextView)convertView.findViewById(R.id.item_title);
label.setText(item.getTitle());
TextView label2 = (TextView)convertView.findViewById(R.id.item_subtitle);
label2.setText(item.getDescription());
Button button = (Button)convertView.findViewById(R.id.btn_download);
button.setOnClickListener(item.listener);
return convertView;
}
我的 Shelf 類有一個名為 downloadBook(String bookKey) <-- 這是我想在每次單擊按鈕時調用的方法,並將相應的書本密鑰傳遞給該方法。 我也有 2 個 xml 文件(shelfrow.xml 和shelflist.xml)。 一個包含文本字段和按鈕,另一個包含列表視圖。
Shelf.java 類中的一些代碼
List<MyListItemModel> myListModel = new ArrayList<MyListItemModel>();
try{
JSONArray entries = json.getJSONArray("entries");
for(int i=0;i<entries.length();i++){
MyListItemModel item = new MyListItemModel(this);
JSONObject e = entries.getJSONObject(i);
item.id = i; //user ID
bookKey = (e.getString("key"));
item.setTitle(e.getString("title"));
item.setDescription(e.getString("description"));
myListModel.add(item);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
MyListAdapter adapter = new MyListAdapter(this);
adapter.setModel(myListModel);
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);
….
public void downloadBook(String theKey) {
//take theKey and append it to a url address to d/l
}
來自 logcat 的堆棧跟蹤
05-23 02:34:59.439: INFO/wpa_supplicant(14819): Reset vh_switch_counter due to receive LINKSPEED cmd 05-23 02:34:59.439: DEBUG/ConnectivityService(1346): getMobileDataEnabled returning true 05-23 02:36:39.269: DEBUG/StatusBarPolicy(6068): onSignalStrengthsChange
這也出現了 zygoteinit methodandargscaller.run
我要在這里冒險,但我想我知道問題是什么。
在最后一段代碼中,您沒有在MyListItemModel
上設置key
字段。 您正在設置一些名為“bookKey”的變量(我沒有看到它的定義位置)。
我敢打賭,如果你改變這一行:
bookKey = (e.getString("key"));
是這樣的:
item.setKey(e.getString("key"));
//or item.key = e.getString("key"));
一切都會為你工作得很好。 如果將null
字符串傳遞給 String(String) 構造函數,則會得到 NullPointerException,因為該構造函數需要非空字符串。
我會提到您沒有必要使用 String(String) 構造函數,您只需在第一個片段中執行此操作即可:
shelf.downloadBook(key);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.