2014-01-15 45 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. 

} });

相關問題