初始状态"已检查"对于ListView中的自定义可检查元素

时间:2014-03-11 12:16:40

标签: android listview android-linearlayout android-selector

我正在尝试使用自定义“可检查”元素构建清单。我能够显示列表,选中复选框(它是一个ImageView)并将每个复选框的状态保存在数据库中。

不幸的是,当我打开一个清单时,我想用一些选中的复选框来初始化清单,但我不能。它不起作用。


截图

当我打开清单时,这是我的屏幕:

android screen that display a checklist with every checkbox unchecked

图例 Android屏幕显示核对清单,每个复选框都未选中

我使用方法.setEnabled(true / false)直观地显示有不同的状态(用于调试目的)。

基本上,当我勾选一个复选框作为用户时,它看起来像这样:

android screen with a checkbox checked

图例 Android屏幕,选中复选框


层次

  • SRC
    • com.checkit.app(package)
      • CheckableLinearLayout.java
      • TasksActivity.java
      • TasksAdapter.java< =(我认为问题可以在这个文件的某处解决)
      • [...]
  • RES
    • 绘制
      • ic_hideable_item.xml
    • 抽拉-MDPI
      • btn_check_off.png
      • btn_check_on.png
      • [...]
    • 布局
      • activity_tasks.xml
      • task_items.xml
      • [...]

代码

TasksAdapter.java

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author tony
 *
 */
public class TasksAdapter extends BaseAdapter {


    /* ************************************* properties */

    // list of items
    private ArrayList<Task> mItemsList;

    // context
    private Context mContext;

    // a single task
    private Task taskItem = null;


    /* ************************************* contructors */

    /**
     * @param context
     * @param item
     */
    public TasksAdapter(Context context, ArrayList<Task> pTasksItems) {
        this.mContext = context;
        this.mItemsList = pTasksItems;
    }


    /* ************************************* override methods */

    @Override
    public int getCount() {
        return mItemsList.size();
    }

    @Override
    public Task getItem(int position) {
        return mItemsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * Display the view of the task of a checklist
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // if we access for the first time : we have to user LayoutInflater
        if (convertView == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(R.layout.task_items, null);
        }

        // get the font size (from the SharedPreferences)
        String fontSizeSetting = MainActivity.getmFontSize();

        // set the text on the tasks
        //CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.task);
        com.checkit.app.CheckableLinearLayout checkboxGlobal = (com.checkit.app.CheckableLinearLayout) convertView.findViewById(R.id.taskGlobalCheckbox);
        ImageView checkboxImg = (ImageView) convertView.findViewById(R.id.taskImageCheckbox);
        TextView checkboxTxt = (TextView) convertView.findViewById(R.id.task_text);

        // get the Task
        taskItem = (Task) mItemsList.get(position);

        // is this task done ?
        // .getIsTick values are 0 (false) or 1 (true)
        if (taskItem.getIsTick() == 1) {
            // the task is done, so tick the checkbox
            Log.d("TEST", "TasksAdapter getView() is getIsTick");

            // TODO I should do something here to tick the box. But what should I do ??
            checkboxGlobal.setChecked(true); // I go to the method, but this do nothing ??
            checkboxGlobal.setEnabled(true); // just to show that it's well working


            // INFO : below are a lot of tries, but no one works properly
            //StateListDrawable states = new StateListDrawable();
            //Drawable drawable = convertView.getResources().getDrawable(R.drawable.ic_hideable_item);
            //drawable.setState(new int[] {android.R.attr.state_checked});
            //states.addState(new int[] {android.R.attr.state_checked}, drawable);
            //states.setState(new int[] {android.R.attr.state_checked});
            //checkboxImg.setImageDrawable(states);
            //checkboxImg.setImageDrawable(drawable);

        } else {
            // the task is undone, so untick the checkbox
            Log.d("TEST", "TasksAdapter getView() is not getIsTick");

            checkboxGlobal.setChecked(false);
            checkboxGlobal.setEnabled(false);

        }

        // set the text on the TextView
        checkboxTxt.setText(taskItem.getName());

        // change the font size
        checkboxTxt.setTextSize(Float.parseFloat(fontSizeSetting));

        // return the view
        return convertView;
    }

}

CheckableLinearLayout.java

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Checkable;
import android.widget.LinearLayout;

/**
 * File based on the official example (cf. http://developer.android.com/samples/CustomChoiceList/index.html )
 * 
 * This is a simple wrapper for {@link android.widget.LinearLayout} that implements the {@link android.widget.Checkable}
 * interface by keeping an internal 'checked' state flag.
 * <p>
 * This can be used as the root view for a custom list item layout for
 * {@link android.widget.AbsListView} elements with a
 * {@link android.widget.AbsListView#setChoiceMode(int) choiceMode} set.
 */
public class CheckableLinearLayout extends LinearLayout implements Checkable {


    /* ************************************* properties */

    // android state for "checked"
    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};

    // default state : FALSE = every checkbox are untick
    private boolean mChecked = false;


    /* ************************************* contructors */

    /**
     * @param context
     * @param attrs
     */
    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    /* ************************************* override methods */

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if (isChecked()) {
            Log.d("TEST ", "CheckableLinearLayout.onCreateDrawableState("+extraSpace+") (isChecked TRUE)");
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }

        return drawableState;
    }


    /* ************************************* aspects methods */

    /**
     * Is this checked ?
     */
    public boolean isChecked() {
        Log.d("TEST ", "CheckableLinearLayout.isChecked()");
        return mChecked;
    }


    /**
     * Set the item as checked (if it's not yet)
     */
    @Override
    public void setChecked(boolean b) {
        Log.d("TEST ", "CheckableLinearLayout.setChecked()");

        // close if it's still the same state
        if (b == mChecked) {
            return;
        }

        Log.d("TEST ", "CheckableLinearLayout.setChecked() inside");

        // modify the state
        mChecked = b;
        // refresh the drawable state
        refreshDrawableState();
    }


    /**
     * Toggle current state
     */
    public void toggle() {
        Log.d("TEST ", "CheckableLinearLayout.toggle()");
        setChecked(!mChecked);
    }

}

ic_hideable_item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true" 
        android:drawable="@drawable/btn_check_on" 
        />
    <item 
        android:state_checked="false" 
        android:drawable="@drawable/btn_check_off" 
        />
</selector>

activity_tasks.xml

<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/listTasks"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="multipleChoice"
        />
</LinearLayout>

task_items.xml

<?xml version="1.0" encoding="utf-8"?>
<com.checkit.app.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/taskGlobalCheckbox"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:minHeight="?android:listPreferredItemHeight"
    android:orientation="horizontal"
    >

    <ImageView 
        android:id="@+id/taskImageCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:duplicateParentState="true"
        android:src="@drawable/ic_hideable_item"
        android:contentDescription="@string/img_alt_checkbox"
        />

    <TextView 
        android:id="@+id/task_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:duplicateParentState="true"
        android:paddingTop="10dip"
        android:paddingRight="10dip"
        android:paddingBottom="10dip"
        />

</com.checkit.app.CheckableLinearLayout>

更多

以下是我想说的一些事情:

  • checkboxGlobal.setEnabled(true);完美无缺,正如我们在屏幕截图(灰色标题)中看到的那样。但是checkboxGlobal.setChecked(true);似乎没有效果(?)
  • 一次,我尝试了Drawable drawable = convertView.getResources().getDrawable(R.drawable.ic_hideable_item);,然后是checkboxImg.setImageDrawable(states);。使用选中的复选框初始化复选框非常有效,但是无法取消勾选该框。

编辑2014-03-12:我的代码基于“Android开发者”网站上的官方代码示例(参见项目“CustomChoiceList”,可在此处获取:http://developer.android.com/samples/CustomChoiceList/index.html

编辑2014-03-13:删除不必要的代码,以便于阅读

提前感谢您的帮助。随意询问您是否需要更多代码。 此致 贝

2 个答案:

答案 0 :(得分:1)

我找到了答案。它看起来不是很有效,但它确实有效。 我的答案是基于 DroidBender 的代码。

对于那些可能感兴趣的人,这里是TasksAdapter.java中.getView()方法的代码:

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

    // if we access for the first time : we have to user LayoutInflater
    if (convertView == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(R.layout.task_items, null);
    }

    // get the font size (from the SharedPreferences)
    String fontSizeSetting = MainActivity.getmFontSize();

    // set the text on the tasks
    checkboxGlobal = (com.checkit.app.CheckableLinearLayout) convertView.findViewById(R.id.taskGlobalCheckbox);
    checkboxImg = (ImageView) convertView.findViewById(R.id.taskImageCheckbox);
    checkboxTxt = (TextView) convertView.findViewById(R.id.task_text);

    // get the Task ( "final" <= very important)
    final Task taskItemFinal = (Task) mItemsList.get(position);

    // is the task done ?
    boolean isTick = (taskItemFinal.getIsTick() == 1 ? true : false );

    // init
    Drawable drawable = null;

    // is this task done ?
    if (isTick) {
        // the task is done, so tick the checkbox
        drawable = convertView.getResources().getDrawable(R.drawable.btn_check_on);
    } else {
        // the task is undone, so untick the checkbox
        drawable = convertView.getResources().getDrawable(R.drawable.btn_check_off);
    }
    // set the initial drawable
    checkboxImg.setImageDrawable(drawable);

    // click event on the checkbox
    checkboxGlobal.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // is the checkbox ticked ?
            boolean isTick = (taskItemFinal.getIsTick() == 1 ? true : false );

            Drawable drawable = null;

            // set a different drawable depending to the state
            if (isTick) {
                drawable = v.getResources().getDrawable(R.drawable.btn_check_off);
            } else {
                drawable = v.getResources().getDrawable(R.drawable.btn_check_on);
            }

            // set the drawable to the element
            checkboxImg = (ImageView) v.findViewById(R.id.taskImageCheckbox);
            checkboxImg.setImageDrawable(drawable);

            // toggle 
            taskItemFinal.toggle();

            // Open the connection
            TaskDAO taskDao = new TaskDAO(v.getContext());
            taskDao.open();

            // update the data
            taskDao.update(taskItemFinal);

            // set to false
            return false;
        }
    });

    // set the text on the TextView
    checkboxTxt.setText(taskItemFinal.getName());

    // change the font size
    checkboxTxt.setTextSize(Float.parseFloat(fontSizeSetting));

    // return the view
    return convertView;
}

提示包括使用名为taskItemFinal的最终变量,并在.taskItemFinal()方法中使用它。然后我自己改变了drawable。 在这种情况下,您必须删除TasksActivity.java文件中的.setOnItemClickListener(就像我之前保存状态一样)。

希望有所帮助。

答案 1 :(得分:0)

首先,按照以下方式更改selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_selected="true" 
        android:drawable="@drawable/btn_check_on" 
        />
    <item 
        android:state_selected="false" 
        android:drawable="@drawable/btn_check_off" 
        />
</selector>

getView(..)的{​​{1}}内(为了便于说明,我使用了一些不存在的getter&amp; setter):

Adapter