【问题标题】:Items in Listview is not clickable androidListview中的项目不可点击android
【发布时间】:2014-01-15 16:12:38
【问题描述】:

我设法创建了一个带有对话框主题的活动,并添加了带有自定义 ArrayAdapter 的列表视图以使用不同的图标。

现在的问题是,列表视图中的项目不可点击....请帮我解决这个问题

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 = (String) l.getItemAtPosition(position);
  • 尝试在您的 XML 中为 ListView 设置 android:clickable="true"。见developer.android.com/reference/android/view/…
  • 你能帮我在点击时更改列表项的背景吗...我也试过了,但没用

标签: android


【解决方案1】:

使用

l.getItemAtPosition(position);

而不是

getListAdapter().getItem(position);

【讨论】:

  • 谢谢它的工作......你能帮我改变点击时列表项的背景......我也尝试过,但没有用
  • 点击改变背景颜色见this问题。
【解决方案2】:

试试这个。替换:

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

与:

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

【讨论】:

    【解决方案3】:

    试试这个

    getListView().setOnItemClickListener(new OnItemClickListener() {
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
    
    //do sth.
    

    } });

    【讨论】:

      猜你喜欢
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多