2015-04-27 34 views
1

我在我的android ListView中面臨一個奇怪的行爲:我禁用了每行中的複選框。當我點擊一行時,似乎複選框會在啓動下一個活動(點擊的結果)之前返回啓用一小段時間。Android ListView單擊將所有複選框設置爲啓用

下面是以前的點擊和點擊後: Before click Just after click

stationsfavoritesadapter.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:descendantFocusability="blocksDescendants"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/listviewborder"> 

     <CheckBox 
      android:id="@+id/favorite_checkbox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="?android:attr/starStyle" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:enabled="false" 

      /> 

     <ImageView 
      android:id="@+id/weatherimage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/favorite_checkbox" 
      /> 

     <TextView 
      android:id="@+id/StationNameText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/weatherimage" 
      android:layout_alignTop="@id/weatherimage" 
      android:textStyle="bold" 
      /> 

     <TextView 
      android:id="@+id/temperatureText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/weatherimage" 
      android:layout_below="@id/StationNameText" 
      /> 

     <TextView 
      android:id="@+id/trailsText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/StationNameText" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="5dp" 
      /> 



    </RelativeLayout> 

</RelativeLayout> 

這裏是適配器類與getView()方法:

public class StationsFavoritesAdapter extends StationsAdapter{ 

    public StationsFavoritesAdapter(StationsManager stations) 
    { 
     _stationManager = stations; 
     _data = new ArrayList<Map.Entry<String, Station>>(); 
     _data.addAll(_stationManager.GetFavorites().entrySet()); 

     _stationFilterList = _data; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     final View result; 
     if (convertView == null) 
     { 
      result = LayoutInflater.from(parent.getContext()).inflate(R.layout.stationsfavoritesadapter, parent, false); 
     } 
     else 
     { 
      result = convertView; 
     } 

     final Map.Entry<String, Station> station = getItem(position); 

     ((TextView) result.findViewById(R.id.temperatureText)).setText(station.getValue().get_temperature() + "°C"); 
     ((TextView) result.findViewById(R.id.StationNameText)).setText(station.getKey()); 
     ((TextView) result.findViewById(R.id.trailsText)).setText(station.getValue().get_trails().first + "/" + station.getValue().get_trails().second); 

     CheckBox favoriteBox = (CheckBox)result.findViewById(R.id.favorite_checkbox); 

     favoriteBox.setChecked(true); 


     ImageView image = (ImageView)result.findViewById(R.id.weatherimage); 

     if(station.getValue().get_weather().equals("sunny")) 
     { 
      image.setImageResource(R.mipmap.sunnyicon); 
     } 
     else if(station.getValue().get_weather().equals("storm-clouds")) 
     { 
      image.setImageResource(R.mipmap.snowicon); 
     } 
     else 
     { 
      image.setImageResource(R.mipmap.cloudyicon); 
     } 

     return result; 
    } 


} 

和ListView單擊事件:

@Override 
public void onListItemClick(ListView parent, View w, int position, long id) 
{ 
    String stationName = adapter.getItem(position).getKey(); 
    Intent intent = new Intent(this, StationActivity.class); 
    Bundle b = new Bundle(); 
    b.putString("Station name", stationName); 
    intent.putExtras(b); 

    startActivity(intent); 
} 

感謝您的時間。

+0

我不明白你的程序是做什麼的,你想實現什麼。你能解釋我的想法嗎? –

+0

我在活動中有一個列表視圖,每行都包含一個禁用的複選框。當我點擊一行時,似乎複選框在短時間內啓用(它們的顏色改變)。爲什麼當我點擊我的列表視圖行時,它們是啓用的。 – user1594047

+0

你能給我一個非常小的ArrayList >()的樣本來填充列表嗎?只是一個小樣本 –

回答

0

您的getView函數被稱爲eveytime應用程序正在繪製列表視圖。我想你想要做的是做你的列表視圖onclicklistener什麼然後設置複選框以真實存在,而不是在getview

使用此代碼在您的活動或片段

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



       } 
      }); 
+0

不應該總是檢查盒子,並且在調試時檢查getView是否在點擊後不被調用。所以點擊不應該改變複選框啓用狀態。 – user1594047

+0

這是爲什麼在你的getView中。 favoriteBox.setChecked(真); –

+0

我希望他們永遠是真實的和禁用。而已。 – user1594047

0

你可以試試將複選框設置爲在適配器中的getView方法內禁用,在您選中該框之後:

@Override 
    public View getView(int position, View convertView, ViewGroup parent){ 

    CheckBox favoriteBox =(CheckBox)result.findViewById(R.id.favorite_checkbox); 

    favoriteBox.setChecked(true); 
    favoriteBox.setEnabled(false); 
+0

它做了同樣的結果我已經嘗試過了。 – user1594047

相關問題