-3

How do i parse JSON in android, that changes its JSON url on clicking.

This is My MainActivity.java;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String URL_DATA ="http://example.com/app_services.php?d=";


    private RecyclerView recyclerView;
    ListView listView;
    private RecyclerView.Adapter adapter;

    private List<ListItem> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.myrecyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        listItems = new ArrayList<>();

        loadRecyclerViewData();

    }

    private void loadRecyclerViewData(){

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading News...");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        progressDialog.dismiss();

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray array = jsonObject.getJSONArray("category");

                            for (int i =0; i<array.length();i++){
                                JSONObject o = array.getJSONObject(i);
                                ListItem item = new ListItem(
                                        o.getString("d"),
                                        o.getString("dirtitle"),
                                        o.getString("folder_image")

                                );

                                listItems.add(item);
                            }

                            adapter = new MyAdapter(listItems,getApplicationContext());
                            recyclerView.setAdapter(adapter);




                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override

                    public void onErrorResponse(VolleyError error) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);



    }
}

In this i want to change the Url of the Json, on clicking. From

http://example.com/app_services.php?d=

http://example.com/app_services.php?d=name

The url's used in this post are not the real one. They are just an example. And these two Urls may or may not contain Different objects inside them.

1 Answers1

0

When you click a item of RecyclerView to add "name" to the tail of original URL, please just set the following code in RecyclerViewAdapter file.

void onItemViewClick (View view) {

   String name = "the name you want"
   String url = URL_DATA+name; //

   /*
    * write the same codes as "loadRecyclerViewData" method, 
    * but just change URL_DATA to url. 
    * If the changed url has the different json format than the original one
    * , you can modify some codes in  "public void onResponse(String response)" method.
    */

}

And the following link has how to add onItemClickListener in RecyclerView. Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?

If you have any more question, please let me know. Thank you.

Community
  • 1
  • 1
Sunhee
  • 101
  • 8
  • i have no idea, how do i do that? – Arjun Martinez May 17 '17 at 14:13
  • Did you handle one item on RecyclerView? In other words, did you add onItemClickListener to items on RecyclerView? @ArjunMartinez – Sunhee May 17 '17 at 14:18
  • `@Override public void onBindViewHolder(ViewHolder holder, int position) { final ListItem listItem = listItems.get(position); holder.mLinearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String NEW_URL = "listItem.getDescription()"; String URL_NEW_DATA = URL_DATA+NEW_URL; } }); }` This is my Adapter class. Is this correct – Arjun Martinez May 17 '17 at 14:25
  • Yes, right. And add some code to below "String URL_NEW_DATA = URL_DATA+NEW_URL; Write the same codes as "loadRecyclerViewData" method, but just change DATA_URL to URL_NEW_DATA. @ArjunMartinez – Sunhee May 17 '17 at 14:28
  • Do you mean that "loadRecyclerViewData" method is not working? – Sunhee May 17 '17 at 14:37
  • I mean on click its working.But not updating the data in URL_DATA in MainActivity class – Arjun Martinez May 17 '17 at 14:40
  • Ya sure.. But shall i add them here? – Arjun Martinez May 17 '17 at 14:55
  • I have shared the codes. Pls check them on ur mail. – Arjun Martinez May 17 '17 at 14:57
  • I got the source. please wait for a moment.^^ – Sunhee May 17 '17 at 14:58
  • I have sent you the modified source files. check it please. – Sunhee May 17 '17 at 15:52
  • I think that you don't know how to add callback to RecyclerView items well. So please check this link http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif. Good luck^^ If you have any question, please let me know. – Sunhee May 17 '17 at 15:56
  • Thank you so much. It refreshs data. But got a problem. It also holds the old data from the url, whick makes the list very long. The old data should have to be refreshed, so that only the clicked values will be shown. – Arjun Martinez May 18 '17 at 02:37
  • Then clean the RecyclerView before putting the new data in it. This might help you http://stackoverflow.com/questions/29978695/remove-all-items-from-recyclerview – Nico May 18 '17 at 06:33
  • @ArjunMartinez That is because the old data are always remained when you add new items to the list. In the callback method, you have to remove all elements of listitems before listitems.add(). Call this method, listitems.clear(), please. – Sunhee May 18 '17 at 07:30
  • Thank you so much.. It Worked like Charm – Arjun Martinez May 18 '17 at 08:30
  • Plz, check a email.^_^ @ArjunMartinez – Sunhee May 18 '17 at 09:47
  • How many apps have you developed. I would like to look into the apps that you have developed – Arjun Martinez May 18 '17 at 09:56
  • I developed preinstalled apps for Smart HDMI Dongle, and a few of apps for smartphone. This is one of apps I built. https://play.google.com/store/apps/details?id=com.innopia.magicsign&hl=ko – Sunhee May 18 '17 at 11:00