1

カスタムの「チェック可能」要素を使用してチェックリストを作成しようとしています。リストを表示し、チェックボックス (ImageView) をチェックして、各チェックボックスの状態をデータベースに保存できます。

残念ながら、チェックリストを開くと、いくつかのチェックボックスをオンにしてチェックリストを初期化したいのですが、できません。うまくいきません。


スクリーンショット

チェックリストを開いたときの画面は次のとおりです。

すべてのチェックボックスがオフになっているチェックリストを表示する Android 画面

凡例:すべてのチェックボックスがオフになっているチェックリストを表示する Android 画面

メソッド .setEnabled(true/false) を使用して、(デバッグ目的で) さまざまな状態があることを視覚的に示しました。

基本的に、ユーザーとしてチェックボックスをオンにすると、次のようになります。

チェックボックスがオンになっている Android 画面

凡例:チェックボックスがオンになっている Android 画面


階層

  • ソース
    • com.checkit.app (パッケージ)
      • CheckableLinearLayout.java
      • TasksActivity.java
      • TasksAdapter.java <= (問題はこのファイルのどこかで解決できると思います)
      • [...]
  • 解像度
    • ドローアブル
      • ic_hideable_item.xml
    • drawable-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 Developers」Web サイトの公式コード サンプルに基づいています (プロジェクト「CustomChoiceList」を参照してください: http://developer.android.com/samples/CustomChoiceList/index. html )。

編集 2014-03-13 :読みやすくするために、不要なコードを削除します

よろしくお願いします。さらにコードが必要な場合は、お気軽にお問い合わせください。よろしく、トニー

4

2 に答える 2

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(..)あなたの内部Adapter(説明のために、存在しないゲッターとセッターをいくつか使用しました):

 // code code code
 checkboxImg.setSelected(taskItem.isTicked());
 checkboxImg.setOnClickListener(new OnClickListener(){

     @Override
     public void onClick(View view){
          taskItem.setTick(!taskItem.isTicked());
          notifyDataSetChanged();
     }

 });
 // code code code
于 2014-03-11T12:29:12.943 に答える