listview click on row goes to next screen


Last Updated:

  1. mindus

    mindus Member This Topic's Starter

    Joined:
    Sep 7, 2010
    Messages:
    9
    Likes Received:
    0
    Hi
    I am working on android project. I have a list view screen where it displays various data fetched form web service. This list view has more than 150 rows. So i implemented pagination and its works fine (display 5 rows).

    I am using simple adapter for this list view. Now i want on click on row it has to collect the data in that row and it should go to new screen. In the new screen click on back button it has to come back to the list view(old position: for eg the list view display 51 - 60 records, once i click on 56th row it has to go to new screen, when i click back button then list view has display 51 - 60 records not 1 - 10 records)

    Any idea about this issue?

    Thanks
    mindus
     

    Advertisement
  2. tencent

    tencent Member

    Joined:
    Oct 1, 2010
    Messages:
    15
    Likes Received:
    0
    well if you are not destroying your activity when you leave it, you could just use intents to load a new activity when they click on a list item.

    So lets assume your intent needs to variables, here is how you would launch this new activity.

    Code (Text):
    1. Intent myIntent = new Intent(getApplicationContext(), NameOfTheActivityToStart.class);
    2. myIntent.putExtra("firstVariableToPass", "Passing a string");
    3. myIntent.putExtra("secondVariableToPass", 65);
    4. startActivity(myIntent);
    Using extras you can attach any needed 'base' variables to intents which can be retrieved easily by the new activity. Some important things to remember. You need to list the activity in your Manifest. To be able to launch activities internal to your application they MUST be in the manifest or you will get an error.

    HTML:
    1. <activity android:name=".NameOfTheActivityToStart"></activity>
    Now in the activity you need to open, in the onCreate you could add the lines to fetch the passed intent extras.

    Code (Text):
    1.  
    2. String firstVar = getIntent().getStringExtra("firstVariableToPass");
    3. int secondVar = getIntent().getIntExtra("secondVariableToPass");
    4.  
    Now for the 2nd part of your question. When you leave an activity android doesn't close it, it just pauses it (onPause). So when you launch your new intent the screen should not change unless in the onPause or onResume (or other override methods) you are clearing the screen. Assuming that you aren't, when the user presses their back button in the new activity they will come back to the original activity and find it in the same state they left it.

    Hope that helps.
     
  3. andywhoa

    andywhoa Well-Known Member

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Overriding the onListItemClick handler is where you can place your code for going to the next screen.


    Code (Text):
    1.     @Override
    2.     protected void onListItemClick(ListView l, View v, [B]int position[/B], long id) {
    3.         super.onListItemClick(l, v, position, id);
    4.  
    5.     // TODO your code here
    6.     }
    When your user clicks on an item, goes to a new screen, and clicks the back button, Android will handle everything for you*.



    On a side note...

    * This assumes your ListActivity is still in memory.

    Simply calling a web-service and storing the returned data in memory is not really good enough. Android reserves the right to reclaim memory from an application if the application is not in the foreground and Android determines that the device is low on resources. When you bring the application back to the foreground, clicking back will go back to the previous Activity, but possibly not the same instance (because, in this case, the Activity would have been reclaimed (read: destroyed) by Android and re-instantiated when the user went back to it. IE: none of your data is in memory and the list will be empty).

    If you don't want to put this much work into it, that is your choice, but a proper implementation of an application that displays the results of a web-service is the following:
    - Start a Service
    - Inside the Service, launch a Thread (you may also choose to use an IntentService, in which case you will not need to worry about this part because the IntentService does this for you)
    - Inside the Thread, hit your web-service
    - Write the resulting data to a database
    - Broadcast a specific message pertaining to this action to the system
    - Your Activity should be registered to hear this broadcast using a BroadcastReceiver, in which case it will invoke a handler inside the Activity which you can use to...
    - Re-query the Cursor. In this kind of application, your ListView would likely use a CursorAdapter for retrieving the data to display. You may also consider using an AsyncQueryHandler to asynchronously query for the Cursor.

    You should also probably understand that SQLite databases do not handle asynchronous I/O operations well; so you would want to wall your database calls (wherever they may be, I use a ContentProvider) with some threading-safety (read: a semaphore).

    As I said, you can choose to not do all this, it is a lot of work, but if your users ever "minimize" your application, receive a phone call while using it, etc, you're opening up doors to problems.
     
  4. mindus

    mindus Member This Topic's Starter

    Joined:
    Sep 7, 2010
    Messages:
    9
    Likes Received:
    0
    Hi
    Thanks for your reply.

    I tried your code.I have the problem like that the list view screen has displayed 41 - 50 records. If i click on 45th record then it will show the next screen, and while clicking on the back button in the next screen then the listview screen shows the records form 1 - 10 records instead of 41- 50.

    Can you please tell me how to rectify this problem?


    Thanks
    mindus
     
  5. tencent

    tencent Member

    Joined:
    Oct 1, 2010
    Messages:
    15
    Likes Received:
    0
    If its losing scroll position Im not sure what else you could do. Unfortunately their is no method to set the scroll position that I am aware of so even if you tried saving their scroll position manually it wouldn't do you any good.

    Not sure its possible but maybe you can set focus to one of the elements in the listview and it'll scroll to it. I seriously doubt that would work but I dont know what else you can even try short of making a custom view that lets you set scroll positions which seems silly. Probably just another case of crappy android elements missing much needed features.
     
  6. andywhoa

    andywhoa Well-Known Member

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Odd.

    Now I'm having this problem and I don't know why. When I go back to the previous Activity (ListActivity) the scroll is back to the top.

    Very frustrating.

    I was messing with layouts. I think this has something to do with it. As far as I can tell, I've removed all my changes, but the problem persists.

    If I find an answer, I'll post it.
     

Share This Page

Loading...