-1
public class AndroidJSONParsingActivity extends ListActivity {

    // url to make request
    private static String url = "http://api.androidhive.info/contacts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";

    // contacts JSONArray
    JSONArray contacts = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts
            contacts = json.getJSONArray(TAG_CONTACTS);

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

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(this, contactList,
                R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL },
                new int[] { R.id.name, R.id.email });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String name = ((TextView) view.findViewById(R.id.name))
                        .getText().toString();
                String cost = ((TextView) view.findViewById(R.id.email))
                        .getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SingleMenuItemActivity.class);
                in.putExtra(TAG_NAME, name);
                in.putExtra(TAG_EMAIL, cost);

                startActivity(in);

            }
        });

    }

}

Using this Class am able to print data after Parsing In Listview name and email .

public class SingleMenuItemActivity extends Activity {

    // JSON node keys
    private static final String TAG_NAME = "name";

    Button next;
    Button Previous;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_item);

        Previous = (Button) findViewById(R.id.button1);
        next = (Button) findViewById(R.id.button2);

        Previous.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        // getting intent data
        getdata();
    }

    public void getdata() {

        Intent in = getIntent();

        // Get JSON values from previous intent
        String name = in.getStringExtra(TAG_NAME);

        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        lblName.setText(name);

    }
}

I have apply @ button On For Next and another for previous if click on Next it should display Next id name in SingleMenuItemActivity Class if click on Previous it should display Previous id Name in SingleMenuItemActivity . please tell me how i ll do this . i have tried and doing First time BUt not able to do i don't know how i will call all Ids and create function so that it should work

user2372154
  • 27
  • 3
  • 10

1 Answers1

0

I have applied @ button On For Next and another for previous if click on Next it should display Next id name in SingleMenuItemActivity Class if click on Previous it should display Previous id Name in SingleMenuItemActivity . please tell me how I Will do this.

=> You can fetch previous/next records by doing ++/-- id field, once you have that particular record in terms of Cursor, you can fetch and display particular field value and display the same in particular view.

Or

Second solution is to get all the records at a time and fetch previous/next record by using below methods:

mCursor.moveToNext()  // for next record from cursor
mCursor.moveToPrevious() // for previous record from cursor

By looking at above different solutions, I would say 1st method is easy when you have thousands of records because there way you won't have to manage thousand of records but a single record. Decision can be taken based on the number of records and your requirement.

Update:

After having talk with you in #AndroidDev chatroom, I came to know your doubt exactly, you should have mentioned earlier and it could have saved time.

Anyway, now as you want to implement prev/next in your SingleMenuItemActivity and as you are already having contactList which you have prepared in AndroidJSONParsingActivity, I would suggest you to pass ArrayList<HashMap<String, String>> from AndroidJSNOParsingActivity to SingleMenuItemActivity.

Now, as you are having complete list, you can fetch prev/next item.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • I am new please tell me where i ll change i have tried Much But i am unable create function for Next and previous in second class please put the code – user2372154 May 21 '13 at 12:12
  • Or If You such Type same Example then please tell me so that i can do this – user2372154 May 21 '13 at 12:13
  • @user2372154 you won't get ready made code every time, try something by your own. Debug the code if you are getting any issue. – Paresh Mayani May 21 '13 at 12:14
  • we have to call all Id in second class? – user2372154 May 21 '13 at 12:16
  • How i can Fetch complete in Another class i alredy Mention in question am having Problem to fetach complte in another class and create fucantion for that so that once i ll call that funcation with index+1 next and index-1 previous – user2372154 May 22 '13 at 07:33
  • @user2372154 Have you gone through the link given above and have you understood description I have given above? I said pass complete list to your SingleMenuItemActivity and get list there. – Paresh Mayani May 22 '13 at 07:38
  • please wait am applying this http://stackoverflow.com/questions/6355787/how-to-pass-arraylisthashmapstring-stringfrom-one-activity-to-another – user2372154 May 22 '13 at 07:44