我不明白出了什么问题?
LIST_ITEM
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator">
<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:id="@+id/name"
android:layout_gravity="top|center_horizontal"
android:layout_marginLeft="10dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkbox"
android:id="@+id/checkBox"
android:clickable="true"
android:paddingLeft="@dimen/abc_action_bar_overflow_padding_start_material"
android:singleLine="false"
android:textAppearance="@color/colorAccent"
android:layout_gravity="right" />
</LinearLayout>
fragment_list
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_margin="@dimen/activity_vertical_margin"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_24dp"
app:layout_anchor="@id/listView"
app:layout_anchorGravity="bottom|right|end"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:backgroundTint="@color/colorAccent"
app:rippleColor="@color/colorPrimary"/>
</android.support.design.widget.CoordinatorLayout>
FragmentList.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_list, container, false);
fab = (FloatingActionButton) getView().findViewById(R.id.fab);
listView = (ListView) v.findViewById(R.id.listView);
//обработка добавления человека - нажатие на fab
fab.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//TODO добавление!!!
Toast.makeText(v.getContext(), "Новая запись добавлена", Toast.LENGTH_SHORT).show();
}
});
//создание курсора
try{
SQLiteOpenHelper databaseHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase 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);
//переход к первой записи в курсоре
if (cursor.moveToFirst()){
//получение данных из курсора
nameText = cursor.getString(1);
isAvatar = (cursor.getInt(2)==1);
//заполнение чекбокса, тест имени и аватар
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
TextView textView = (TextView) v.findViewById(R.id.name);
ImageView image = (ImageView) v.findViewById(R.id.imageView);
checkBox.setChecked(isAvatar);
textView.setText(nameText);
if (checkBox.isChecked()) {
image.setImageResource(R.drawable.avatar);
} else image.setImageResource(R.drawable.no_avatar);
}
} catch (SQLiteException e){
Toast.makeText(v.getContext(), "База данных недоступна", Toast.LENGTH_SHORT).show();
}
//обработка нажатия по чекбоксу
// checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkBox.isChecked();
// }
// });
return v;
}
//обработка нажатия пункте списка
public void onListItemClick(){
}
//закрытие базы данных и курсора
@Override
public void onDestroy(){
super.onDestroy();
cursor.close();
db.close();
}
}
为什么CheckBox checkBox =(CheckBox)v.findViewById(R.id.checkBox)为空? 我尝试了很多变化,但变得愚蠢((
答案 0 :(得分:0)
您正在膨胀不包含复选框,图像视图或文本视图的fragment_list布局。这就是你得到这个错误的原因。 实现这个游标适配器
public class MyCursorAdapter extends CursorAdapter {
public MyCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.mylist_row, parent, false);
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
if (cursor.moveToFirst()){
//получение данных из курсора
nameText = cursor.getString(1);
isAvatar = (cursor.getInt(2)==1);
//заполнение чекбокса, тест имени и аватар
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
TextView textView = (TextView) v.findViewById(R.id.name);
ImageView image = (ImageView) v.findViewById(R.id.imageView);
checkBox.setChecked(isAvatar);
textView.setText(nameText);
if (checkBox.isChecked()) {
image.setImageResource(R.drawable.avatar);
} else image.setImageResource(R.drawable.no_avatar);
}
}
}
现在在onCreateview
MyCursorAdapter todoAdapter = new MyCursorAdapter(this.getActivity(), cursor, 0);
lvItems.setAdapter(todoAdapter);
答案 1 :(得分:0)
您正在查看checkBox
上的v
:CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
但v
已分配到fragment_list
此处View v = inflater.inflate(R.layout.fragment_list, container, false);
但是fragment_list
不包含checkBox
和,因此v.findViewById(R.id.checkBox)
返回null 。
当你想使用SimpleCursorAdapter时,你需要创建一个扩展SimpleCursorAdapter的类,然后你可以访问/膨胀getView (final int pos, View view, ViewGroup parent)
中的“row-item”布局文件 - 然后你就可以说:CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox)
。