在listliew上显示项目

时间:2013-12-10 17:49:26

标签: java android listview

我制作了一个片段,它往往显示数据库中的注释列表视图,每个项目都有3个文本视图:主题,内容和所有者,但是当按下列表按钮显示列表视图时出错,似乎我的函数showList出现了问题但是没有不知道为什么,这是我的logcat和代码。感谢任何提前帮助我的坏英语!

12-11 00:36:13.961: E/AndroidRuntime(779): FATAL EXCEPTION: main
12-11 00:36:13.961: E/AndroidRuntime(779): java.lang.NullPointerException
12-11 00:36:13.961: E/AndroidRuntime(779):  at com.bblackbb.jotdownv2.WhiteNote.showNoteList(WhiteNote.java:117)
12-11 00:36:13.961: E/AndroidRuntime(779):  at com.bblackbb.jotdownv2.WhiteNote.access$1(WhiteNote.java:109)
12-11 00:36:13.961: E/AndroidRuntime(779):  at com.bblackbb.jotdownv2.WhiteNote$2.onClick(WhiteNote.java:92)
12-11 00:36:13.961: E/AndroidRuntime(779):  at android.view.View.performClick(View.java:4084)
12-11 00:36:13.961: E/AndroidRuntime(779):  at android.view.View$PerformClick.run(View.java:16966)
12-11 00:36:13.961: E/AndroidRuntime(779):  at android.os.Handler.handleCallback(Handler.java:615)
12-11 00:36:13.961: E/AndroidRuntime(779):  at android.os.Handler.dispatchMessage(Handler.java:92)
2-11 00:36:13.961: E/AndroidRuntime(779):   at android.os.Looper.loop(Looper.java:137)
12-11 00:36:13.961: E/AndroidRuntime(779):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-11 00:36:13.961: E/AndroidRuntime(779):  at java.lang.reflect.Method.invokeNative(Native Method)

WhiteNote.java

public class WhiteNote extends Fragment {
private NoteDatabase note_database;
private int i=0;
public Context context;
public NoteListAdapter noteListAdapter;
public ListView note_listview_container;
static SQLiteDatabase note_db;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.white_note, container, false);

    context=getActivity();
    note_database = new NoteDatabase(context);

    final EditText text_ed_1;
    final EditText text_ed_2;
    Button button_addNote;
    Button button_listallNote;
    Button button_delallNote;
    //final TextView note_list;
    //final LinearLayout note_container;

    text_ed_1 = (EditText)rootView.findViewById(R.id.textedit1);
    text_ed_2 = (EditText)rootView.findViewById(R.id.textedit2);
    button_addNote = (Button)rootView.findViewById(R.id.button1);
    button_listallNote = (Button)rootView.findViewById(R.id.button2);
    button_delallNote = (Button)rootView.findViewById(R.id.button3);
    //note_list=(TextView)rootView.findViewById(R.id.textview1);
    //note_container = (LinearLayout)rootView.findViewById(R.id.listcontainer);
    note_listview_container=(ListView)rootView.findViewById(R.id.note_listview);

    //showNoteList();

    button_addNote.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            note_database.open();
            note_database.createData(text_ed_1.getText().toString(),text_ed_2.getText().toString());
            //i++;
            note_database.close();
        }
    });

    button_listallNote.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            note_database.open();
            //String ds = note_database.getData();
            showNoteList();
            note_database.close();
            //note_list.setText(ds);
        }
    });

    button_delallNote.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            note_database.open();
            note_database.deleteAllNote();
            note_database.close();
        }
    });
    return rootView;
}

private void showNoteList() {

      ArrayList<NoteItems> noteList = new ArrayList<NoteItems>();
      noteList.clear();


      note_database.open();
      String[] columns = new String[] {note_database.COLUMN_ID,note_database.COLUMN_TOPIC,note_database.COLUMN_NOTECONTENT,note_database.COLUMN_NAME};
      Cursor c = note_db.query(note_database.TABLE_NOTE, columns, null, null, null, null, null);
      int iRow = c.getColumnIndex(note_database.COLUMN_ID);
      int iTopic = c.getColumnIndex(note_database.COLUMN_TOPIC);
      int iContent = c.getColumnIndex(note_database.COLUMN_NOTECONTENT);
      int iOwner = c.getColumnIndex(note_database.COLUMN_NAME);
      for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){     
          NoteItems one_rowItems = new  NoteItems();

          one_rowItems.set_rowTopic(c.getString(iTopic));
          one_rowItems.set_rowContent(c.getString(iContent));
          one_rowItems.set_rowOwner(c.getString(iOwner));
          noteList.add(one_rowItems);

      }
      c.close();

      note_database.close();

      noteListAdapter = new NoteListAdapter(getActivity(), noteList);
      note_listview_container.setAdapter(noteListAdapter);

     }
}

NoteDatabase.java

public class NoteDatabase {
private static final String DATABASE_NAME = "DB_NOTE";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NOTE = "NOTE";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TOPIC = "Topic";
public static final String COLUMN_NOTECONTENT = "Content";
public static final String COLUMN_NAME = "Name";

private static Context my_context;
static SQLiteDatabase note_sqldb;
private OpenHelper noteopenHelper;

public NoteDatabase(Context c){
    NoteDatabase.my_context = c;
}

public NoteDatabase open() throws SQLException{
    noteopenHelper = new OpenHelper(my_context);
    note_sqldb = noteopenHelper.getWritableDatabase();
    return this;
}

public void close(){
    noteopenHelper.close();
}

public long createData(String chude_note, String noidung_note) { 
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_TOPIC, chude_note);
    cv.put(COLUMN_NOTECONTENT, noidung_note);
    cv.put(COLUMN_NAME, "by Black");
    return note_sqldb.insert(TABLE_NOTE, null, cv);
}

public String getData() {
    String[] columns = new String[] {COLUMN_ID,COLUMN_TOPIC,COLUMN_NOTECONTENT,COLUMN_NAME};
    Cursor c = note_sqldb.query(TABLE_NOTE, columns, null, null, null, null, null);
    /*if(c==null)
        Log.v("Cursor", "C is NULL");*/
    String result="";
    int iRow = c.getColumnIndex(COLUMN_ID);
    int iTopic = c.getColumnIndex(COLUMN_TOPIC);
    int iContent = c.getColumnIndex(COLUMN_NOTECONTENT);
    int iOwner = c.getColumnIndex(COLUMN_NAME);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){     
        result = result +" \n"+ c.getString(iRow)
                + "\n - Topic: " + c.getString(iTopic)
                + "\n - Content: " + c.getString(iContent)
                + "\n - Owner: " + c.getString(iOwner) + "\n";
    }
    c.close();
    //Log.v("Result", result);
    return result;
}

public Cursor selectQuery(String query) {
      Cursor c1 = null;
      try {

       if (note_sqldb.isOpen()) {
           note_sqldb.close();

       }
       note_sqldb = noteopenHelper.getWritableDatabase();
       c1 = note_sqldb.rawQuery(query, null);

      } catch (Exception e) {

       System.out.println("DATABASE ERROR " + e);

      }
      return c1;

     }

/*Ham xoa 1 note */
public int deleteNote(String topic) {
    return note_sqldb.delete(TABLE_NOTE, COLUMN_TOPIC + "='" + topic + "'", null);
}

/*Ham xoa toan bo table NOTE*/
public int deleteAllNote() {
    return note_sqldb.delete(TABLE_NOTE, null, null);
}

//---------------- class OpenHelper ------------------
private static class OpenHelper extends SQLiteOpenHelper {
    public OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        arg0.execSQL("CREATE TABLE " + TABLE_NOTE + " ("
                + COLUMN_ID + " INTEGER PRIMARY KEY, "
                + COLUMN_TOPIC + " TEXT NOT NULL, "
                + COLUMN_NOTECONTENT + " TEXT NOT NULL, "
                + COLUMN_NAME + " TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        arg0.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTE);
        onCreate(arg0);
    }
}
}

NoteItems.java

public class NoteItems {

private String note_row_topic;
private String note_row_content;
private String note_row_owner;

public String get_rowTopic() {
    return note_row_topic;
}

public void set_rowTopic(String topic) {
    this.note_row_topic = topic;
}

public String get_rowContent() {
    return note_row_content;
}

public void set_rowContent(String content) {
    this.note_row_content = content;
}

public String get_rowOwner() {
    return note_row_owner;
}

public void set_rowOwner(String owner) {
    this.note_row_owner = owner;
}

@Override
public String toString() {
    return "[ Topic=" + note_row_topic + ", content =" +
            note_row_content + " , owner=" + note_row_owner + "]";
}
}

NoteListAdapter.java

public class NoteListAdapter extends BaseAdapter {

Context mcontext;
ArrayList<NoteItems> noteList;

public NoteListAdapter(Context context, ArrayList<NoteItems> list) {

    this.mcontext = context;
    noteList = list;
}

@Override
public int getCount() {

    return noteList.size();
}

@Override
public Object getItem(int position) {

    return noteList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    NoteItems noteListItems = noteList.get(position);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_row, null);

    }

    TextView noteTopic = (TextView) convertView.findViewById(R.id.row_topic);
    noteTopic.setText(noteListItems.get_rowTopic());

    TextView noteContent = (TextView) convertView.findViewById(R.id.row_content);
    noteContent.setText(noteListItems.get_rowContent());

    TextView noteOwner = (TextView) convertView.findViewById(R.id.row_owner);
    noteOwner.setText(noteListItems.get_rowOwner());

    return convertView;
}

}

0 个答案:

没有答案