How to add a OnItemClickListener to a Custom ListView?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to add a OnItemClickListener to a Custom ListView?

Postby Andyoid » Sun Feb 05, 2012 3:53 pm

Hello, i got a custom made ListVIew, but i cant get a OnItemClickListener to work.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ChooseInjectionActivity extends Activity {
  2.  
  3.     private ListView listView1;
  4.  
  5.     @Override
  6.     public void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.layoutchooseinjection);
  9.        
  10.        
  11.         // Create the Custom View by getting the array of strings from the values,
  12.         // strip it apart and than create the list items
  13.         CustomListView custom_listview_data[] = new CustomListView[CONSTANTS.NUMBER_OF_INJECTIONS];
  14.        
  15.         for (int i = 0; i < (CONSTANTS.NUMBER_OF_INJECTIONS); i++) {
  16.                
  17.                 // Take the Injections String-Array from Values and strip it to its part
  18.             String temp[] = getResources().getStringArray(R.array.injections);
  19.             String[] separated = temp[i].split(";");
  20.            
  21.            
  22.             // Take the Icon Part and get the ID so it can be used for custom view List
  23.             String mDrawableName = separated[CONSTANTS.INJECTIONS_ARRAY_ICON_NUMBER]; //"ic_launcher";
  24.             int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
  25.            
  26.             // Take the Name Part
  27.             String injectionName = separated[CONSTANTS.INJECTIONS_ARRAY_NAME_NUMBER];
  28.            
  29.             //Put everything together to create the Custom View List
  30.                 custom_listview_data[i] = new CustomListView(resID, injectionName+i);
  31.                
  32.                 custom_listview_data[i].setOnItemClickListener(new View.OnClickListener())
  33.                 {
  34.                
  35.                 }
  36.            
  37.         }
  38.  
  39.        
  40.         CustomListViewAdapter adapter = new CustomListViewAdapter(this,
  41.                 R.layout.custom_listview_item_row, custom_listview_data);
  42.        
  43.        
  44.         listView1 = (ListView)findViewById(R.id.listView1);
  45.        
  46.         View header = (View)getLayoutInflater().inflate(R.layout.custom_listview_header_row, null);
  47.         listView1.addHeaderView(header);
  48.        
  49.         listView1.setAdapter(adapter);
  50.     }
  51. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Can someone pls help me here?
Andyoid
Freshman
Freshman
 
Posts: 2
Joined: Sat Feb 04, 2012 8:02 pm

Top

Re: How to add a OnItemClickListener to a Custom ListView?

Postby droidarama » Sun Feb 05, 2012 11:29 pm

Two things, you need to set the setOnItemClickListener to the listview1 itself, and then you need to use the function

public void onItemClick(AdapterView<?> parent, View view, int position, long id)

Inside of your setOnItemClickListener, and you can set things by position on the list, first listitem would be position(0)
droidarama
Freshman
Freshman
 
Posts: 9
Joined: Fri Feb 03, 2012 4:48 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests