0
public   class   MainActivity   extends   **strong text**

AppCompatActivity {

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

private ListView lv;

ArrayList<HashMap<String, String>> companyList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    companyList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.list_row_xml);

    new GetCompany().execute();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent i = new Intent(MainActivity.this, SingleView.class);

            i.putExtra("venue", venue);
            startActivity(i);
        }
    });
}


private class GetCompany extends AsyncTask<Void, Void, JSONObject> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(MainActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();

    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected JSONObject doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String url = "http://10.0.2.2:6060/api/v1/company/getInfo?limit=10&page=1";
        JSONArray company = sh.makeServiceCall(url);

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

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

                // Getting JSON Array node
                //JSONArray company = new JSONArray(jsonStr);
                // System.out.println("Reached");
                // looping through All Contacts
                for (int i = 0; i < company.length(); i++) {
                    //System.out.println("Reached1");
                    JSONObject c = company.getJSONObject(i);
                    String id = c.getString("id");
                    String name = c.getString("companyName");

                    // Walking Details in  Json object
                    JSONObject walkingDetails=c.getJSONObject("walkingdetails");
                    String date = walkingDetails.getString("walkingdate");
                    String venue = walkingDetails.getString("venu");

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

                    // adding each child node to HashMap key => value
                    companyy.put("id",id);
                    companyy.put("date", date);
                    companyy.put("companyname", name);
                    companyy.put("venue", venue);


                    // adding contact to contact list
                    companyList.add(companyy);
                }
            } 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);
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, companyList,
                R.layout.list_row, new String[]{"date","companyname","venue"}, new int[]{ R.id.date, R.id.companyname, venue});
        lv.setAdapter(adapter);*/
    }
}

second acivity:

public class SingleView extends AppCompatActivity {


EditText txtVenue;

// String[] Venue;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_single_view);

Intent i = getIntent();
    position = i.getExtras().getInt("positi`enter code here`on");
    txtVenue = (EditText) findViewById(R.id.Venue);
 }
} 
Meenal
  • 2,879
  • 5
  • 19
  • 43
B.Thakur
  • 61
  • 1
  • 8

4 Answers4

1

You can not pass directly JSONObject to another activity. But you can convert json to string and pass it. Then in SecondActivity you can convert it to json again.

Start SecondActivity codes in FirstActivity:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("data", json.toString());
startActivity(intent);

Then get this data from SecondActivity:

String data = getIntent().getStringExtra("data");
try {
    JSONObject json = new JSONObject(data);
} catch (JSONException e) {
    e.printStackTrace();
}

Good luck.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

You can achieve by creating your own class by extending the JSONObject and implementing Serializable interface. So that you can pass through the intent.

Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29
0

HashMaps do not preserve ordering.

Better use a model class, it would also be easy to retrieve data.

public class CompanyData {

    public String id;
    public String date;
    public String name;
    public String venue;
}

then change arraylist,

ArrayList<CompanyData> companyList;

then store values,

CompanyData companyy = new CompanyData();

companyy.id = id;
companyy.date = date;
companyy.name = name;
companyy.venue = venue;

companyList.add(companyy);

then implement onitemclicklistener,

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        CompanyData data = companyList.get(position);

        Intent i = new Intent(MainActivity.this, SingleView.class);

        i.putExtra("venue", data.venue);
        startActivity(i);
    }
});

***To get position simply send position value inside onItemClickListener

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        CompanyData data = companyList.get(position);

        Intent i = new Intent(MainActivity.this, SingleView.class);

        i.putExtra("position", position);
        startActivity(i);
    }
});

You can also send all data of a specific position by

CompanyData data = companyList.get(position);
i.putExtra("data", data); //this will pass all data of clicked row
Exigente05
  • 2,161
  • 3
  • 22
  • 42
0

You have 2 options: 1. Convert json to string and pass as string. 2. Make model class for json , make it parcelable. Pass this object to next activity.

seema
  • 991
  • 1
  • 13
  • 27