在Asynctask之后从结果中更新ViewPager(Update ViewPager after Asynctask from the result)

编程入门 行业动态 更新时间:2024-10-28 11:34:01
在Asynctask之后从结果中更新ViewPager(Update ViewPager after Asynctask from the result)

我在这里遇到了问题。

我有一个login_activity执行asynctask从php连接到mysql数据库的文件中得到一些结果。

public class login_main extends AppCompatActivity implements OnDataSendToActivity{ ViewPager viewPager; CustomAdapter adapter; ArrayList<String> image = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //setSupportActionBar(toolbar); String type = "getProfileImages"; RetrieveData retrieveData = new RetrieveData(this); retrieveData.execute(type); //After Asycntask, get the result and put back into image array viewPager = (ViewPager) findViewById(R.id.view_pager); adapter = new CustomAdapter(login_main.this,image); viewPager.setAdapter(adapter); } public void sendData(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObject.getJSONArray("result"); for (int i = 0 ; i < result.length(); i++) { JSONObject obj = result.getJSONObject(i); String id = obj.getString("image"+i); image.add("http://192.168.12.252/"+id+"/profile.png"); } } catch (JSONException e) { e.printStackTrace(); } }

当我得到结果时,我尝试使用asynctask的接口来处理结果

protected void onPostExecute(String result) { super.onPostExecute(result); dataSendToActivity.sendData(result); }

一切工作正常,除了当我将它传递给PageAdapter时,图像数组总是空的。 我如何将数据传回到asycntask的活动中,并通过使用接口将它推入数组映像,它无法工作。

或者无论如何,我可以直接在Asyntask中处理它并更新ViewPager。

这是我的Asynctask

public class RetrieveData extends AsyncTask<String,Void,String>{ Context context; RetrieveData(Context ctx) { context = ctx; } private OnDataSendToActivity dataSendToActivity; public RetrieveData(Activity activity){ dataSendToActivity = (OnDataSendToActivity)activity; } @Override protected String doInBackground(String... params) { String profile_url = "http://192.168.12.252/getprofile.php"; String image_url = "http://192.168.12.252/imagelist.php"; String type = params[0]; if(type.equals("getProfile")){ try { String user_name = params[1]; URL url = new URL(profile_url); HttpURLConnection httpURLConnection = null; httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); OutputStream outputStream = httpURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8"); bufferedWriter.write(post_data); bufferedWriter.flush(); bufferedWriter.close(); outputStream.close(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); String result = null; result = bufferedReader.readLine(); bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); return result; } catch (IOException e) { e.printStackTrace(); } }else if(type.equals("getProfileImages")){ try { URL url = new URL(image_url); HttpURLConnection httpURLConnection = null; httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); OutputStream outputStream = httpURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); String post_data = URLEncoder.encode("Image", "UTF-8")+"="+URLEncoder.encode("Image", "UTF-8"); bufferedWriter.write(post_data); bufferedWriter.flush(); bufferedWriter.close(); outputStream.close(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); String result = null; result = bufferedReader.readLine(); bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); return result; } catch (IOException e) { e.printStackTrace(); } } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); dataSendToActivity.sendData(result); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } }

I had a problem here.

I have a login_activity that execute asynctask to get some result from a php file which it connects to mysql database.

public class login_main extends AppCompatActivity implements OnDataSendToActivity{ ViewPager viewPager; CustomAdapter adapter; ArrayList<String> image = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //setSupportActionBar(toolbar); String type = "getProfileImages"; RetrieveData retrieveData = new RetrieveData(this); retrieveData.execute(type); //After Asycntask, get the result and put back into image array viewPager = (ViewPager) findViewById(R.id.view_pager); adapter = new CustomAdapter(login_main.this,image); viewPager.setAdapter(adapter); } public void sendData(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObject.getJSONArray("result"); for (int i = 0 ; i < result.length(); i++) { JSONObject obj = result.getJSONObject(i); String id = obj.getString("image"+i); image.add("http://192.168.12.252/"+id+"/profile.png"); } } catch (JSONException e) { e.printStackTrace(); } }

When i get the result i try to process the result by using interface from the asynctask

protected void onPostExecute(String result) { super.onPostExecute(result); dataSendToActivity.sendData(result); }

Everything is working fine except that the image array always empty when i pass it to the PageAdapter. How can i pass the data back to activity from asycntask and push into the array image, by using interface, it cant work.

Or anyway i can directly process it in Asyntask and update the ViewPager.

Here is my Asynctask

public class RetrieveData extends AsyncTask<String,Void,String>{ Context context; RetrieveData(Context ctx) { context = ctx; } private OnDataSendToActivity dataSendToActivity; public RetrieveData(Activity activity){ dataSendToActivity = (OnDataSendToActivity)activity; } @Override protected String doInBackground(String... params) { String profile_url = "http://192.168.12.252/getprofile.php"; String image_url = "http://192.168.12.252/imagelist.php"; String type = params[0]; if(type.equals("getProfile")){ try { String user_name = params[1]; URL url = new URL(profile_url); HttpURLConnection httpURLConnection = null; httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); OutputStream outputStream = httpURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8"); bufferedWriter.write(post_data); bufferedWriter.flush(); bufferedWriter.close(); outputStream.close(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); String result = null; result = bufferedReader.readLine(); bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); return result; } catch (IOException e) { e.printStackTrace(); } }else if(type.equals("getProfileImages")){ try { URL url = new URL(image_url); HttpURLConnection httpURLConnection = null; httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); OutputStream outputStream = httpURLConnection.getOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); String post_data = URLEncoder.encode("Image", "UTF-8")+"="+URLEncoder.encode("Image", "UTF-8"); bufferedWriter.write(post_data); bufferedWriter.flush(); bufferedWriter.close(); outputStream.close(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); String result = null; result = bufferedReader.readLine(); bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); return result; } catch (IOException e) { e.printStackTrace(); } } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); dataSendToActivity.sendData(result); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } }

最满意答案

执行顺序都搞砸了。 在你向图像数组添加数据时,空图像数组已经被传递给视图寻呼机适配器,因此图像数组为空。

你继续做同样的事情,但不是在onCreate()中设置view pager适配器,而是在你调用方法sendData()后设置适配器。

像这样修改sendData()方法。

public void sendData(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObject.getJSONArray("result"); for (int i = 0 ; i < result.length(); i++) { JSONObject obj = result.getJSONObject(i); String id = obj.getString("image"+i); image.add("http://192.168.12.252/"+id+"/profile.png"); } } catch (JSONException e) { e.printStackTrace(); } adapter = new CustomAdapter(login_main.this,image);//set ur image array here viewPager.setAdapter(adapter); }

The execution sequence is all messed up. by the time ur adding data to ur image array the,the empty image array has already been passed to the view pager adapter,therfore ur image array is empty.

u continue to do the same thing but instead of setting view pager adapter in onCreate() set the adapter after u call the method sendData();

modify the sendData() method like this.

public void sendData(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObject.getJSONArray("result"); for (int i = 0 ; i < result.length(); i++) { JSONObject obj = result.getJSONObject(i); String id = obj.getString("image"+i); image.add("http://192.168.12.252/"+id+"/profile.png"); } } catch (JSONException e) { e.printStackTrace(); } adapter = new CustomAdapter(login_main.this,image);//set ur image array here viewPager.setAdapter(adapter); }

更多推荐

本文发布于:2023-08-06 13:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ViewPager   Asynctask   result   Update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!