【发布时间】: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