我试图将2个String []从doinbackground传递到片段,其中我有RecyclerView。我也尝试过setter getter方法,但仍然没有运气
这是我的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.activity_friend_list,container,false);
recyclerView= (RecyclerView) view.findViewById(R.id.RecyclerViewForFriendList);
//WANTS TO REPLACE THESE TWO ARRAY WITH WHICH I AM GETTING IN DOINBACKGROUND
Name=getResources().getStringArray(R.array.left_Drawer_Menu);
Image=getResources().obtainTypedArray(R.array.user_image);
recyclerView.setHasFixedSize(true);
adapter=new RecyclerFriendListAdapter(getActivity(),Distance,Image,LastSeen,Name);
recyclerView.setAdapter(adapter);
layoutManager=new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return view;
}
这是我的AsyncTask类
protected String[] doInBackground(String... params) {
try {
URL url=new URL(URL_Path);
connection= (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer stringBuffer=new StringBuffer();
while ((line=reader.readLine())!=null){
stringBuffer.append(line);
}
String completeJSON=stringBuffer.toString();
JSONArray parentArray=new JSONArray(completeJSON);
String[] Name=new String[parentArray.length()];
String[] ImagePath=new String[parentArray.length()];
for (int i = 0; i <parentArray.length() ; i++) {
JSONObject childObject=parentArray.getJSONObject(i);
String Fname=childObject.getString("First_Name") ;
String Lname=childObject.getString("Last_Name") ;
Name[i]=Fname+" "+Lname;
ImagePath[i]=childObject.getString("Image");
Log.d(TAG,"String Arrays "+Name[i]+" "+ ImagePath[i]);
//GETTING ALL VALUES IN LOG SUCCESSFULLY
}
return Name;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
try {
connection.disconnect();
if (reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String[] strings) {
super.onPostExecute(strings);
}
我在片段父活动的工具栏中调用AsyncTask执行方法
更新:
1)作为@BryanDunlap被要求 onPostExecute (它刚刚实现,其中没有代码)以及如何返回两个数组以获取片段
2)@IrisLouis建议实现界面,这是我如何处理setter和getter但我没有工作
ublic class WrapperClass {
private String[] Name;
private String[] ImagePath;
public String[] getImagePath() {
return ImagePath;
}
public void setImagePath(String[] imagePath) {
ImagePath = imagePath;
}
public String[] getName() {
return Name;
}
public void setName(String[] name) {
Name = name;
}
我如何在AsyncTask中设置它
Name[i]=Fname+" "+Lname;
ImagePath[i]=childObject.getString("Image");
Log.d(TAG,"String Arrays "+Name[i]+" "+ ImagePath[i]);
//LOG RETURNS VALUE OF BOTH ARRAYS
WrapperClass w=new WrapperClass();
w.setImagePath(ImagePath);
w.setImagePath(Name);
在onCreateView中获取片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
....
....
WrapperClass wrapperClass=new WrapperClass();
String[] Name=wrapperClass.getName();
String[] ImagePath=wrapperClass.getImagePath();
Log.d(TAG,Name.toString()+" "+ ImagePath.toString())
//IT RETURNS NULLS
答案 0 :(得分:0)
为您解决方案,可以对其进行测试。
在Asynctask类中创建一个接口回调结果:
public class PageLoader extends AsyncTask<String, Void, String[] results> {
private Context context;
private LoaderListener mLoaderListener;
public PageLoader(Context context, LoaderListener mLoaderListener) {
this.context = context;
this.mLoaderListener = mLoaderListener;
}
public interface LoaderListener {
void onLoadCompleted(String[] results);
void onLoadFailed();
void onPreparing();
}
@Override
protected void onPreExecute() {
mLoaderListener.onPreparing();
}
@Override
protected String[] doInBackground(String... params) {
try {
URL url=new URL(URL_Path);
connection= (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer stringBuffer=new StringBuffer();
while ((line=reader.readLine())!=null){
stringBuffer.append(line);
}
String completeJSON=stringBuffer.toString();
JSONArray parentArray=new JSONArray(completeJSON);
String[] Name=new String[parentArray.length()];
String[] ImagePath=new String[parentArray.length()];
for (int i = 0; i <parentArray.length() ; i++) {
JSONObject childObject=parentArray.getJSONObject(i);
String Fname=childObject.getString("First_Name") ;
String Lname=childObject.getString("Last_Name") ;
Name[i]=Fname+" "+Lname;
ImagePath[i]=childObject.getString("Image");
Log.d(TAG,"String Arrays "+Name[i]+" "+ ImagePath[i]);
//GETTING ALL VALUES IN LOG SUCCESSFULLY
}
return Name;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
try {
connection.disconnect();
if (reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String[] results) {
if (results!= null) {
mLoaderListener.onLoadCompleted(results);
} else {
mLoaderListener.onLoadFailed();
}
}
}
在Activity中执行Asynctask。
在Fragment中,你必须实现LoaderListener和Override三个函数。
例如:YourFragment extends Fragment implements LoaderListener
您可以在onLoadCompleted
前:
@Override
public void onLoadCompleted(String[] results){
//Do something with results
}