2

I have 2 activities, 1st containing the list view, and when user click on that list view's list item, the detail view activity will be launched (2nd activity).

Now on 2nd activity, I have 2 buttons, NEXT button and PREVIOUS button, so when user click on NEXT button, the next list item from the 1st activity must be shown. And on clicking previous button, the previous list item must be shown.

I have extended ArrayAdapter on 1st activity, to display the custom list item. And on adapter, there are 2 items, i.e. 2 textviews.

And when user clicks and 2nd activity is launched, those 2 textview's details will be shown in detail. And to make the navigation through the items, going back to the 1st activity, and then again 2nd activity is launched per item, that is hectic scenario for user, so I want to navigate the items on detail view activity itself. So what I am supposed to do?

What I have done so far is, I have extended the ArrayAdapter, so from getView() method, I have taken the position value and then on 2nd activity I have done is,

 next = (ImageView) findViewById(R.id.next);
    previous = (ImageView) findViewById(R.id.previous);

    next.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            position++;

            Log.e("position", String.valueOf(position));

        }
    });

    previous.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            position--;

            Log.e("position", String.valueOf(position));

        }
    });

But I don't know what should I do next?

The position value is taken from 1st activity, with intent.putExtra("position", position) and fetched on 2nd activity using intent.getStringExtra("position").

I am not even able to understand this and that , being just theoretical. Thanks in advance.


EDIT-1:

I have custom ArrayAdapter in 1st activity, and on onclickListener, I have applied,

 convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(fActivity.this, sActivity.class);

                intent.putExtra("txt1", "name");

                intent.putExtra("txt2", "address");

                intent.putExtra("position", position);

                startActivity(intent);
            }
        });

this position is retrieved from

public View getView(final int position, View convertView, ViewGroup parent) {
....
}

this method, is in

 class homeAdapter extends ArrayAdapter<list>

as I have written before on this same panel. And now on 2nd activity I have to get txt1 and txt2 on 2nd activity with next and previous buttons.

Community
  • 1
  • 1

4 Answers4

0

For this you have to keep your Array/List global so that both activity can access it. You can do this by passing whole array/list to next activity by bundle also.

You have to do followings:

  1. Put clicked position and whole array/list in bundle and send it to second activity.
  2. Get the current position and array/list from bundle and show the detail of current position.
  3. Increase the position on click of Next button, and get the item of this position from list.array and show the detail of this position.
  4. Decrease the position on click of Previous button, and get the item of this position from list.array and show the detail of this position.
Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
0

It would have been really helpful if you would have pasted the code you used to populate your ListView. what kind Object you are using to create the List? none the less, here is some code and approach. I am assuming a couple of things here, if they do not match your code, comment on the answer.

  1. Create an ArrayList, now this can be as simple as a String ArrayList<String> or some other object that you want, like ArrayList<Items> // sample Array<Items> itemsList = new ArrayList<Items>();

  2. Now in your itemClickListener on the list view, pass the position and the whole array list to your 2nd activity using Intents.

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

    @override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
        Items item = (Items) parent.getItemAtPosition(position);
        Intent startDetailActivity = new Intent(Activity1.this, Activity2.class);
        startDetailActivity.putExtra("position", position);
        startDetailActivity.putExtra("itemList", itemList);
        startActivity(startDetailActivity);
    }
    

    });

  3. In your activity 2, resolve the intent data:

    Intent i = this.getIntent(); int clickedPosition = i.getIntExtra(position, 0); ArrayList<Items> itemsList = i.getSerializableExtra("itemList");

  4. Set data on your text views by getting the object at position in the itemsList:

    textView1.setText(itemsList.get(clickedPosition).getSomeText()); textView2.setText(itemsList.get(clickedPosition).getSomeOtherText());

And lastly, when you click the next & previous buttons on your activity 2, then increase or decrease the size of the clickedPosition variable, so that it points to the right object in the list. Just be sure that the index you are requesting from the array list is valid, otherwise you will get an index out of bounds exception. This can be done by checking the size of the array list before requesting for the object at that index.

Now, this answers assumes a lot of things, like you have an array list of some object. The variables in those objects have at least getter methods, and that the class in which these variables are implements a Serializable interface. If you have any trouble implementing this code, put it in comments. I'll be happy to help in any way i can.

EDIT-1:

Now, I guess you were able to resolve the intent in your sActivity, but you are facing problems with displaying data on sActivity from the position and List. So, firstly just make sure u have passed the clicked position and also the entire list in the intent. Resolve them in the sActivity. Now, to display data in sActivity from the list, write the below lines of code(After resolving intent on sActivity).

yourTextView.setText(yourList.get(clickedPosition).getSomething());
yourTextView2.setText(yourList.get(clickedPosition).getSomeOtherThing());

Don't be confused by getSomething(), it is just an illustration. You should get the data what you want to display here. I have not seen the list you are making, so i've put this code. Now, to deal with next & previous button clicks. Simply add to clicked position variable on next click like:

nextButton.setOnClickListener(new View.OnClickListener() {
@Override
        public void onClick(View v) {
           clickedPosition++;
yourTextView.setText(yourList.get(clickedPosition).getSomething());
yourTextView2.setText(yourList.get(clickedPosition).getSomeOtherThing());

        }
    });

On the previous button onClick, just decrement the clickedPosition variable like:

clickedPosition--;
//set text views here then

This should do it my friend.

vibhor_shri
  • 374
  • 2
  • 12
  • what if the use of the link [bundle use](http://stackoverflow.com/a/14936846/5738881) and use bundle as mentioned, then is it the right solution? As told by @Ashish_Tiwari [solution for this issue](http://stackoverflow.com/a/37266950/5738881) –  May 19 '16 at 11:15
  • @vibhor_soni have you got the other way out after my edit? –  May 20 '16 at 06:54
  • @JackSmith, saw your edit just now, Its basically the same thing i explained earlier. You have got your key value pairs in intent. Resolve them in the sActivity as i explained earlier. U r done then as far as text1 and text2 are concerned. As for the list itself, u need to add another extra to your intent, the ArrayList from which you are showing the list view. Resolve it in the sActivity and then, when u get a next and previous click event, add or subtract the position and show data using the list. If u r still not clear, paste the entire code. But, give this a try first. – vibhor_shri May 20 '16 at 07:54
  • @JackSmith, as for the question regarding bundle. You can also use that, just that you will need to create a bundle object and set data to it first. Add that bundle to you intent. Then resolve the bundle in the sActivity, and get items from the bundle. – vibhor_shri May 20 '16 at 07:56
  • I can't pass `itemList` in `intent.putExtra`.. and this solution is not working, because, even if increment or decrements in the value of `position`, I am not able to use this method as I am not sure how to put txt1, txt2 and position value in list and get the same list in `sActivity`.. –  May 20 '16 at 14:30
  • @JackSmith, dude... i vl put up another answer soon. Check that, that would cover everything. – vibhor_shri May 21 '16 at 14:42
  • @JackSmith, check the updated answer now. Also, if you are not able to pass the itemList in Intent as an extra, make sure that the class from which it is made is Serializable. – vibhor_shri May 23 '16 at 10:08
0

If you have static lists and do not want to change the list data dynamically, the most simple thing would be to store the list data in your strings.xml file like

<string-array name="textview1_items> 
<item>head1</item>
<item>head2</item>
<item>head3</item>
.
.
.
</string-array>

likewise for textview 2 items.

and then retrieve the list items from these string arrays in both your list adapter and in your second activity.

To access these arrays from java, you need to do the following :

String[] headers = context.getResources.getStringArray(R.array.textview1_items); //use context in adapter and this in second activity

Now when the user clicks list item just pass the position of selected item to second activity and retrieve the information of that index from your string arrays.

Hope it helps! In case of any doubts, please ask!

0

You simply just need to do,

in 2nd activity,

str1 = fActivity.listAdapter.getItem(position).data1;
str2 = fActivity.listAdapter.getItem(position).data2;

and in 1st activity,

public static ArrayAdapter<List> listAdapter;

and there you go, you can have your answer.

Cheers!

RUTURAJ Raval
  • 133
  • 5
  • 16