在android按钮中返回值

时间:2016-07-02 13:35:45

标签: java android

我想为这部分代码返回imageUrls数组 " adapter = new LazyAdapter(this,imageUrls); "当按钮单击时 它的错误说你无法在void方法中返回值,我不知道用什么方法来返回值,

我的代码:

public class MainActivity extends Activity {
ListView list;
LazyAdapter adapter;
public static String res="";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn1=(Button)findViewById(R.id.button1);
    list = (ListView) findViewById(R.id.listView1);
    String[] imageUrls = new String[0];
    adapter = new LazyAdapter(this, imageUrls);
    list.setAdapter(adapter);

    btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new server_side("http://192.168.1.6:81/upload/time.php","").execute();
        String imageUrls[]={"http://localhost:81/upload/url.jpg"};

        if (!res.equals("")) {
            Toast.makeText(getApplicationContext(), "get data", Toast.LENGTH_SHORT).show();
    }
return imageUrls;
}
});

}


}

1 个答案:

答案 0 :(得分:3)

不要忘记你在覆盖方法中... ...   View.OnClickListener 界面的public void onClick(View v)方法...

所以基本上你 不能 在那里返回任何东西......你能做的最多就是回电,

解决方案:

你可以肯定修改逻辑

public class MainActivity extends Activity {
private ListView list;
private LazyAdapter adapter;
private String res = "";
//here declare as class variable....
private String[] imageUrls;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //here initialize it....
    imageUrls = new String[1];

    Button btn1=(Button)findViewById(R.id.button1);
    list = (ListView) findViewById(R.id.listView1);
    adapter = new LazyAdapter(this, imageUrls);
    list.setAdapter(adapter);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new server_side("http://192.168.1.6:81/upload/time.php","").execute();
            //here asign it some value....
            imageUrls[0] = {"http://localhost:81/upload/url.jpg"};
            if (!res.equals("")) {
                Toast.makeText(getApplicationContext(), "get data", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}