Urgent: ListView icon problem

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

Urgent: ListView icon problem

Postby s1nn0n » Sat Aug 29, 2009 12:07 pm

Hi all,

I would be VERY greatful if you could help me with this one, its literally driving me insane, been trying to do this for 2 days now.

I have a listview, now what I want to do it is, when a position in the listview gets clicked on, it changes the icon for that position, to a drawable named "ok.png" . Simple, :D

Below is the method I use to fill the ListView

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     private void fillData() {
  3.  
  4.         // Get all of the notes from the database and create the item list
  5.  
  6.         Cursor c = mDbHelper.fetchAllNotes();
  7.  
  8.         startManagingCursor(c);
  9.  
  10.  
  11.  
  12.         //String[] from = new String[] { DbAdapter.KEY_TITLE };
  13.  
  14.      
  15.  
  16.         int[] to = new int[] { R.id.text1 };
  17.  
  18.        
  19.  
  20.         // Now create an array adapter and set it to display using our row
  21.  
  22.         SimpleCursorAdapter notes =
  23.  
  24.             new SimpleCursorAdapter(this, R.layout.row, c, from, to);
  25.  
  26.        
  27.  
  28.         setListAdapter(notes);
  29.  
  30.        
  31.  
  32.        
  33.  
  34.        
  35.  
  36.  
  37.  
  38.     }
  39.  
  40.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


and the array that is first declared:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class QuickList extends ListActivity implements View.OnClickListener{
  3.  
  4.         //declares
  5.  
  6.         Button add;
  7.  
  8.         EditText input;
  9.  
  10.         private DbAdapter mDbHelper;
  11.  
  12.         private int mNoteNumber = 1;
  13.  
  14.     private static final int DELETE_ID = Menu.FIRST;
  15.  
  16.     String[] from = new String[] { DbAdapter.KEY_TITLE };
  17.  
  18.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I would REALLY appreciate some help with this.

Thanks in advance.
s1nn0n
Freshman
Freshman
 
Posts: 4
Joined: Wed Aug 26, 2009 10:44 pm

Top

Postby s1nn0n » Sat Aug 29, 2009 2:18 pm

As can be seen from below, when I click on the listview, it sucessfully returns the position, 0, 1, 2 and so forth. But Im stumped on how to proceed to change the icon?

I have filled with question marks below the code in which im unsure about.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         protected void onListItemClick(ListView l, View v, int position, long id)
  4.  
  5.         {
  6.  
  7.                 super.onListItemClick(l, v, position, id);
  8.  
  9.                 Object o = this.getListAdapter().getItem(position);
  10.  
  11.                 String keyword = o.toString();
  12.  
  13.                
  14.  
  15.                
  16.  
  17.                 ImageView icon =(ImageView)????.findViewById(R.id.icon);
  18.  
  19.                
  20.  
  21.                 icon.setImageResource(R.drawable.delete);
  22.  
  23.                
  24.  
  25.                 //test position
  26.  
  27.             new AlertDialog.Builder(this)
  28.  
  29.             .setTitle("Test")
  30.  
  31.             .setMessage("position is: "+position)
  32.  
  33.             .setNeutralButton("Close", new DialogInterface.OnClickListener(){
  34.  
  35.                 public void onClick(DialogInterface dlg,int sumthin) {
  36.  
  37.                         //no nothing
  38.  
  39.                 }
  40.  
  41.             })
  42.  
  43.             .show();
  44.  
  45.                
  46.  
  47.         }
  48.  
  49.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
s1nn0n
Freshman
Freshman
 
Posts: 4
Joined: Wed Aug 26, 2009 10:44 pm

Postby nmc » Sat Aug 29, 2009 3:25 pm

ImageView icon =(ImageView)????.findViewById(R.id.icon);


Use the function-parameter "v" to replace the "?", as it will represent your "R.layout.row".

But you unless you save/restore the icon-state, you will not get this working correctly, because the listview will reuse the views while scrolling.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests