Android动作监听器在另一个类中调用一个方法

时间:2011-05-23 00:10:10

标签: android button action listener

我有一个列表视图,每行都有一个按钮。我已经创建了一个自定义Adapter类和一个ItemModel类来保存每行的数据。在ItemModel类中,我为按钮定义了一个ActionListener。如何从我的按钮的动作监听器中调用另一个类中的方法?

现在,如果我说Classname clsName = new Classname();并且在actionlistener内部执行clsName.methodName(variableToPass); < ---当我点击按钮时,这一切都会编译但崩溃。任何人都知道如何让它工作?

MyListModel Class

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;
}

My Shelf类有一个名为downloadBook(String bookKey)的方法&lt; - 这是我想用每个按钮点击调用的方法,并将此方法传递给相应的书键。我还有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的Stacktrace

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

1 个答案:

答案 0 :(得分:0)

我将在这里走出困境,但我想我知道这是什么问题。

在最后一段代码中,您没有在key上设置MyListItemModel字段。您正在设置一个名为'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);