get list source

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

get list source

Postby vallis » Wed Nov 05, 2008 4:42 pm

Hi All,
Quick question this time.
Does anyone know if it's possible to find the data source of a list?
e.g. I have a list where each item is made up of lines of text from different files. I'd like to be able to find out the name of the file associated with a list item.
Seems simple but I've not found a way to do it yet.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Top

Postby alpesh_harsoda » Fri Nov 14, 2008 11:52 am

hey this is also i want to do but no idea same like u..........
so if u hv found any way then pls inform me....thnks in advance....
if i found then i will tell u definately..... :) .....
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby MrSnowflake » Fri Nov 14, 2008 1:08 pm

IIRC you can ListView.getAdapter() or .getListAdapter() or something like that.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby vallis » Fri Nov 14, 2008 1:13 pm

Hey,
Not sure if it's the right way to do it but I ended up using one method to create a 2d array of form array[x][2], each 'x' contained the information I needed to display in the list as well as the filename that that information came from.
Then in the method for creating the list I made another array using only the information to be displayed.

So in the method where I need to get access to the file again (for deleting it in my case) I build yet another array, this time using only the filenames from the original. I can then use the getItemAtPosition method of the list to point to the right place in the array of filenames.

Sorry that seems like a horrible explenation, I hope it makes sense.
See code sample below for further detail.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     public void createList(String order)
  3.  
  4.     {
  5.  
  6.         //the 2d array that gets all details including filename
  7.  
  8.         String[][] SMSlist = Functions.fillList((Context)this, (Activity)this, listType);
  9.  
  10.  
  11.  
  12.         //the array with just information to be displayed      
  13.  
  14.         String[] SMSheaders = new String[SMSlist.length];
  15.  
  16.        
  17.  
  18.         //create list from array
  19.  
  20.                 ArrayAdapter messages =
  21.  
  22.                     new ArrayAdapter((Context)this, R.layout.rowview, SMSheaders);
  23.  
  24.                         setListAdapter(messages);
  25.  
  26.     }
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2.  
  3.     protected void delete(Context context, ListView l, int position, String order)
  4.  
  5.     {
  6.  
  7.         //creates the same 2d array as last time
  8.  
  9.         String[][] SMSlist = Functions.fillList((Context)context, (Activity)this, listType);
  10.  
  11.        
  12.  
  13.         //creates an array on just filenames this time
  14.  
  15.         String[] files = new String[SMSlist.length];
  16.  
  17.         for(int p = 0; p < SMSlist.length; p++)
  18.  
  19.         {
  20.  
  21.                 files[p] = SMSlist[p][2];
  22.  
  23.         }
  24.  
  25.        
  26.  
  27.         //this String is the filename at the position that was passed in from list.getSelectedItemPosition()
  28.  
  29.         String filename = files[position];
  30.  
  31.         context.deleteFile(filename);          
  32.  
  33.     }
  34.  
  35.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I hope all that makes sense. I can try to explain further if you need
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Postby MrSnowflake » Fri Nov 14, 2008 1:56 pm

For advanced stuff like you are doing, you should implement your own ListAdapter.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby vallis » Fri Nov 14, 2008 2:40 pm

You're probably right snowflake, that would be far more efficient if the software was running on a device.
The application is only a student project though and it was meant to be more about the SMS protocol (from a networks perspective) than advanced software engineering, although it hasn't really worked out that way.

Luckily for the phone using public my software will never be released outside of the University :D. Issues like this make for a good report as well. It's nice to have a 'ways it could have been better' section in there.
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Top

Postby MrSnowflake » Fri Nov 14, 2008 2:43 pm

I like the section called "What we did before we got this amazing sollution" a lot more :D.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby vallis » Fri Nov 14, 2008 3:04 pm

haha, I can see the academic reference now (MrSnowflake, 2008)
With only four weeks to go before this badboy has to be handed in I think I'll stick to my horrible inefficient code. Maybe I could propose a Masters project "Making what I did for honours actually work well"

Anyway, if you think that part is bad you really should see some of the rest of it. There's all sorts of hacky badness in there!
Cheers,
V.
vallis
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Oct 11, 2008 12:52 pm
Location: Scotland

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 10 guests