listview position id to activity

Put your problem here if it does not fit any of the other categories.

listview position id to activity

Postby la3bna » Tue May 10, 2011 6:24 am

Hello

I have some code:
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. lv.setOnItemClickListener(new OnItemClickListener() {
  2.                     @Override
  3.                     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  4.  
  5.                 Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.speiding.no/kart/?krets="+ position));
  6.                 startActivity(browserIntent);
  7.                                 }
  8.                     }
  9.              );
  10.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Is there a way to make use of the position to start other activity?

I mean rework the code here:

Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. lv.setOnItemClickListener(new OnItemClickListener() {
  2.             public void onItemClick(AdapterView<?> parent, View view,
  3.                 int position, long id) {
  4.                
  5.                 if(position == 0) {
  6.                     Intent intent = new Intent(getApplicationContext(), SmafriluftActivity.class);
  7.                     startActivity(intent);
  8.                     }
  9.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


to use position from the listview so that the code for the list activity dosen't have to be long ?

I can't seem to figure it out but is it possible??
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Top

Re: listview position id to activity

Postby la3bna » Tue May 10, 2011 9:00 am

To try and clarify what Im looking to do is have as little code as possible for a list view that will launch other activities. for example if I press an item with the name cheese in the list I want to start the activity called cheese.

So Im wondering if its possible to take the list entry (here cheese) and put in like this:

Code: Select all
Intent intent = new Intent(getApplicationContext(), [stringid]+Activity.class);
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

ListView onitemclick-pass data to next activity

Postby maheser1 » Mon Mar 12, 2012 12:25 pm

Hi,
I need help to develop an app, which displays an listview of name's from the database which keeps on increasing, and when a name is clicked the corresponding details of the name like D.o.B, age, etc.. should be fetched from the database and has to displayed in the next activity. One thing to be satisfied is I don't want to hardcode the name using if or switch case because the name in the listview wil be keep on increasing... can anyone help me out? Thanks in advance..
maheser1
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 12, 2012 11:44 am

Re: listview position id to activity

Postby vandzi » Mon Mar 12, 2012 8:32 pm

la3bna wrote:To try and clarify what Im looking to do is have as little code as possible for a list view that will launch other activities. for example if I press an item with the name cheese in the list I want to start the activity called cheese.

So Im wondering if its possible to take the list entry (here cheese) and put in like this:

Code: Select all
Intent intent = new Intent(getApplicationContext(), [stringid]+Activity.class);

I think it's not possible. Try to use switch with stringid instead. E.g.:
Code: Select all
             switch(stringid){
                  case R.string.first : intent = new Intent(getApplicationContext(), FirstActivity.class);

}
vandzi
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jun 11, 2010 11:31 am
Location: Slovakia

Re: listview position id to activity

Postby nuetronprotron » Wed Mar 14, 2012 6:04 am

Hi la3bna,
I do not know why you wanted do like what you are intended,but if your wish of making the click Listener clean then you can handle that operation in another function like ActivitySwitcher(int position) and there you can handle the intents.

If you do not fancy such type of coding still want to do in differently then you can go with enums or try to use ArrayList.

If you want to use in arraylist then try this:
ArrayList<Class> destination = new ArrayList<Class>();
destination.add(your.class);

// handling intents
Intent i = new Intent(context,destination.get(position));
startActivity(i);


I hope this will help you.
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests