2013-10-11 34 views
0

我正在尋找一個示例/教程來插入數據在sqlLite數據庫中,並在列表視圖中顯示數據。那麼這是一個很好的例子:http://androidsolution4u.blogspot.it/2013/09/android-populate-listview-from-sqlite.html它的工作原理。我在我的工作中實現了代碼。問題是,如果我想要插入另一個沒有出現在列表視圖中,並且活動是空的。我添加了一個名爲地址的字段,如同其他人一樣,並且在代碼的每個部分中,我都添加了需要從其他人當然改變名字)。但是,當我嘗試添加項目不出現。當然,我用新字段更新xml文件。我不能編寫整個代碼,因爲它太多了。但如果有人能幫我找到出路,我會寫出所需的代碼部分。謝謝 編輯:代碼是相同的例子,但與我需要添加的字段。在db中插入字段後Listview爲空?

的DisplayActivity

public class DisplayActivity extends Activity { 

    private DbHelper mHelper; 
    private SQLiteDatabase dataBase; 

    private ArrayList<String> userId = new ArrayList<String>(); 
    private ArrayList<String> user_fName = new ArrayList<String>(); 
    private ArrayList<String> user_lName = new ArrayList<String>(); 
    private ArrayList<String> user_address = new ArrayList<String>(); 

    private ListView userList; 
    private AlertDialog.Builder build; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.display_activity); 

     userList = (ListView) findViewById(R.id.List); 

     mHelper = new DbHelper(this); 

     //add new record 
     findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       Intent i = new Intent(getApplicationContext(), AddActivity.class); 
       i.putExtra("update", false); 
       startActivity(i); 

      } 
     }); 

     //click to update data 
     userList.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

       Intent i = new Intent(getApplicationContext(), AddActivity.class); 
       i.putExtra("Fname", user_fName.get(arg2)); 
       i.putExtra("Lname", user_lName.get(arg2)); 
       i.putExtra("address", user_address.get(arg2)); 
       i.putExtra("ID", userId.get(arg2)); 
       i.putExtra("update", true); 
       startActivity(i); 

      } 
     }); 

     //long click to delete data 
     userList.setOnItemLongClickListener(new OnItemLongClickListener() { 

      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) { 

       build = new AlertDialog.Builder(DisplayActivity.this); 
       build.setTitle("Delete " + user_fName.get(arg2) + " " + user_lName.get(arg2) + " " + user_address(arg2)); 
       build.setMessage("Do you want to delete ?"); 
       build.setPositiveButton("Yes",new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 

           Toast.makeText(getApplicationContext(), 
             user_fName.get(arg2) + " " 
               + user_lName.get(arg2) 
               + user_address.get(arg2) 
               + " is deleted.", 3000).show(); 

           dataBase.delete(
             DbHelper.TABLE_NAME, 
             DbHelper.KEY_ID + "=" 
               + userId.get(arg2), null); 
           displayData(); 
           dialog.cancel(); 
          } 
         }); 

       build.setNegativeButton("No", new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = build.create(); 
       alert.show(); 

       return true; 
      } 
     }); 
    } 

    @Override 
    protected void onResume() { 
     displayData(); 
     super.onResume(); 
    } 

    /** 
    * displays data from SQLite 
    */ 
    private void displayData() { 
     dataBase = mHelper.getWritableDatabase(); 
     Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DbHelper.TABLE_NAME, null); 

     userId.clear(); 
     user_fName.clear(); 
     user_lName.clear(); 
     user_address.clear(); 
     if (mCursor.moveToFirst()) { 
      do { 
       userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID))); 
       user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME))); 
       user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME))); 

      } while (mCursor.moveToNext()); 
     } 
     DisplayAdapter disadpt = new DisplayAdapter(DisplayActivity.this,userId, user_fName, user_lName, user_address); 
     userList.setAdapter(disadpt); 
     mCursor.close(); 
    } 

} 

的DisplayAdapter

public class DisplayAdapter extends BaseAdapter { 
    private Context mContext; 
    private ArrayList<String> id; 
    private ArrayList<String> firstName; 
    private ArrayList<String> lastName; 
    private ArrayList<String> address; 


    public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> fname, ArrayList<String> lname, ArrayList<String> address) { 
     this.mContext = c; 

     this.id = id; 
     this.firstName = fname; 
     this.lastName = lname; 
     this.address = address; 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return id.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public View getView(int pos, View child, ViewGroup parent) { 
     Holder mHolder; 
     LayoutInflater layoutInflater; 
     if (child == null) { 
      layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      child = layoutInflater.inflate(R.layout.listcell, null); 
      mHolder = new Holder(); 
      mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id); 
      mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName); 
      mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName); 
      mHolder.txt_address = (TextView) child.findViewById(R.id.txt_address); 
      child.setTag(mHolder); 
     } else { 
      mHolder = (Holder) child.getTag(); 
     } 
     mHolder.txt_id.setText(id.get(pos)); 
     mHolder.txt_fName.setText(firstName.get(pos)); 
     mHolder.txt_lName.setText(lastName.get(pos)); 
     mHolder.txt_address.setText(address.get(pos)); 

     return child; 
    } 

    public class Holder { 
     TextView txt_id; 
     TextView txt_fName; 
     TextView txt_lName; 
     TextView txt_address; 
    } 

} 

的DbHelper

public class DbHelper extends SQLiteOpenHelper { 
    static String DATABASE_NAME="userdata"; 
    public static final String TABLE_NAME="user"; 
    public static final String KEY_FNAME="fname"; 
    public static final String KEY_LNAME="lname"; 
    public static final String KEY_ID="id"; 
    public static final String KEY_ADDRESS="address"; 
    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_FNAME+" TEXT, "+KEY_LNAME+" TEXT, "+KEY_ADDRESS+" TEXT)"; 
     db.execSQL(CREATE_TABLE); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); 
     onCreate(db); 

    } 

} 

而且AddActivity

public class AddActivity extends Activity implements OnClickListener { 
private Button btn_save; 
private EditText edit_first,edit_last,edit_address; 
private DbHelper mHelper; 
private SQLiteDatabase dataBase; 
private String id,fname,lname,address; 
private boolean isUpdate; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_activity); 

     btn_save=(Button)findViewById(R.id.save_btn); 
     edit_first=(EditText)findViewById(R.id.frst_editTxt); 
     edit_last=(EditText)findViewById(R.id.last_editTxt); 
     edit_address=(EditText)findViewById(R.id.address_editTxt); 

     isUpdate=getIntent().getExtras().getBoolean("update"); 
     if(isUpdate) 
     { 
      id=getIntent().getExtras().getString("ID"); 
      fname=getIntent().getExtras().getString("Fname"); 
      lname=getIntent().getExtras().getString("Lname"); 
      address=getIntent().getExtras().getString("address"); 
      edit_first.setText(fname); 
      edit_last.setText(lname); 
      edit_address.setText(address); 

     } 

     btn_save.setOnClickListener(this); 

     mHelper=new DbHelper(this); 

    } 

    // saveButton click event 
    public void onClick(View v) { 
     fname=edit_first.getText().toString().trim(); 
     lname=edit_last.getText().toString().trim(); 
     address=edit_address.getText().toString().trim(); 
     if(fname.length()>0 && lname.length()>0 && address.length()>0) 
     { 
      saveData(); 
     } 
     else 
     { 
      AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AddActivity.this); 
      alertBuilder.setTitle("Invalid Data"); 
      alertBuilder.setMessage("Please, Enter valid data"); 
      alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 

       } 
      }); 
      alertBuilder.create().show(); 
     } 

    } 

    /** 
    * save data into SQLite 
    */ 
    private void saveData(){ 
     dataBase=mHelper.getWritableDatabase(); 
     ContentValues values=new ContentValues(); 

     values.put(DbHelper.KEY_FNAME,fname); 
     values.put(DbHelper.KEY_LNAME,lname); 
     values.put(DbHelper.KEY_ADDRESS,address); 

     System.out.println(""); 
     if(isUpdate) 
     {  
      //update database with new data 
      dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null); 
     } 
     else 
     { 
      //insert data into database 
      dataBase.insert(DbHelper.TABLE_NAME, null, values); 
     } 
     //close database 
     dataBase.close(); 
     finish(); 


    } 

} 

就是這樣。我找不到問題。幫助?

+0

好吧,我會用代碼來更新這個問題,只需一分鐘 –

+0

更新代碼 –

+0

任何解決方案?似乎是正確的代碼。 –

回答

0

在displayData遺忘()user_address.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ADDRESS)));

+0

沒有再次出現。即使我添加這一行。我只有一個空白的活動。 –

+0

那麼?任何其他想法? –

+0

添加字段地址後,你有沒有添加任何數據到數據庫? – ramaral