2014-01-15 38 views
0

我设法创建与对话为主题的活动,并与定制ArrayAdapter添加列表视图使用不同的图标。项目在列表视图无法点击的Android

现在的问题是,在ListView的项目是不能点击....请帮助我解决这个

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); 
     } 
     } 

} 
+0

尝试此String了selectedValue =(字符串)l.getItemAtPosition(位置); – iGio90

+0

尝试在XML中为ListView设置'android:clickable =“true”''。见http://developer.android.com/reference/android/view/View.html#attr_android:clickable – Sundeep

+0

u能帮助我改变列表项的背景点击时......我想这太,但没有工作 – karthik

回答

1

使用

l.getItemAtPosition(position); 

,而不是

getListAdapter().getItem(position); 
+0

感谢它的工作.... u能帮助点击时我改变列表项的背景......我尝试过这个,但没有工作 – karthik

+0

有关点击改变背景颜色看看[这里](HTTP://计算器.COM /问题/ 4365893 /如何对变化颜色的,Android的列表项上单击,或选择)的问题。 – Nfear

+0

我想以编程方式做...如何做到这一点.. – karthik

0

与此尝试。替换:

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

有了:

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

试试这个

getListView().setOnItemClickListener(new OnItemClickListener() { 

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

//do sth. 

} });