Problem with different icons in Listview

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with different icons in Listview

Postby Bertrand31 » Mon Nov 23, 2009 2:03 pm

Hi,

I'm trying to display in a ListView, music files and folders of a directory, and I would like to have different icons for these two.
I've build a database in which I put the result of "ls" . Everything is working great, and thanks to a KEY of my database I can make the difference between folders and music files. Then I can create each row of the ListView with two different functions using two different "row_file.xml" with no problem.

The problem is, that I have the same icon displayed for music files and folder which is the icon of folders !

This is my code

musicfolder_row_directory.xml :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.                                  android:layout_width="wrap_content"
  9.  
  10.                                  android:layout_height="wrap_content">
  11.  
  12.  
  13.  
  14. <LinearLayout  android:layout_width="wrap_content"
  15.  
  16.                         android:layout_height="wrap_content">
  17.  
  18.      
  19.  
  20.    
  21.  
  22. <ImageView android:id="@+id/ImageView03"
  23.  
  24.                      android:layout_width="wrap_content"
  25.  
  26.                      android:layout_height="wrap_content"
  27.  
  28.                      android:src="@drawable/directory">
  29.  
  30. </ImageView>
  31.  
  32.    
  33.  
  34. <TextView android:id="@+id/text4"
  35.  
  36.                  android:layout_width="wrap_content"
  37.  
  38.                  android:layout_height="wrap_content"
  39.  
  40.                  android:textSize="30px"/>
  41.  
  42.  
  43.  
  44. </LinearLayout>
  45.  
  46. </LinearLayout>
  47.  
  48.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


musicfolder_row_music.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.                                 android:layout_width="wrap_content"
  9.  
  10.                                 android:layout_height="wrap_content">
  11.  
  12.  
  13.  
  14. <LinearLayout
  15.  
  16.      android:layout_width="wrap_content"
  17.  
  18.      android:layout_height="wrap_content">
  19.  
  20.    
  21.  
  22. <ImageView android:id="@+id/ImageView03"
  23.  
  24.                      android:layout_width="wrap_content"
  25.  
  26.                      android:layout_height="wrap_content"
  27.  
  28.                      android:src="@drawable/music">
  29.  
  30. </ImageView>
  31.  
  32.    
  33.  
  34. <TextView android:id="@+id/text3"
  35.  
  36.                  android:layout_width="wrap_content"
  37.  
  38.                  android:layout_height="wrap_content"
  39.  
  40.                  android:textSize="30px"/>
  41.  
  42. </LinearLayout>
  43.  
  44. </LinearLayout>
  45.  
  46.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


musicfolder.xml :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.                                  android:orientation="vertical"
  9.  
  10.                                  android:layout_width="fill_parent"
  11.  
  12.                                  android:layout_height="fill_parent">
  13.  
  14.  
  15.  
  16.  
  17.  
  18. <TextView    android:layout_width="fill_parent"
  19.  
  20.                     android:layout_height="wrap_content"
  21.  
  22.                     android:gravity="center_horizontal"
  23.  
  24.                     android:layout_margin="20px"
  25.  
  26.                     android:text="This is a test:  "
  27.  
  28.                     android:textSize="20px"
  29.  
  30.                     android:paddingBottom="40px"/>  
  31.  
  32.    
  33.  
  34.  
  35.  
  36. <ListView android:id="@android:id/list"
  37.  
  38.        android:layout_width="wrap_content"
  39.  
  40.        android:paddingLeft="30px"
  41.  
  42.        android:layout_height="wrap_content"/>
  43.  
  44.  
  45.  
  46. </LinearLayout>
  47.  
  48.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


And this is how I put the results of "ls" in my database and insert rows in my ListView :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.   [ ... ]
  3.  
  4.                             //
  5.  
  6.                            // Open the MusicFolder DataBase
  7.  
  8.                            //
  9.  
  10.                            
  11.  
  12.                             mMusicDbHelper = new MusicFolderAdapter(this);
  13.  
  14.                             mMusicDbHelper.open();
  15.  
  16.                            
  17.  
  18.                             //
  19.  
  20.                             //Get the list of files in the remote server directory
  21.  
  22.                             //
  23.  
  24.                             java.util.Vector files = c.ls(sshRemoteDirectory);
  25.  
  26.                            
  27.  
  28.                             //
  29.  
  30.                             // Only display the name of directories and music files
  31.  
  32.                             //
  33.  
  34.                             int end = (40+ 2*sshUserName.length()) ;
  35.  
  36.                             if(files.size() != 2){
  37.  
  38.                               for(int ii=0; ii<(files.size()-2); ii++){
  39.  
  40.                                       String line = files.elementAt(ii).toString();
  41.  
  42.                                       String filename = line.substring(end);
  43.  
  44.                                  
  45.  
  46.                                       if (line.startsWith("d") && (line.substring(end).startsWith(".") == false)){
  47.  
  48.                                           mMusicDbHelper.createMusic(filename, "a");
  49.  
  50.                                           fillDirectoryData();
  51.  
  52.                                       }
  53.  
  54.                                      
  55.  
  56.                                       if (filename.endsWith("mp3") && (line.startsWith("d") == false )) {
  57.  
  58.                                           mMusicDbHelper.createMusic(filename, "b");
  59.  
  60.                                           fillMusicData();               
  61.  
  62.                                       }
  63.  
  64.                                      
  65.  
  66.                               }
  67.  
  68.                             }                                                    
  69.  
  70.                                            
  71.  
  72.                         } catch (Exception e) {
  73.  
  74.                             System.err.println("Error: "+e.toString());
  75.  
  76.                         }
  77.  
  78.             };                 
  79.  
  80.                        
  81.  
  82.        
  83.  
  84.             private void fillMusicData() {
  85.  
  86.                 // Get all of the rows from the database and create the item list
  87.  
  88.                 mMusicCursor = mMusicDbHelper.fetchAllMusic();
  89.  
  90.                 startManagingCursor(mMusicCursor);
  91.  
  92.                
  93.  
  94.                 // Create an array to specify the fields we want to display in the list
  95.  
  96.                 String[] from = new String[]{MusicFolderAdapter.KEY_FILENAME};
  97.  
  98.                
  99.  
  100.                 // and an array of the fields we want to bind those fields to
  101.  
  102.                 int[] to = new int[]{R.id.text3};
  103.  
  104.                
  105.  
  106.                 // Now create a simple cursor adapter and set it to display
  107.  
  108.                 SimpleCursorAdapter songs =
  109.  
  110.                             new SimpleCursorAdapter(this, R.layout.musicfolder_row_music, mMusicCursor, from, to);
  111.  
  112.                 setListAdapter(songs);
  113.  
  114.             }
  115.  
  116.                
  117.  
  118.            
  119.  
  120.             private void fillDirectoryData() {
  121.  
  122.                 // Get all of the rows from the database and create the item list
  123.  
  124.                 mMusicCursor = mMusicDbHelper.fetchAllMusic();
  125.  
  126.                 startManagingCursor(mMusicCursor);
  127.  
  128.                
  129.  
  130.                 // Create an array to specify the fields we want to display in the list
  131.  
  132.                 String[] from = new String[]{MusicFolderAdapter.KEY_FILENAME};
  133.  
  134.                
  135.  
  136.                 // and an array of the fields we want to bind those fields to
  137.  
  138.                 int[] to = new int[]{R.id.text4};
  139.  
  140.                
  141.  
  142.                 // Now create a simple cursor adapter and set it to display
  143.  
  144.                 SimpleCursorAdapter songs =
  145.  
  146.                             new SimpleCursorAdapter(this, R.layout.musicfolder_row_directory, mMusicCursor, from, to);
  147.  
  148.                 setListAdapter(songs);
  149.  
  150.             }
  151.  
  152.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


I really hope you can help me on this because I don't understand why this is not working !
Thank you!

Bertrand
Bertrand31
Freshman
Freshman
 
Posts: 5
Joined: Wed Nov 18, 2009 11:29 am

Top

Postby Kur1310 » Tue Nov 24, 2009 7:18 am

Hey, u can refer the FileExplorer tutorial by plusminus, on this site..
It's very nice and simple tutorial.
-Kur
Kur1310
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Oct 28, 2009 10:50 am

Postby Bertrand31 » Tue Nov 24, 2009 9:55 am

Hi,

Thank you for your post.
I've already read this, but I thought there might be an easier way to do this...

Furthermore, I'm using a special library ,because I'm trying to make a FileBrowser on a remote device using ssh connexion.
I think I have to wipe out lot of code and start again!!!! Youuuuhouuuuu :D

Anyway I'm making a copy and I hope someone finds an idea soon!

Best Regards,

Bertrand
Bertrand31
Freshman
Freshman
 
Posts: 5
Joined: Wed Nov 18, 2009 11:29 am

Postby Droid123 » Tue Nov 24, 2009 11:31 pm

Hi,

I'm assuming that your database is correct.
It seems to me that you're trying to 2 different cursor objects (music and folder) to the same listadapter. ListActivity only has 1 ListView and 1 listadapter. If you set 2 cursor objects to that listadapter then the data in the ListView will be overwritten, so i suppose that the results that you're getting is logical.

You should make your own Listadapter class by extending the SimpleCursorAdapter class and override the getView() method, like this:

class myAdapter extends SimpleCursorAdapter {
public myAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.rowlistview, null);
}
String name = getItem(position).name;
String details = getItem(position).details;

((TextView)convertView.findViewById(R.id.text1)).setText(name);
((TextView)convertView.findViewById(R.id.text2)).setText(details);

ImageView icon = (ImageView)convertView.findViewById(R.id.imageview);
if(...?)
icon.setImageResource(R.drawable.musicimage);
else
icon.setImageResource(R.drawable.musicfolder);

return convertView;
}
}
Droid123
Experienced Developer
Experienced Developer
 
Posts: 77
Joined: Sat Nov 21, 2009 1:21 pm

Postby Bertrand31 » Sat Nov 28, 2009 8:46 am

Hi,

Thank you,

I didn't understand how everything works, but I'm going to study this!

Best Regards,

Bertrand
Bertrand31
Freshman
Freshman
 
Posts: 5
Joined: Wed Nov 18, 2009 11:29 am

Top

Return to View, Layout & Resource Problems

Who is online

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