How to get View from customised ListView?

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

How to get View from customised ListView?

Postby prajkti.khadse » Fri Apr 24, 2009 11:02 am

Hi all,

I have a Customised ListView which contains ImageView and TextView.

I want to get instance of the TextView in onCreate().

I tried, but m getting NullPointerException….

Can anybody please help me out…..


Here’s the code m working with…






delete_list.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <RelativeLayout
  2.         xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.         android:background="#ffffff"
  7.    >
  8.  
  9.         <ListView
  10.                 android:id="@+id/DELETE_LIST"
  11.                 android:layout_width="fill_parent"
  12.                 android:layout_height="wrap_content"
  13.                 android:choiceMode="multipleChoice"
  14.                 android:clickable="true"
  15.         />
  16.        
  17. </RelativeLayout>      
  18.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


custom_list.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <RelativeLayout
  2.         xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent"
  5.         >
  6.         <ImageView
  7.                 android:id="@+id/LIST_IMAGE"
  8.                 android:layout_width="140px"
  9.                 android:layout_height="70px"
  10.                 android:layout_alignParentLeft="true"
  11.                 android:layout_marginTop="5px"
  12.                 android:src="@drawable/below_image_online"
  13.         />
  14.         <TextView
  15.                 android:id="@+id/LIST_DELETE_ICON"
  16.                 android:layout_width="wrap_content"
  17.                 android:layout_height="wrap_content"
  18.                 android:layout_alignParentRight="true"
  19.                 android:layout_marginRight="5px"
  20.                 android:layout_marginTop="5px"
  21.                 android:text="i"
  22.        
  23.         />
  24. </RelativeLayout>
  25.  
  26.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


And Here’s the Java File


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DeleteList extends Activity {
  2.  
  3.         ListView myList;
  4.        
  5.         @Override
  6.         protected void onCreate(Bundle savedInstanceState) {
  7.                 super.onCreate(savedInstanceState);
  8.                 setContentView(R.layout.delete_list);
  9.                
  10.                 mBuddyList = (ListView) findViewById(R.id.DELETE_LIST);
  11.                 ArrayList<HashMap<String, String>> arrList = new        ArrayList<HashMap<String, String>>();
  12.                 HashMap<String, String> map = new HashMap<String, String>();
  13.                 map.put("Image",R.drawable.image1);
  14.                 map.put("Text","Delete");
  15.                 arrList.add(map);
  16.  
  17.                 map = new HashMap<String, String>();
  18.                 map.put("Image",R.drawable.image2);
  19.                 map.put("Text","Delete");
  20.                 arrList.add(map);
  21.                
  22.                 map = new HashMap<String, String>();
  23.                 map.put("Image",R.drawable.image3);
  24.                 map.put("Text","Delete");
  25.                 arrList.add(map);
  26.  
  27.                 map = new HashMap<String, String>();
  28.                 map.put("Image",R.drawable.image4);
  29.                 map.put("Text","Delete");
  30.                 arrList.add(map);
  31.  
  32.                 SimpleAdapter myAdapter = new SimpleAdapter(this,arrList,R.layout.custom_list,new String[]{"Image","Text"},new int[]{R.id.LIST_IMAGE,R.id.DELETE_ICON});
  33.             mList.setAdapter(myAdapter);
  34.            
  35.             TextView deleteIcon = (TextView) mList.findViewById(R.id.LIST_DELETE_ICON);
  36.                 // here i get deleteIcon as null
  37.                  
  38.         }
  39.  
  40. }
  41.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


please help me out... :cry: :oops:
prajkti.khadse
Developer
Developer
 
Posts: 32
Joined: Thu Mar 19, 2009 1:01 pm
Location: Bangalore

Top

Postby shyjumon.n » Sat Apr 25, 2009 4:46 pm

Hi
I am also facing a similar issue. Did find any solution ?
SMN
shyjumon.n
Freshman
Freshman
 
Posts: 9
Joined: Wed Mar 18, 2009 8:13 am

Postby prajkti.khadse » Mon Apr 27, 2009 4:36 am

Yes, i achieved it by OnItemClickListener.

Here's the sample code.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mList.setOnItemClickListener(new OnItemClickListener(){
  2.  
  3.  
  4.  
  5.                         @SuppressWarnings("unchecked")
  6.  
  7.                         public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
  8.  
  9.                                
  10.  
  11.                                 TextView deleteIcon = (TextView) v.findViewById(R.id.LIST_DELETE_ICON);
  12.  
  13.                                 if(deleteIcon != null)
  14.  
  15.                                 {
  16.  
  17.                                         deleteIcon.setOnClickListener(new OnClickListener(){
  18.  
  19.                                                 public void onClick(View v) {
  20.  
  21.                                                         showDialog(0);
  22.  
  23.                                                 }
  24.  
  25.                                         });
  26.  
  27.                                 } else {
  28.  
  29.                                         Log.i("DELETEICON","Sorryy... NULL!!!!!!!!");
  30.  
  31.                                 }
  32.  
  33.                         }});
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


:)
prajkti.khadse
Developer
Developer
 
Posts: 32
Joined: Thu Mar 19, 2009 1:01 pm
Location: Bangalore

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests