请帮助我找到一个解决方案,当我点击菜单项&#34时,如何设置我的复选框可见;删除"当我按下"确认"并设置为不可见或"取消"。 我使用SimpleCursorAdapter,cuz listview是从带有游标加载器的数据库中填充的... 这是我的代码:
MainActivity
public class MainActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor>{
final String LOG = "phonebookLogs";
private static final int LOADER_ID = 1;
Intent intentAddContact;
ListView lvMain;
CheckBox cbxList;
MenuItem addContact, deleteContact, settings, expimp, confirmDelete, cancelDelete;
DB mDB;
SimpleCursorAdapter scAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find ListView
lvMain = (ListView)findViewById(R.id.lvMain);
//calling for creating adapter
onCreateAdapter();
}
public void onCreateAdapter(){
String[] from = new String[] { DB.COL_LINK, DB.COL_SURNAME, DB.COL_NAME };
int[] to = new int[] { R.id.ivPhoto, R.id.tvSurname, R.id.tvName, };
if(to[0] == 0){
to[0] = R.drawable.default_contact;
}
scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
lvMain.setAdapter(scAdapter);
// loader for reading data
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
}
public void deleteRecord(){
//when MenuItem "Delete" is clicked
addContact.setVisible(false);
deleteContact.setVisible(false);
settings.setVisible(false);
expimp.setVisible(false);
confirmDelete.setVisible(true);
cancelDelete.setVisible(true);
}
public void onCancelDeleteRecord(){
////when MenuItem "cancel" is clicked
addContact.setVisible(true);
deleteContact.setVisible(true);
settings.setVisible(true);
expimp.setVisible(true);
confirmDelete.setVisible(false);
cancelDelete.setVisible(false);
}
public void confirmDeleteRecord(){
////when MenuItem "confirm" is clicked
addContact.setVisible(true);
deleteContact.setVisible(true);
settings.setVisible(true);
expimp.setVisible(true);
confirmDelete.setVisible(false);
cancelDelete.setVisible(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
//find all MenuItems
addContact = menu.findItem(R.id.addContact);
deleteContact = menu.findItem(R.id.deleteContact);
settings = menu.findItem(R.id.settings);
expimp = menu.findItem(R.id.expimp);
confirmDelete = menu.findItem(R.id.confirmDelete);
cancelDelete = menu.findItem(R.id.cancelDelete);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.addContact:
intentAddContact = new Intent(this, NewContact.class);
startActivity(intentAddContact);
break;
case R.id.deleteContact:
deleteRecord();
break;
case R.id.settings:
break;
case R.id.expimp:
break;
case R.id.confirmDelete:
confirmDeleteRecord();
break;
case R.id.cancelDelete:
onCancelDeleteRecord();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
return new MyCursorLoader(this, mDB);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
scAdapter.swapCursor(null);
}
}
DB.class和CursorLoader.class很好,简单的查询和加载。 和我的ListView的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:layout_weight="0.5"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivPhoto"
android:layout_width="@dimen/imageWidth"
android:layout_height="@dimen/imageHeight"
android:layout_gravity="center_vertical"
android:contentDescription="@string/photoDescription"
android:src="@drawable/default_contact" />
</LinearLayout>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin"
android:text=""
android:textSize="@dimen/textSize" />
<TextView
android:id="@+id/tvSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin"
android:text=""
android:textSize="@dimen/textSize" />
</LinearLayout>
<CheckBox
android:id="@+id/cbxList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/margin"
android:layout_weight="0.2"
android:visibility="invisible" />
</LinearLayout>
我已经阅读了很多内容,但如何在不创建自己的适配器的情况下实现它仍然无法获得...... 非常感谢任何寻求解决方案的帮助!
答案 0 :(得分:1)
您可以将复选框设置为可见:cbxlist.setVisibility(View.VISIBLE);
如果您希望它完全消失,请使用(即让它看起来好像从未添加过):cbxlist.setVisibility(View.GONE);
如果您希望它只是隐身,但仍然占据相同的空间:cbxlist.setVisibility(View.INVISIBLE);
编辑:
for(int i = 0; i < lvMain.getChildCount(); i++) {
lvMain.getChildAt(i).findViewById(R.id.cbxList).setVisibility(View.VISIBLE);
}
这应该将所有复选框设置为可见。同样,在需要时请求View.INVISIBLE
。
答案 1 :(得分:0)
如果要更改列表视图中每个复选框的可见性(假设您的XML是列表行视图),则需要创建自定义适配器并覆盖getView方法。
在那里,您将执行以下操作:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
cBox = (CheckBox ) convertView.findViewById(R.id.cbxList);
cBox.setVisibility(View.GONE);
return convertView;
}