我正在尝试使用ListView
开发一个CheckBox
。
文本数据由SQLite管理,SimpleCursorAdapter
用作适配器。
当我单击CheckBox
时,我想使其消失并缩小空间(而不是从数据库中删除)。我使用setVisibility(View.GONE)
,但空格仍然存在。
为什么空间仍然存在? 我应该如何实施?
监听器:
private class ItemClickListener implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
CheckBox selectedCB = view.findViewById(R.id.cb01);
selectedCB.setChecked(!selectedCB.isChecked());
selectedCB.setVisibility(View.GONE);
}
}
xml:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cb01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:clickable="false"
android:focusable="false"
android:text="" />
onCreate(MainActivity.java):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivityForResult(intent, REQUEST_CODE_ADD);
}
});
ListView lvCB = findViewById(R.id.lvCB);
DatabaseHelper helper = new DatabaseHelper(MainActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
Cursor c = db.rawQuery("select * from belongings",null);
String[] from = {"name"};
int[] to = {R.id.cb01};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row,c,from,to,0);
lvCB.setAdapter(adapter);
ItemClickListener clickListener = new ItemClickListener();
lvCB.setOnItemClickListener(clickListener);
ItemLongListener longListener = new ItemLongListener();
lvCB.setOnItemLongClickListener(longListener);
}
AddListener(AddActivity.java):
private class AddListener implements View.OnClickListener{
Intent intent = new Intent();
@Override
public void onClick(View view){
EditText input = findViewById(R.id.editText_name);
String inputStr = input.getText().toString();
DatabaseHelper helper = new DatabaseHelper(AddActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
String sqlInsert = "INSERT INTO belongings (name) VALUES (?)";
SQLiteStatement stmt = db.compileStatement(sqlInsert);
stmt.bindString(1, inputStr);
stmt.executeInsert();
intent.putExtra("INPUT_STRING", inputStr);
setResult(RESULT_OK, intent);
finish();
}
}
答案 0 :(得分:0)
如果要隐藏列表视图的元素,则需要为此使用适配器。就您而言,使用setVisibility(View.GONE)
将隐藏视图,但是您将看到一个空白区域。尽管您可以通过一些技巧来删除该空间,但是通过更改方向可以看到列表处于初始状态。这里的事情是您的列表将考虑该项目(因为它存在于适配器中)并将显示它。根据一般经验,如果您做的事情会影响列表视图中的给定视图,则应始终以某种方式在适配器中反映出来。
从列表视图中隐藏内容的正确方法是从适配器中删除该项目,并通知列表某些内容已更改:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Write here the code to remove the item from the adapter
adapter.notifyDataSetChanged(); // keep this unchanged
}
});