我在我的android ListView中面臨一個奇怪的行爲:我禁用了每行中的複選框。當我點擊一行時,似乎複選框會在啓動下一個活動(點擊的結果)之前返回啓用一小段時間。Android ListView單擊將所有複選框設置爲啓用
下面是以前的點擊和點擊後:
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);
}
感謝您的時間。
我不明白你的程序是做什麼的,你想實現什麼。你能解釋我的想法嗎? –
我在活動中有一個列表視圖,每行都包含一個禁用的複選框。當我點擊一行時,似乎複選框在短時間內啓用(它們的顏色改變)。爲什麼當我點擊我的列表視圖行時,它們是啓用的。 – user1594047
你能給我一個非常小的ArrayList>()的樣本來填充列表嗎?只是一個小樣本 –