karthik
karthik

Reputation: 165

Items in Listview is not clickable android

I manages to to create a activity with dialog theme and add listview with custom ArrayAdapter for using different icons.

Now the problem is ,the items in the listview are not clickable....pls help me to solve this

contact_info_more_options.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:divider="#00CCFF"
        android:dividerHeight="0.2dp">
    </ListView>
    <Button android:id="@+id/btn_cancel"             
            android:layout_width="match_parent" 
            android:layout_height="0.0dip" 
            android:background="@drawable/button_selector"                       
            android:text="Cancel"
            android:textColor="#FFFFFF" 
            android:textSize="15sp"
            android:textStyle="normal"
            android:gravity="center"
            android:layout_weight="1"/>

</LinearLayout>

list_more_options.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="#FFFFFF"
    android:padding="5dip">

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="30dip"
        android:layout_height="30dip"
        android:scaleType="fitXY"
        android:src="@drawable/karthik" 
        android:layout_marginLeft="5dip"

        android:layout_marginRight="5dip"/>

<TextView
    android:id="@+id/txtvw_option"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/list_image"
    android:layout_toRightOf="@+id/list_image"
    android:text="Save as note"
    android:textColor="#181818"
    android:textSize="20dip" />
    </RelativeLayout>


</RelativeLayout>

ContactInfoMoreOption.java

public class ContactInfoMoreOption extends ListActivity{
    ArrayAdapter<String> adapter;
     String[] moreOptions ={ "Save as note", "Call", "SMS", "Send E-Mail"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_info_more_options);
        getListView().setAdapter(new IconicAdapter());  
        StateListDrawable state=new StateListDrawable();
        state.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.gradinet_hover));
        state.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.drawable.gradinet_hover));    
        getListView().setBackgroundDrawable(state);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();


    }
    class IconicAdapter extends ArrayAdapter {
        IconicAdapter() {
          super(ContactInfoMoreOption.this, R.layout.list_more_options, moreOptions);
        }

        public View getView(int position, View convertView,
                            ViewGroup parent) {
          LayoutInflater inflater=getLayoutInflater();
          View row=inflater.inflate(R.layout.list_more_options, parent, false);
          TextView label=(TextView)row.findViewById(R.id.txtvw_option);

          label.setText(moreOptions[position]);

          ImageView icon=(ImageView)row.findViewById(R.id.list_image);

          if (label.getText().equals("Save as note")) {
            icon.setImageResource(R.drawable.note);
        }
          else if (label.getText().equals("Call")) {
              icon.setImageResource(R.drawable.call);
        }
          else if (label.getText().equals("SMS")) {
              icon.setImageResource(R.drawable.sms);
            }
          else {
              icon.setImageResource(R.drawable.mail);
            }

          return(row);
        }
      }

}

Upvotes: 0

Views: 125

Answers (3)

stefana
stefana

Reputation: 2626

Use

l.getItemAtPosition(position);

instead of

getListAdapter().getItem(position);

Upvotes: 1

bturkmen
bturkmen

Reputation: 31

Try this

getListView().setOnItemClickListener(new OnItemClickListener() {

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

//do sth.

} });

Upvotes: 0

iGio90
iGio90

Reputation: 3301

Try with this. Replace:

String selectedValue = (String) getListAdapter().getItem(position);

With:

String selectedValue = (String) l.getItemAtPosition(position);

Upvotes: 0

Related Questions