Sun
Sun

Reputation: 6888

How to show detail of previous and next list items

In my program i am fetching data from json and storing into arraylist and then populating into ListView, once user do tap on any of the list item showing record in another activity, where i have two buttons, namely:- previous & next, and now i want whenever user do tap on Previous button (need to show previous list item detail) and Next button (need to show next list item detail).....

here is my code i am using to fetch data and to show detail of tapped item

DetailActivity.java:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        Bundle bundle = getIntent().getExtras();
        // add cast to ArrayList<Actors>
        final ArrayList<Actors> arrayList= (ArrayList<Actors>) bundle.getSerializable("information");
        final int currentindex = bundle.getInt("index");
        final Actors actors = arrayList.get(currentindex);

        textName = (TextView) findViewById(R.id.textName);

        for(int i=0; i<arrayList.size(); i++)
        {             
           textName.setText(actors.getName());             
        }

        btnNext = (Button) findViewById(R.id.button1);
        btnPrev = (Button) findViewById(R.id.button2);

        btnNext.setOnClickListener(new OnClickListener() {

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

            }
        });

Upvotes: 2

Views: 2547

Answers (2)

Venu
Venu

Reputation: 353

  int currentindex;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    Bundle bundle = getIntent().getExtras();
    // add cast to ArrayList<Actors>
    final ArrayList<Actors> arrayList= (ArrayList<Actors>)  bundle.getSerializable("information");
      currentindex = bundle.getInt("index");
    textName = (TextView) findViewById(R.id.textName);
    textName.setText(""+arrayList.arrayList.get(currentindex).getName());
    btnNext = (Button) findViewById(R.id.button1);
    btnPrev = (Button) findViewById(R.id.button2);

    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            currentindex=currentindex+1;
            if(currentindex<arrayList.size()){
                 textName.setText(""+arrayList.get(currentindex).getName());
            }
            else{
                currentindex=currentindex-1;
            }


        }
    });

    btnPrev.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            currentindex=currentindex-1;
            if(currentindex>=0){
                 textName.setText(""+arrayList.get(currentindex).getName());
            }
            else{
                currentindex=currentindex+1;
            }
        }
    });

Upvotes: 3

Devendra B. Singh
Devendra B. Singh

Reputation: 304

Just pass the JSON collected arraylist with selected index to next launching activity and than based on the current selection get the -1 and +1 etc position and get the value from your collection to display particulr position information

Do something like below on lciking to launch nre activity

intent.putStringArrayListExtra("contactList ", contactList );
        intent.putExtra("current", pos);

In the next activity Do something like below

ArrayList<String > contactList contactList = intent.getStringArrayListExtra("contactList ");
        int position = intent.getIntExtra("current", 0);
        contactList.get(position);

        //for previous do
        contactList.get(position-1);
        //for next do
        contactList.get(position+1);

Upvotes: 1

Related Questions