Android ListView单击将所有复选框设置为启用

时间:2015-04-27 22:19:50

标签: android 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);
}

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

您的getView函数被称为eveytime,应用程序将绘制列表视图。我想你想要做的是在列表视图上执行onclicklistener,然后将复选框设置为true,而不是在getview中

在您的活动或片段中使用此代码

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



                }
            });

答案 1 :(得分: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);