2013-02-27 72 views
0

我用2個ListView創建了示例應用程序。我在listview選擇的事件中遇到問題。我想在用戶點擊第一個listview時將數據綁定到第二個ListView。我在Activity類中擴展了'Activity'。我也想改變我目前的列表項目不同的顏色。android listview選擇事件

listviewCatagory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       public void onItemSelected(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 
        arg1.setSelected(true); 
        TextView lblCatagory = (TextView) findViewById(R.id.lblCatagory); 
        lblCatagory.setTextColor(getResources().getColor(
          R.color.White)); 
        //Log.d("Selected Item", Integer.toString(arg2)); 

        Toast.makeText(getApplicationContext(), Integer.toString(arg2), Toast.LENGTH_SHORT).show(); 
       } 

       public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 

       } 

      }); 

回答

1

對於listview使用onItemClickListeneronItemSelectedListener用於紡紗器

+0

感謝阿比納夫的答案 – 2013-02-27 07:15:10

+0

歡迎我的朋友 – 2013-02-27 07:16:11

1

使用drawable提供選擇顏色和其他東西。

item_selected.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_focused="true"> 
    <shape> 
     <solid android:color="#FFFFFF" /> 
    </shape> 
</item> 
<item> 
    <shape> 
     <solid android:color="#666666" /> 
    </shape> 
</item> 

</selector> 

,並用這個你custom layout裏面如下。 !

假設你custom layoutRelativeLayout然後

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/item_selected"> 
+1

感謝chintan讓我檢查這個我用這個選擇是安卓state_selected而不是機器人:state_focused – 2013-02-27 07:01:49

+0

是,還有其他選項可用。你可以根據你的需要使用。我只是舉例說明如何使用'selector'。謝謝。 – 2013-02-27 07:09:39

+0

Chintan我試圖改變我的選擇器,但它沒有工作 – 2013-02-27 07:18:31