我設法創建與對話爲主題的活動,並與定製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);
}
}
}
嘗試此String了selectedValue =(字符串)l.getItemAtPosition(位置); – iGio90
嘗試在XML中爲ListView設置'android:clickable =「true」''。見http://developer.android.com/reference/android/view/View.html#attr_android:clickable – Sundeep
u能幫助我改變列表項的背景點擊時......我想這太,但沒有工作 – karthik