项目无法添加到列表中

时间:2012-02-04 16:26:05

标签: java android sqlite listview

我正在尝试在SQLite中保存List(TextString)。但是,我现在面临的问题是,在点击“添加”按钮后,名称无法添加并显示在列表中。

这是我的代码:

MainActivity.java:

public class MainActivity extends Activity implements OnKeyListener {
NoteAdapter adapter = null;
NoteHelper helper=null;
Cursor dataset_cursor=null;
String noteID = null;


EditText editText1=null;
Button addButton;
ListView listView1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   try{
   setContentView(R.layout.main);  
   editText1 = (EditText)findViewById(R.id.myeditText1);
    addButton = (Button)findViewById(R.id.addButton);
    listView1 = (ListView)findViewById(R.id.listView1);
    helper = new NoteHelper (this);
    dataset_cursor = helper.getAll();
    startManagingCursor (dataset_cursor);


    addButton.setOnClickListener(onSave);
    editText1.setOnKeyListener(this);
    adapter = new NoteAdapter (dataset_cursor);
    listView1.setAdapter(adapter);

}

   catch (Exception e)
   { 
       Log.e("ERROR", "ERROR IN CODE:"+e.toString());
       e.printStackTrace();

   }
}



@Override
public void onDestroy(){

super.onDestroy();
helper.close();
}



private View.OnClickListener onSave = new View.OnClickListener()
{

public void onClick(View v) {

 helper.insert(editText1.getText().toString());
 dataset_cursor.requery();
 editText1.setText("");


    }


};

class NoteAdapter extends CursorAdapter
{
    NoteAdapter (Cursor c){
        super(MainActivity.this, c);

}

    @Override
    public void bindView(View row, Context ctxt, Cursor c)
    {
        NoteHolder holder = (NoteHolder)row.getTag();
        holder.populateFrom(c,helper);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row,parent, false);
        NoteHolder holder = new NoteHolder (row);
        row.setTag(holder);
        return (row);
    }
}

static class NoteHolder{
    private TextView noteText = null;

    NoteHolder(View row){
        noteText=(TextView)row.findViewById(R.id.note);}

    void populateFrom(Cursor c, NoteHelper helper){
        noteText.setText(helper.getNote(c));
    }
}

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (event.getAction()==KeyEvent.ACTION_DOWN && keyCode==KeyEvent.KEYCODE_ENTER)
{
InputMethodManager in = (InputMethodManager)      
getSystemService(Context.INPUT_METHOD_SERVICE);

in.hideSoftInputFromWindow(editText1.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS );}

    return false;
}

我的数据库 - NoteHelper.java

    class NoteHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME="note.db";
private static final int SCHEMA_VERSION=1;

public NoteHelper (Context context){

    super(context, DATABASE_NAME, null, SCHEMA_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE NOTES(_id INTEGER PRIMARY KEY AUTOINCREMENT, note   
    TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {

}

public void insert (String note){

    ContentValues cv= new ContentValues ();
    cv.put("note", note);
    getWritableDatabase().insert("Notes", "note", cv);
}

public Cursor getAll(){

return (getReadableDatabase().rawQuery("SELECT_id, note FROM Notes", null));
}

public String getNote (Cursor c){

return (c.getString(1));
}

}

main.xml中:

 <?xml version="1.0" encoding="utf-8"?>
 <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:clickable="true">

 <LinearLayout 
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"     
android:weightSum="1">

<ImageView 

android:id="@+id/phones_icon"
 android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/user"
android:layout_margin="20dp"
/>

<Button
android:text="Add New Appoinments"
android:id="@+id/addButton"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="sans"
  ></Button>

<EditText android:id="@+id/myeditText1" android:layout_height="wrap_content"   
android:layout_width="match_parent" android:inputType="textPersonName">
    <requestFocus></requestFocus>
</EditText>

<ListView 
android:id="@+id/listView1" 
android:layout_width="wrap_content" 
android:layout_height="252dp" 
android:clickable="true"></ListView>  



</LinearLayout>
</ScrollView>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView 
 android:id="@+id/note"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
android:ellipsize="end"
/>
</LinearLayout>

请指导我。谢谢。

1 个答案:

答案 0 :(得分:0)

我相信你只是错过了适配器中对notifyDatasetChanged()的调用,以便列表实际刷新:

public void onClick(View v) {
  helper.insert(editText1.getText().toString());
  dataset_cursor.requery();
  editText1.setText("");
  adapter.notifyDatasetChanged();
}