我正在Android中制作便笺应用。我想在主活动的ListFragment中显示ListView。但是,由于未在自定义适配器中调用getView方法,因此未显示该列表。我阅读了很多SO帖子并尝试了很多解决方案,但它们似乎都不适合我。 getCount()方法工作正常并返回正确的数据大小而不是0,但它被调用大约3到4次。
我检查了getAllNotes(),getCount(),notesListView,notesList,adapter的值,它们都返回正确的值。我在代码中设置了断点并对其进行了调试,但执行并没有停止在getView()方法中设置的断点处,所以我知道问题出在哪里。
我的列表片段(NotesListFragment.java):
package com.example.sriramkailasam.notes;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class NotesListFragment extends ListFragment {
private Context context;
private ArrayList<Note> notesList = new ArrayList<>();
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
System.out.println("notes list fragment attached");
}
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
System.out.println("fragment going to inflate layout");
return inflater.inflate(R.layout.fragment_notes_list, container);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
System.out.println("fragment view created");
NotesAdapter adapter = new NotesAdapter(context,
R.layout.notes_list_item,
R.id.list_no_items,
getAllNotes());
ListView notesListView = getListView();
notesListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private ArrayList<Note> getAllNotes() {
openAndQueryDatabase();
return notesList;
}
private void openAndQueryDatabase() {
DatabaseHelper helper = new DatabaseHelper(context);
SQLiteDatabase database = helper.getReadableDatabase();
String selectQuery =
"SELECT title, body FROM " + DatabaseContract.NotesEntry.TABLE_NAME;
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor != null) {
cursor.moveToFirst();
do {
Note note = new Note();
note.setTitle(cursor.getString(cursor.getColumnIndex("title")));
note.setBody(cursor.getString(cursor.getColumnIndex("body")));
notesList.add(note);
} while (cursor.moveToNext());
cursor.close();
}
else {
System.out.println("cursor is null");
}
}
}
自定义适配器(NotesAdapter.java):
package com.example.sriramkailasam.notes;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class NotesAdapter extends ArrayAdapter<Note> {
private int resourceId;
private List<Note> notes;
public NotesAdapter(Context context,
int resource,
int textViewResourceId,
List<Note> notesList) {
super(context, resource,textViewResourceId, notesList);
resourceId = resource;
notes = notesList;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
System.out.println("getCount called: " + notes.size());
return notes.size();
}
@Override
public Note getItem(int position) {
return notes.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView called");
LayoutInflater inflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = inflater.inflate(resourceId, parent, true);
}
TextView titleTextView =(TextView) convertView.findViewById(R.id.note_title);
TextView bodyTextView = (TextView) convertView.findViewById(R.id.note_body);
titleTextView.setText((getItem(position)).getTitle());
bodyTextView.setText((getItem(position)).getBody());
return convertView;
}
}
行布局(notes_list_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView
android:id="@+id/note_body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
片段布局(fragment_notes_list.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.sriramkailasam.notes.NotesListFragment">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/list_no_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/empty_list" />
</LinearLayout>
Logcat输出:
06-19 15:35:45.466 14513-14524/com.example.sriramkailasam.notes I/art: Background sticky concurrent mark sweep GC freed 14374(578KB) AllocSpace objects, 0(0B) LOS objects, 38% free, 5MB/8MB, paused 6.269ms total 27.655ms
06-19 15:35:45.686 14513-14513/com.example.sriramkailasam.notes W/System: ClassLoader referenced unknown path: /data/app/com.example.sriramkailasam.notes-1/lib/arm
06-19 15:35:45.700 14513-14513/com.example.sriramkailasam.notes I/InstantRun: starting instant run server: is main process
06-19 15:35:45.792 14513-14513/com.example.sriramkailasam.notes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-19 15:35:46.001 14513-14513/com.example.sriramkailasam.notes I/System.out: notes list fragment attached
06-19 15:35:46.002 14513-14513/com.example.sriramkailasam.notes I/System.out: fragment going to inflate layout
06-19 15:35:46.008 14513-14513/com.example.sriramkailasam.notes I/System.out: fragment view created
06-19 15:35:46.018 14513-14513/com.example.sriramkailasam.notes I/System.out: getCount called: 30
06-19 15:35:46.018 14513-14513/com.example.sriramkailasam.notes I/System.out: getCount called: 30
06-19 15:35:46.018 14513-14513/com.example.sriramkailasam.notes I/System.out: getCount called: 30
06-19 15:35:46.018 14513-14513/com.example.sriramkailasam.notes I/System.out: getCount called: 30
06-19 15:35:46.018 14513-14513/com.example.sriramkailasam.notes I/System.out: getCount called: 30
06-19 15:35:46.086 14513-14550/com.example.sriramkailasam.notes I/Adreno: QUALCOMM build : 48a5bf5, I15255e4b4a
Build Date : 02/22/17
OpenGL ES Shader Compiler Version: XE031.09.00.03
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.UM.5.5.R1.07.00.00.269.019
Remote Branch : NONE
Reconstruct Branch : NOTHING
06-19 15:35:46.092 14513-14550/com.example.sriramkailasam.notes I/OpenGLRenderer: Initialized EGL, version 1.4
06-19 15:35:46.092 14513-14550/com.example.sriramkailasam.notes D/OpenGLRenderer: Swap behavior 1
06-19 15:35:46.125 14513-14513/com.example.sriramkailasam.notes W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
答案 0 :(得分:0)
您尚未初始化listView。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes_list,
container, false);
notesListView = (ListView) rootView.findViewById(android.R.id.yourListView);
return rootView;
}
然后
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
System.out.println("fragment view created");
NotesAdapter adapter = new NotesAdapter(context,
R.layout.notes_list_item,
R.id.list_no_items,
getAllNotes());
notesListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
修改
private ArrayList<Note> getAllNotes() {
DatabaseHelper helper = new DatabaseHelper(context);
SQLiteDatabase database = helper.getReadableDatabase();
String selectQuery =
"SELECT title, body FROM " + DatabaseContract.NotesEntry.TABLE_NAME;
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor != null) {
cursor.moveToFirst();
do {
Note note = new Note();
note.setTitle(cursor.getString(cursor.getColumnIndex("title")));
note.setBody(cursor.getString(cursor.getColumnIndex("body")));
notesList.add(note);
} while (cursor.moveToNext());
cursor.close();
}
else {
System.out.println("cursor is null");
}
return noteList;
}
}