-2

I am trying to create a list view with only "name" in the parent list (MainActivity) and pass the remaining "email" and "mobile" to another activity (SingleContactActivity) along with the "name". How can I achieve that? Now my MainActivity listview is showing all details, name, email, mobile, I know I have to delete something from here. My second activity result is OK. But MainActivity must display the list with "name" only. For this I need to make changes to the MainActivity code. The issue is that I am new to JAVA and Android programming. I want your help showing exactly which line to delete from MainActivity so it show only "name" in the list, and pass the other two parameters "email" and "mobile to the second activity along with the "name".

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();

    private ProgressDialog pDialog;
    private ListView lv;

    // URL to get contacts JSON
    private static String url = "http://api.androidhive.info/contacts/";

    ArrayList<HashMap<String, String>> contactList;

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

        contactList = new ArrayList<>();

        lv = (ListView) findViewById(R.id.list);

        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    JSONArray contacts = jsonObj.getJSONArray("contacts");

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString("id");
                        String name = c.getString("name");
                        String email = c.getString("email");
                        String address = c.getString("address");
                        String gender = c.getString("gender");

                        // Phone node is JSON Object
                        JSONObject phone = c.getJSONObject("phone");
                        String mobile = phone.getString("mobile");
                        String home = phone.getString("home");
                        String office = phone.getString("office");

                        // tmp hash map for single contact
                        HashMap<String, String> contact = new HashMap<>();

                        // adding each child node to HashMap key => value
                        contact.put("id", id);
                        contact.put("name", name);
                        contact.put("email", email);
                        contact.put("mobile", mobile);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, contactList,
                    R.layout.list_item, new String[]{"name", "email",
                    "mobile"}, new int[]{R.id.name,
                    R.id.email, R.id.mobile});

            lv.setAdapter(adapter);
        }
    }
}
Nodirbek
  • 3
  • 4
  • Possible duplicate of http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android and http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – Justin Mitchell Nov 24 '16 at 06:14
  • Use intent to pass the data. – Sagar Gangawane Nov 24 '16 at 06:15
  • I can't see any code that's starting the second activity. Anyways, you can pass your data in the intent bundle. – Pravin Sonawane Nov 24 '16 at 06:15
  • create a custom adapter and expose a method from the listview to the calling activity – SaravInfern Nov 24 '16 at 06:16
  • There's no on click handlers and there's no intent creation. – Justin Mitchell Nov 24 '16 at 06:16
  • Here is the second activity, I am using this stackoverflow for the first time, and quite impressed with such a quite responses. Thank you all. http://www.codeforge.com/read/252789/SingleContactActivity.java__html – Nodirbek Nov 24 '16 at 06:29
  • Dear SaravInfern, I am new to JAVA so I would really appreciate if you could help me in creating a custom adapter. – Nodirbek Nov 24 '16 at 07:22

2 Answers2

1

you can save data in bundle and carry it on next activity. or u can also use shared preference to save data temporarily and next activity you get the data from shared preference and then clear it. https://www.tutorialspoint.com/android/android_shared_preferences.htm

Aashutosh Kumar
  • 183
  • 4
  • 12
0

Try to go in second activity using intent.and pass your variables to display in second activity like this:

use this in onPostExecute function or any other event what u want

Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("name", namestringvariable);
intent.putExtra("email", emailstringvariable);
startActivity(intent);
finish();

and in second activity

Bundle bundle = getIntent().getExtras();
String name= bundle.getString("name");
String email= bundle.getString("email");
Adi
  • 400
  • 8
  • 25
  • Dear Adi, I have the code you wrote in my second activity. I don't have to make any chages to the second activity the issue is with the first. Now my MainActivity listview is showing all details, name, email, mobile, I know I have to delete something from here. My second activity result is OK. But MainActivity must display the list with "name" only. I want your help showing exactly which line to delete from MainActivity so it show only "name" in the list, and pass the other two parameters "email" and "mobile to the second activity along with the "name". – Nodirbek Nov 24 '16 at 07:16
  • From where U get this "R.id.email" ? – Adi Nov 24 '16 at 08:32