我目前正在开发一个Android项目,我希望能够按下一个按钮,它会将用户数据显示在另一个活动的列表视图中。我目前在某种意义上工作,但它需要我按两个按钮。一个从mysql数据库中获取json数据,另一个然后将其发送到下一个活动,允许我显示它。
我希望有一种方法可以通过一次单击按钮来运行这两种方法,或者在一种方法中同时使用这两种方法。
public class MyMainActivity extends AppCompatActivity {
String url = "http://192.168.20.120";
String json_string;
View v;
//Button to get the data
ImageButton getUserDetailsBtn;
//Button to parse the data
ImageButton parseUserDetailsBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
getUserDetailsBtn= (ImageButton)findViewById(R.id.getUserDetailsButton);
parseUserDetailsBtn = (ImageButton)findViewById(R.id.parseUserDetailsButton);
getUserDetailsBtn.setVisibility(View.GONE);
}
//
//get user details in app
//
public void getDetails(View view) {
new LoadUserDetails().execute();
parseUserDetailsBtn .setVisibility(View.GONE);
getUserDetailsBtn= .setVisibility(View.VISIBLE);
}
//Load the user details
class LoadUserDetails extends AsyncTask<Void,Void,String>
{
String json_url;
String JSON_STRING;
@Override
protected void onPreExecute() {
json_url = url+"/getUserDetails.php";
}
@Override
protected String doInBackground(Void... voids){
try{
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while((JSON_STRING = bufferedReader.readLine())!=null)
{
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedJsonException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
TextView textview = (TextView) findViewById(R.id.textview);
textview.setText("");
json_string = result;
}
}
//
//parse the data
//
public void parseJSON(View view)
{
if(json_string==null){
Toast.makeText(getApplicationContext(), "First get JSON Data", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(this, ShowUserActivity.class);
intent.putExtra("json_data", json_string);
startActivity(intent);
}
}
答案 0 :(得分:3)
从Activity
开始onPostExecute
,因为onPostExecute
在UI
帖子上执行
@Override
protected void onPostExecute(String result) {
TextView textview = (TextView) findViewById(R.id.textview);
textview.setText("");
json_string = result;
if(json_string==null){
Toast.makeText(getApplicationContext(), "First get JSON Data", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(MyMainActivity.this, ShowUserActivity.class);
// and change this to MyMainActivity.this
// this will point to your task instead of Activity
intent.putExtra("json_data", json_string);
startActivity(intent);
}
}