【问题标题】:Make invisible an element located in a row of a ListView使位于 ListView 行中的元素不可见
【发布时间】:2017-10-31 15:02:16
【问题描述】:

我需要使位于 ListView 行中的 TextView 不可见。 ListView 由数据库创建。我希望具有 Id (day0, day1, day2..., day7) 的 TextView 可以根据数据库的内容变得可见或不可见。

特别是我不能将具有这些 id (day0, day1, day2…, day7) 的元素放入 Textview 变量中

我尝试了这段代码来验证是否所有具有这些 id 的元素都变得不可见,但它不起作用。

TextView tx = (TextView) v.findViewById(R.id.day0);
tx.setVisibility(View.INVISIBLE);

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="vertical"
android:showDividers="none"
android:descendantFocusability="blocksDescendants">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="none"
android:descendantFocusability="blocksDescendants"
>
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp">
        <TextView
            android:id="@+id/hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextAppearance.AppCompat"
            android:text="12"
            android:textSize="@dimen/text_size_display_3"/>
        <Space
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />

        <TextView
            android:id="@+id/separator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextAppearance.AppCompat"
            android:text=":"
            android:textSize="@dimen/text_size_display_3"

            />
        <Space
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/minute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextAppearance.AppCompat"
            android:text="00"
            android:textSize="@dimen/text_size_display_3"

            />
  </LinearLayout>
  <Switch
      android:id="@+id/switch2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="40dp"
      android:layout_centerVertical="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"/>
</RelativeLayout>
<LinearLayout
    android:id="@+id/days"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/cacca"
        style="@style/AlarmDayToggle"
        android:text="LUN"/>

    <TextView
        android:id="@+id/day1"
        style="@style/AlarmDayToggle"
        android:text="MAR"/>

    <TextView
        android:id="@+id/day2"
        style="@style/AlarmDayToggle"
        android:text="MER"/>

    <TextView
        android:id="@+id/day3"
        style="@style/AlarmDayToggle"
        android:text="GIO"/>

    <TextView
        android:id="@+id/day4"
        style="@style/AlarmDayToggle"
        android:text="VEN"/>

    <TextView
        android:id="@+id/day5"
        style="@style/AlarmDayToggle"
        android:text="SAB" />

    <TextView
        android:id="@+id/day6"
        style="@style/AlarmDayToggle"
        android:text="DOM"/>

  </LinearLayout>

AndroidListViewCursorAdaptorActivity.java

    public class AndroidListViewCursorAdaptorActivity extends AppCompatActivity {

    private DbAdapter dbHelper;
    private SimpleCursorAdapter dataAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        dbHelper = new DbAdapter(this);
        dbHelper.open();

        //clean all data
        dbHelper.deleteAllAlarm();
        //add some data
        dbHelper.insertSomeAlarm();

        //Generate listView from sqlite database
        displayListView();
    }

    private void displayListView(){
        Cursor cursor = dbHelper.fetchAllAlarm();
        String[] columns = new String[] {
                DbAdapter.COLUMN_HOUR,
                DbAdapter.COLUMN_MINUTES
        };
        Log.d("column", cursor.toString());

        int[] to = new int[]{
                R.id.hour,
                R.id.minute
        };

        dataAdapter = new SimpleCursorAdapter(
                this,
                R.layout.row,
                cursor,
                columns,
                to,
                0);

    }

}

DbAdapter.java

public class DbAdapter extends AndroidListViewCursorAdaptorActivity{

public static final String DATABASE_CREATE =
    "CREATE TABLE " + TABLE_ALARMS + " ("
            + COLUMN_ID + " INTEGER PRIMARY KEY, "
            + COLUMN_HOUR + " INTEGER NOT NULL, "
            + COLUMN_MINUTES + " INTEGER NOT NULL, "
            + COLUMN_SUNDAY + " INTEGER NOT NULL DEFAULT 0, "
            + COLUMN_MONDAY + " INTEGER NOT NULL DEFAULT 0, "
            + COLUMN_TUESDAY + " INTEGER NOT NULL DEFAULT 0, "
            + COLUMN_WEDNESDAY + " INTEGER NOT NULL DEFAULT 0, "
            + COLUMN_THURSDAY + " INTEGER NOT NULL DEFAULT 0, "
            + COLUMN_FRIDAY + " INTEGER NOT NULL DEFAULT 0, "
            + COLUMN_SATURDAY + " INTEGER NOT NULL DEFAULT 0);";






public long createAlarm (String hours, String minutes,
                         Integer mon, Integer tue, Integer wed,
                         Integer thu, Integer fri, Integer sat,
                         Integer sun){
    ContentValues values = new ContentValues();
    values.put(COLUMN_HOUR, hours);
    values.put(COLUMN_MINUTES, minutes);
    values.put(COLUMN_MONDAY, mon);
    values.put(COLUMN_TUESDAY, tue);
    values.put(COLUMN_WEDNESDAY, wed);
    values.put(COLUMN_THURSDAY, thu);
    values.put(COLUMN_FRIDAY, fri);
    values.put(COLUMN_SATURDAY, sat);
    values.put(COLUMN_SUNDAY, sun);
    return mDb.insert(SQLITE_TABLE, null, values);

}


public Cursor fetchAllAlarm(){
    Cursor mCursor = mDb.query(SQLITE_TABLE,
            new String[] {COLUMN_ID,COLUMN_HOUR,COLUMN_MINUTES},
            null, null, null, null, null);
    if (mCursor != null){
        Log.d("fetchalarm","fetchalarm");
        mCursor.moveToFirst();
    }
    return mCursor;
}

public void insertSomeAlarm(){
    Log.d("insert","insert");
    createAlarm("12","30",1,1,1,1,1,0,0);
    createAlarm("13","01",0,0,0,0,0,1,1);
    createAlarm("15","00",1,0,1,0,1,0,1);
    createAlarm("16","00",1,0,1,1,1,0,0);
    createAlarm("17","00",0,1,0,0,1,0,0);
} 

}

感谢您的帮助!

【问题讨论】:

  • 您没有day0 的ID。这段代码不会崩溃吗?

标签: android xml sqlite listview


【解决方案1】:

为了能够操作列表中的视图,您需要通过扩展合适的适配器来使用自定义适配器。

首先,尽管您需要向适配器提供值,所以我建议提供一种方法来创建包含所有列的 Cursor,而不是像您的 fetchAllAlarm 方法提供的可用列的子集。类似于:-

public Cursor getAllAlarms() {
    return mDb.query(TABLE_ALARMS,null,null,null,null,null,null);
}

现在为您的自定义适配器创建一个类,例如扩展 SimpleCursorAdapter 的 ALarmCursorAdpater。例如:-

public class AlarmCursorAdapter extends SimpleCursorAdapter {

}

添加 /a 构造函数,例如:-

公共类 AlarmCursorAdapter 扩展 SimpleCursorAdapter {

AlarmCursorAdapter(Context context,
                   int layout,
                   Cursor csr, String[] columns, int[] toviews) {
    super(context, layout,csr,columns,toviews,0);
}

}

注意!在上面我没有允许选项指定标志(第 6 个)参数,而是在调用 super 时硬编码。

现在是通过覆盖适当的方法来实现自定义的问题。在这种情况下,有两种方法可以提供帮助:bindViewgetView

bindView 将数据(根据提供的列)附加到视图(根据视图提供)。该方法传递了光标,光标将被适当定位(即在为其构建视图的行)。

getView 获得适当的视图。该方法传递了位置、视图及其父级,并且必须返回视图。

两者中最适合对数据进行操作的是 bindView 方法,因为它提供了光标,因此很容易对光标内的数据进行操作。

无论如何,首先这是 CustomAdapter,它覆盖了 bindViewgetView 方法,它们不会改变任何东西(即它只是调用超级方法): -

public class AlarmCursorAdapter extends SimpleCursorAdapter {

    AlarmCursorAdapter(Context context,
                       int layout,
                       Cursor csr, String[] columns, int[] toviews) {
        super(context, layout,csr,columns,toviews,0);
    }

    @Override
    public void bindView(View view, Context context, Cursor csr) {
        super.bindView(view, context, csr);
    }

    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        return super.getView(position, convertview, parent);
    }
}

现在假设你想让一天的值不可见,如果它的值为 0,否则让它可见,那么需要两件事 a) 相应地确定数据的状态和 b) 视图 (TextView) 是调整。

  • 由于光标可用,因此可以使用适当的光标get???? 方法来获取数据。
  • 作为视图(不是数据的特定视图,而是 ListView 项的视图)提供。可以使用 findViewById 获取数据的具体视图。

所以适配器可以变成:-

public class AlarmCursorAdapter extends SimpleCursorAdapter {

    AlarmCursorAdapter(Context context,
                       int layout,
                       Cursor csr, String[] columns, int[] toviews) {
        super(context, layout,csr,columns,toviews,0);
    }

    @Override
    public void bindView(View view, Context context, Cursor csr) {
        TextView monday = (TextView) view.findViewById(R.id.day1);
        if (csr.getInt(csr.getColumnIndex(DbAdapter.COLUMN_MONDAY)) > 0) {
            monday.setVisibility(View.VISIBLE);
            monday.setBackgroundColor(0xFFCCCCFF);
        } else {
            monday.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        return super.getView(position, convertview, parent);
    }
}

因此,如果正在处理的行的星期一列中的值大于 0,则它的视图将变为可见,并且出于说明目的还将其背景颜色设置为浅蓝色。否则(即如果数据的值为 0 或更小),那么它将是不可见的。

现在可以使用自定义适配器,例如:-

private void displayAltListView() {
    Cursor csr = dbHelper.getAllAlarms();
    String[] columns = new String[] {
            DbAdapter.COLUMN_HOUR,
            DbAdapter.COLUMN_MINUTES,
    };
    int[] toviewids = new int[]{
            R.id.hour,
            R.id.minute
    };
    alarmAdapter = new AlarmCursorAdapter(
            this,
            R.layout.row,
            csr,
            columns,
            toviewids
    );
    alarmlist.setAdapter(alarmAdapter);
}

要利用上述内容,您需要一个名为 alarmAdapter 的类变量,用于 CustomAdapter。

请注意,我在您的代码中没有设置 Listview 的适配器,也没有从它的 id 中检索 ListView。因此,如果您复制以上内容,则需要:-

所以你会有:-

DbAdapter dbHelper;
SimpleCursorAdapter dataAdapter;
AlarmCursorAdapter alarmAdapter;
ListView alarmlist;

而不是:-

DbAdapter dbHelper;
SimpleCursorAdapter dataAdapter;

在通过调用setContentView 设置视图后,您还需要在活动的onCreate 方法中添加以下行:-

alarmlist = (ListView) this.findViewById(R.id.alarm_listview); 

结果将是:-

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    相关资源
    最近更新 更多