-1

我正在尝试将path函数的值传递给onListItemClick()同一个类。但我无法做到这一点。请参阅以下示例代码 -

public void insert(String path) //I want to pass this path variable to onListItemClick().
{
  setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {

super.onListItemClick(l, v, position, id);

  // ListView Clicked item index
   int itemPosition     = position;

  // ListView Clicked item value
   String  itemValue    = (String) l.getItemAtPosition(position);

 //  content.setText("Click : \n  Position :"+itemPosition+"  \n  ListItem : " +itemValue);
   info.insert(path,itemValue); //Here I am using the path variable.


} 

请帮助我,因为我是android的新手。

提前致谢。

4

1 回答 1

1

我不知道你正在实现的接口,但通过跨实例方法使用实例字段,你可以解决这个问题

public class Main {
    private String tempPath;

    public void insert(String path) {       
        this.tempPath = path;
        onListItemClick(null, null, 0, 0);
    }

    public void onListItemClick(Object l, Object v, int position, long id) { // just for show, should be your actual method
        System.out.println(tempPath); // do some null check, obviously
    }

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.insert("Path to enlightenment.");
    }
}
于 2013-09-24T14:00:07.010 回答