为什么我得到0?必须是一个字

时间:2016-05-02 14:00:15

标签: android xml sqlite

enter image description here

为什么复选框有0? 字符串资源必须有字符串“Avatar”。 我需要帮助。 这是我的代码.................. ................................ ................................

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

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/no_avatar"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="jjjjj"
        android:id="@+id/name"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="19dp"
        android:layout_marginTop="13dp"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView"
        android:layout_toRightOf="@id/imageView"
        android:clickable="false"
        android:textSize="20sp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/checkbox"
        android:id="@+id/checkBox"
        android:height="0dp"
        android:clickable="true"
        android:checked="false"
        android:layout_gravity="right"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="9dp"
        android:layout_marginRight="9dp"/>
</RelativeLayout>

从SQLite填写ListView

try{
        SQLiteOpenHelper databaseHelper = new DatabaseHelper(v.getContext());
        db = databaseHelper.getWritableDatabase();
        Cursor cursor = db.query("PEOPLE", new String[] {"_id", "NAME", "CHECKBOX"}, null, null, null, null, null);
        CursorAdapter listAdapter = new SimpleCursorAdapter(v.getContext(), R.layout.list_item, cursor, new String[]{"NAME", "CHECKBOX"}, new int[]{R.id.name, R.id.checkBox}, 0);
        listView.setAdapter(listAdapter);
    } 
    catch (SQLiteException e){
        Toast.makeText(v.getContext(), "База данных недоступна", Toast.LENGTH_SHORT).show();
    }

资源文件:

<resources> 
    <string name="app_name">Test Application</string> 
    <!-- TODO: Remove or change this placeholder text --> 
    <string name="menu_lang">English</string> 
    <string name="listtab">List</string> 
    <string name="scalingtab">Scaling</string> 
    <string name="parsingtab">Parsing</string> 
    <string name="maptab">Map</string> 
    <string name="checkbox">Avatar</string> 
    <string name="btn_add">Add</string> 
    <string name="btn_rev">Revert</string> 
    <string name="add_title">Add new person</string> 
</resources>

2 个答案:

答案 0 :(得分:1)

你得到&#34; 0&#34;而不是&#34;阿凡达&#34;因为SimpleCursorAdapterCheckBox视为一种TextView(这基本上是正确的,因为CheckBoxTextView的间接子类。)

对于所有TextView,它只会将文本设置为相应数据库列中的值(&#34; 0&#34;或&#34; 1&#34;)( CHECKBOX )。

如果您想要其他行为,Activity(或Fragment)必须充当SimpleCursorAdapter.ViewBinder

public class MyActivity extends AppCompatActivity implements SimpleCursorAdapter.ViewBinder

您将Activity设为ViewBinder SimpleCursorAdapter,如下所示:

CursorAdapter listAdapter = new SimpleCursorAdapter(v.getContext(), R.layout.list_item, cursor, new String[]{"NAME", "CHECKBOX"}, new int[]{R.id.name, R.id.checkBox}, 0);
listAdapter.setViewBinder(this);

您必须覆盖setViewValue()方法:

@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex)
{
    if (view instanceof CheckBox)
    {
        CheckBox cb = (CheckBox)view;
        String checked = cursor.getString(columnindex);
        switch(checked)
        {
             case "0":
                 cb.setChecked(false);
                 break;
             default: // <=> case "1"
                 cb.setChecked(true);
        }

        // ('true' means you took care of it )
        return true;
    }

    // don't do anything with other View types as the default worked fine
    // ('false' means you leave it to the SimpleCursorAdapter)
    return false;
}

答案 1 :(得分:0)

0是res / values / string.xml文件中包含的“checkbox”字符串的当前值

如果您不想在复选框上显示文字,请删除android:text="@string/checkbox"