Java 如何在异步方法之外获取列表的值?
我已经在另一个stackoverflow帖子上搜索了2个小时,但仍然无法解决这个问题。在Java 如何在异步方法之外获取列表的值?,java,android,android-asynctask,Java,Android,Android Asynctask,我已经在另一个stackoverflow帖子上搜索了2个小时,但仍然无法解决这个问题。在DetailMemilihIqroActivity类中有一个名为copyAudioListIqro的变量,其数据类型为列表字符串。当AsyncTask类中名为audioIqros的变量(确切地说是在onPostExecute方法中)时,此列表中有一个值来自我的json,我想通过updateData方法(在AsyncTask类之外)将audioIqros变量复制到copyAudioListIqro。当我在upd
DetailMemilihIqro
Activity类中有一个名为copyAudioListIqro
的变量,其数据类型为列表字符串。当AsyncTask类中名为audioIqros
的变量(确切地说是在onPostExecute方法中)时,此列表中有一个值来自我的json,我想通过updateData方法(在AsyncTask类之外)将audioIqros
变量复制到copyAudioListIqro
。当我在updateData方法上看到日志监视器时,我可以看到copyAudioListIqro
中的值,但问题是,当我通过readDataAudioURL方法(在asynctask类之外)访问它时,copyAudioListIqro
变量变为空
这个问题的解决方案是什么?
多谢各位
下面是memilihiqro类的总体细节
public class DetailMemilhIqro extends AppCompatActivity {
private ProgressDialog pDialog;
private List<ModelAudioIqro> audioIqros;
private List<String> copyAudioListIqro;
private AudioAdapter mAdapter;
private RecyclerView recyclerView;
private String TAG = DetailMemilihIqro.class.getSimpleName();
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_memilih_iqro);
recyclerView = (RecyclerView) findViewById(R.id.rvCVAudioIqro);
pDialog = new ProgressDialog(this);
audioIqros = new ArrayList<>();
mAdapter = new AudioAdapter(getApplicationContext(), audioIqros);
context = getApplicationContext();
copyAudioListIqro = new ArrayList<>();
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
Bundle getPosition = getIntent().getExtras();
int position = getPosition.getInt("positionUserClicked");
Log.d(TAG, "Position User clicked " + position);
if (position == 0) {
String endpoint = "http://latihcoding.com/jsonfile/audioiqro1.json";
new DownloadTask().execute(endpoint);
} else if (position == 1) {
String endpoint = "http://latihcoding.com/jsonfile/audioiqro2.json";
new DownloadTask().execute(endpoint);
} else if (position == 2) {
String endpoint = "http://latihcoding.com/jsonfile/audioiqro3.json";
new DownloadTask().execute(endpoint);
}
readDataAudioURL();
}
public void updateData(List<String> pathUrl) {
for (int i = 0; i < pathUrl.size(); i++) copyAudioListIqro.add(pathUrl.get(i));
Log.d(TAG, "updateData Method " + copyAudioListIqro.toString());
}
public void readDataAudioURL() {
Log.d(TAG, "readDataAudioURL Method " + copyAudioListIqro.toString());
}
public class DownloadTask extends AsyncTask<String, Void, List<String>> {
List<String> modelAudioIqroList;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Downloading json...");
pDialog.show();
}
@Override
protected List<String> doInBackground(String... strings) {
modelAudioIqroList = new ArrayList<>();
int result;
HttpURLConnection urlConnection;
try {
URL url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int statusCode = urlConnection.getResponseCode();
// 200 represents HTTP OK
if (statusCode == 200) {
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
response.append(line);
}
parseResult(response.toString());
result = 1; // Successful
Log.d(TAG, "Result " + result);
} else {
//"Failed to fetch data!";
result = 0;
Log.d(TAG, "Result " + result);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return modelAudioIqroList; //"Failed to fetch data!";
}
@Override
protected void onPostExecute(List<String> audioIqros) {
super.onPostExecute(audioIqros);
pDialog.hide();
if (!audioIqros.isEmpty()) {
updateData(modelAudioIqroList);
} else {
Toast.makeText(context, "Empty", Toast.LENGTH_SHORT).show();
}
}
private void parseResult(String result) {
try {
JSONArray response = new JSONArray(result);
for (int i = 0; i < response.length(); i++) {
JSONObject object = response.getJSONObject(i);
ModelAudioIqro modelAudioIqro = new ModelAudioIqro();
modelAudioIqro.setName(object.getString("name"));
modelAudioIqro.setUrl(object.getString("url"));
String path = modelAudioIqro.getUrl();
Log.d(TAG, "String path " + path);
modelAudioIqroList.add(path);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter.notifyDataSetChanged();
}
}
}
public类DetailMemilhIqro扩展了AppCompative活动{
私人对话;
私人名单;
私有列表CopyAudioListikro;
专用音频适配器;
私人回收站;
private String TAG=DetailMemilihIqro.class.getSimpleName();
语境;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u detail\u memilih\u iqro);
recyclerView=(recyclerView)findViewById(R.id.rvCVAudioIqro);
pDialog=新建进度对话框(此对话框);
audioIqros=新的ArrayList();
mAdapter=新的音频适配器(getApplicationContext(),audioIqros);
context=getApplicationContext();
copyAudioListIqro=新的ArrayList();
setLayoutManager(新的LinearLayoutManager(上下文));
setItemAnimator(新的DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
Bundle getPosition=getIntent().getExtras();
int position=getPosition.getInt(“positionUserClicked”);
Log.d(标签,“位置用户点击”+位置);
如果(位置==0){
字符串端点=”http://latihcoding.com/jsonfile/audioiqro1.json";
新建DownloadTask().execute(端点);
}否则如果(位置==1){
字符串端点=”http://latihcoding.com/jsonfile/audioiqro2.json";
新建DownloadTask().execute(端点);
}否则如果(位置==2){
字符串端点=”http://latihcoding.com/jsonfile/audioiqro3.json";
新建DownloadTask().execute(端点);
}
readDataAudioURL();
}
公共无效更新数据(列表路径URL){
对于(inti=0;i
readDataAudioURL()
调用,即普通的日志调用,应该被移动。事实上,任务本质上是异步的,因此变量是倾斜的
@Override
protected void onPostExecute(List<String> audioIqros) {
super.onPostExecute(audioIqros);
pDialog.hide();
if (!audioIqros.isEmpty()) {
updateData(modelAudioIqroList);
//data is now updated, notify datasets and/or send broadcast
mAdapter.notifyDataSetChanged();
readDataAudioURL();
} else {
Toast.makeText(context, "Empty", Toast.LENGTH_SHORT).show();
}
}