Android-RecyclerView Set Image And Text From JSON

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 6

Aug 5, 2016 4:57:53 AM via Website

Hi i am trying to make RecyclerView List From JSON which is sending Image URL and Name (Only these i need to display to RecyclerView For Now). I tried Google and Stack From past 2 days but Still No Solution To Set Text and Images From JSON to RecyclerView Most of the tutorials use Volley or Other Library and They use AsyncTask in Same Class (which solve data passing problem ). Which I am not using

But my condition is different then most of the solution

1) I am calling AsyncTask.Execute Method From Parent Activity of Fragment

2) My AsyncTask Class is not in same class where i am calling AsyncTask.Execute Method.

3) I am trying get values in Fragment from AsyncTask which always return Null even after setting setter and getter method (Hard time in transfer Images[] and Name[] to Fragment)

4) As i m getting Image Path in String[] then how to set path into Recycler View

I have successfully made recyclerView with local images and text Stored in Drawable Folder and In String Array(Values-->String.xml) and its working fine

Here is my Classes

***From Where I am Calling AsyncTask.Execute Method

  public class Navigation_Drawer extends AppCompatActivity {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //Handle Item Selection
    switch (item.getItemId()){
        case R.id.action_websearch:    
            Toast.makeText(getBaseContext(), "You have Clicked WebSearch", Toast.LENGTH_LONG).show();
            break;
        case R.id.parseJson:
             new JsonParsing().execute();//AYSNCTASK EXECUTE
    }

Then That Aysnctask Execute Called This Class

public class JsonParsing extends AsyncTask<String,Void,String[]> {

private String URL_Path="example/test/Android/Data/showUers.php";
private static final String TAG="JSON PARSER**";
HttpURLConnection connection;
BufferedReader reader;
private Context context;

@Override
protected String[] doInBackground(String... params) {
try {
URL url=new URL(URL_Path);
connection= (HttpURLConnection) url.openConnection();
connection.connect();
Log.d(TAG,connection.toString());

    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]);
        //THIS LOG SUCCESSFULLY SHOWING RESULT IN LOG


    }

    return Name; // CONFUSE IN HOW TO RETURN 2 ARRAY STRING NAME AND IMAGE

} 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) {
//SOMETHING TODO WITH RESULTS
}
}

But my condition is different then most of the solution

1) I am calling AsyncTask.Execute Method From Parent Activity of Fragment

2) My AsyncTask Class is not in same class where i am calling AsyncTask.Execute Method.

3) I am trying get values in Fragment from AsyncTask which always return Null even after setting setter and getter method (Hard time in transfer Images[] and Name[] to Fragment)

4) As i m getting Image Path in String[] then how to set path into Recycler View

Here is my Classes

***From Where I am Calling AsyncTask.Execute Method.

public class FriendsList extends Fragment {

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] Name;
TypedArray Image;

public static final String TAG="Friends List******";

@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);

    Name=getResources().getStringArray(R.array.left_Drawer_Menu);
    Image=getResources().obtainTypedArray(R.array.user_image);

    recyclerView.setHasFixedSize(true);
adapter=new RecyclerFriendListAdapter(getActivity(),Name,Image);
    recyclerView.setAdapter(adapter);
    recyclerView.setAdapter(adapter);
    layoutManager=new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);

    return view;
}

}

Recycler Adapter CLass

public class RecyclerFriendListAdapter extends RecyclerView.Adapter<RecyclerFriendListAdapter.ViewHolder> {

String[] Name,Distance,LastSeen;
TypedArray Image;
Context context;
static final String TAG="FRIEND LIST ADAPTER**";

public RecyclerFriendListAdapter(Context context,String[] name,TypedArray image) {
this.context = context;
//Distance = distance;
Image = image;
//LastSeen = lastSeen;
Name = name;
}

@Override
public RecyclerFriendListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater= (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.listview_item_recycler,null);
Log.d(TAG,"Constructor Calling");
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerFriendListAdapter.ViewHolder holder, int position) {
Log.d(TAG,"Holder "+position);
holder.UserImage.setImageResource(Image.getResourceId(position,0));
holder.Name.setText(Name[position]);
holder.Distance.setText(Distance[position]);
holder.LastSeen.setText(LastSeen[position]);
}

@Override
public int getItemCount() {
return Name.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
TextView Distance,LastSeen,Name;
ImageView UserImage;
Context context;

public ViewHolder(View v) {
    super(v);
    Log.d(TAG,"View Holder");

    Name= (TextView) v.findViewById(R.id.User_Name_Item);
    UserImage= (ImageView) v.findViewById(R.id.UserImage);
}

}

know i am doing some mistakes in code But I need to do These things

Get Images And Names String Array from doinbackground then get these Values in Fragment and send both array to RecyclerView Adapter to show in recyclerview List.

Its Little Advance Question and only Experts can solve this so please help me resolve this issue