2012-02-04 114 views
0

我想在SQLite中保存列表(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> 

請指導我。提前感謝。

回答

0

我相信你只是缺少在適配器調用notifyDatasetChanged()因此這個列表實際上刷新:

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

我已經加入adapter.notifyDatasetChanged();我的代碼。但我仍然無法解決它。名稱仍然不能添加.. :( – 2012-02-04 17:35:47