2017-06-19 93 views
-1

我正在Android中创建一个记事应用程序。我想在主要活动的ListFragment中显示一个ListView。但是由于getView方法没有在自定义适配器中调用,因此不会显示该列表。我阅读了很多SO帖子,并尝试了很多解决方案,但他们都没有为我工作。 getCount()方法工作正常,并返回正确的数据大小而不是0,但它被调用大约3到4次。getView()不会在ListView的自定义适配器中调用

我检查了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

你应该使用'Toast',而不是'的System.out.println( “getView称为”);' –

+0

并且还,这条线'ListView notesListView = getListView();'看起来不对。 –

+0

我不希望用户看到调用什么方法。 print语句是为了调试程序。 –

回答

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; 
} 
    } 
+0

谢谢你,但ListView仍然没有出现。 –

+0

什么是'R.layout.notes_list_item, R.id.list_no_items'? –

+0

对不起,我忘了提到这一点。 R.layout.notes_list_item是我包含的自定义行布局文件。 R.id.list_no_items是当列表为空时应该显示的TextView的id。 –

相关问题