can anybody tell pls....

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

can anybody tell pls....

Postby Raghav_s » Mon Oct 12, 2009 10:49 am

Can anybody tell pls.....
I m new to android.....

Here is my question:

example:
|4|2|raghav|4|Roshan|5|raghul|6|sachin

Wat i need means i want to separate this by string tokenizer("|") and store it into separate values....the first number "4"indicates i need 4 list itms should display with name specified above ....the second indicates "2"are unique id for each member like"2"for raghav ,"4" for Roshan and "5"for raghul and "6" for Sachin....

How i can display certain listitem based upon number indicating in first (i.e)"4" above given example...


when i display list it should consists only name not the unique id....if i click the name the particular unique id should display......

Pls its urgent....

Thanks in anvance....
Raghav.S
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Top

Postby padde » Mon Oct 12, 2009 4:26 pm

A rather simple solution.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.android.apis.view;
  3.  
  4.  
  5.  
  6. import java.util.ArrayList;
  7.  
  8. import java.util.List;
  9.  
  10.  
  11.  
  12. import android.app.ListActivity;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.View;
  17.  
  18. import android.widget.ArrayAdapter;
  19.  
  20. import android.widget.ListView;
  21.  
  22. import android.widget.Toast;
  23.  
  24.  
  25.  
  26. class MyItem {
  27.  
  28.     private String title;
  29.  
  30.     private String value;
  31.  
  32.  
  33.  
  34.     public MyItem(String title, String value) {
  35.  
  36.         this.title = title;
  37.  
  38.         this.value = value;
  39.  
  40.     }
  41.  
  42.  
  43.  
  44.     public String getTitle() { return this.title; }
  45.  
  46.     public String getValue() { return this.value; }
  47.  
  48.     public String toString() { return this.title; }
  49.  
  50. }
  51.  
  52.  
  53.  
  54. public class List1 extends ListActivity {
  55.  
  56.     private static final String data = "4|2|raghav|4|Roshan|5|raghul|6|sachin";
  57.  
  58.  
  59.  
  60.     public void onCreate(Bundle savedInstanceState) {
  61.  
  62.         super.onCreate(savedInstanceState);
  63.  
  64.  
  65.  
  66.         String[] temp = data.split("\\|");
  67.  
  68.  
  69.  
  70.         List<MyItem> MyItemList = new ArrayList<MyItem>();
  71.  
  72.         for(int i=1; i < temp.length; i += 2)
  73.  
  74.             MyItemList.add(new MyItem(temp[i+1],temp[i]));
  75.  
  76.  
  77.  
  78.         int resource = android.R.layout.simple_list_item_1;
  79.  
  80.         setListAdapter(new ArrayAdapter<MyItem>(this, resource, MyItemList));
  81.  
  82.     }
  83.  
  84.  
  85.  
  86.     protected void onListItemClick(ListView l, View v, int position, long id) {    
  87.  
  88.         String text = ((MyItem)l.getItemAtPosition(position)).getValue();
  89.  
  90.         Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
  91.  
  92.     }
  93.  
  94. }
  95.  
  96.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

thanks padde

Postby Raghav_s » Tue Oct 13, 2009 5:29 am

Hi Padde thanks for ur kind reply........

Its working good......really Awesome...keep it....
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests