0

我自己新手到android,我已經完成了從數據庫中搜索特定記錄並使用簡單的遊標適配器顯示它的列表的代碼,以獲得圖像也與數據I開始使用自定義適配器,代碼工作得很好,當我嘗試從db中檢索所有數據,但是當我將參數傳遞給db中的方法以搜索特定記錄並在列表中顯示錯誤時出現如何使用customadapter從數據庫獲取特定記錄

這是我的代碼 當用戶選擇要顯示的數據時通過的代碼

Intent i= new Intent(SearchByBed.this,SearchBedView.class); 
    Bundle bbed= new Bundle(); 
    bbed.putString("bedValue", spinn_Bed.getSelectedItem().toString()); 
    i.putExtras(bbed); 
    startActivity(i); 

In這部分通過從以前的類參數爲捕捉,然後傳遞到數據庫,並想要檢索的記錄,再加上它綁定到定製適配器

my asynctask code 


public class loadSomeStuff extends AsyncTask<String, Integer, String> { 
    ProgressDialog dialog; 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     dialog = new ProgressDialog(SearchBedView.this); 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     dialog.setMax(100); 
     dialog.setMessage("Loading Data"); 
     dialog.show(); 

    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     dialog.incrementProgressBy(values[0]); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     for (int i = 0; i < 20; i++) { 
      publishProgress(5); 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     dialog.dismiss(); 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     displaydemo(); 

    } 


} 

public void displaydemo() { 
    // TODO Auto-generated method stub 

    Bundle bbedview = this.getIntent().getExtras(); 

    name = bbedview.getString("name").toString();  
    //data.setText(name); //as per log here i was getting null pointer exception but 

    //know commented as it was line from my previous code 

    info = new PropertyInfo(this); 
    info.open(); 
    cursor = info.getBed(name); 
    String[] from = new String[] { PropertyInfo.KEY_ROWID, 
      PropertyInfo.KEY_TYPE, PropertyInfo.KEY_BEDROOMS, 
      PropertyInfo.KEY_STATUS, PropertyInfo.KEY_CITY, 
      PropertyInfo.KEY_FURNISHING, PropertyInfo.KEY_TOTPRICE }; 
    int[] to = new int[] { R.id.rowId, R.id.rowtype, R.id.rowBed, 
      R.id.rowStatus, R.id.rowCity, R.id.rowFurnish, 
      R.id.rowTotalPrice }; 
    custAdapter = new CustomContactsAdapterBedView(this, R.layout.row, cursor, 
      from, to); 

    this.setListAdapter(custAdapter); 
      } 

,雖然這是mycustomadapter類

public class CustomContactsAdapterBedView extends SimpleCursorAdapter { 

    private int layout; 
    LayoutInflater inflator; 
    TextView tid,ttype,tbed,tstatus,tcity,tfurnish,ttotprice; 

    public CustomContactsAdapterBedView(Context context, int layout, Cursor     c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to, 0); 
     this.layout = layout; 
     inflator = LayoutInflater.from(context); 
    } 


    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = inflator.inflate(layout, parent, false); 
     return v; 
    } 


    @Override 
    public void bindView(View v, final Context context, Cursor c) { 
     final String id = c.getString(c.getColumnIndex(PropertyInfo.KEY_ROWID)); 
     final String type = c 
       .getString(c.getColumnIndex(PropertyInfo.KEY_TYPE)); 
     final String bed = c.getString(c 
       .getColumnIndex(PropertyInfo.KEY_BEDROOMS)); 
     final String status = c.getString(c 
       .getColumnIndex(PropertyInfo.KEY_STATUS)); 
     final String city = c 
       .getString(c.getColumnIndex(PropertyInfo.KEY_CITY)); 
     final String furnish = c.getString(c 
       .getColumnIndex(PropertyInfo.KEY_FURNISHING)); 
     final String totprice = c.getString(c 
       .getColumnIndex(PropertyInfo.KEY_TOTPRICE)); 
     final byte[] image = c.getBlob(c.getColumnIndex(PropertyInfo.IMAGE)); 
     ImageView iv = (ImageView) v.findViewById(R.id.photo); 

     if (image != null) { 
      if (image.length > 3) { 
       iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, 
         image.length)); 
      } 
     } 

     tid = (TextView) v.findViewById(R.id.rowId); 
     tid.setText(id); 

     ttype = (TextView) v.findViewById(R.id.rowtype); 
     ttype.setText(type); 

     tbed = (TextView) v.findViewById(R.id.rowBed); 
     tbed.setText(bed); 

     tstatus = (TextView) v.findViewById(R.id.rowStatus); 
     tstatus.setText(status); 

     tcity = (TextView) v.findViewById(R.id.rowCity); 
     tcity.setText(city); 

     tfurnish = (TextView) v.findViewById(R.id.rowFurnish); 
     tfurnish.setText(furnish); 

     ttotprice = (TextView) v.findViewById(R.id.rowTotalPrice); 
     ttotprice.setText(totprice); 

    } 
} 

和方法,以dB爲作爲跟隨

  public Cursor getBed(String l) { 
    String[] columns = new String[] { KEY_ROWID, KEY_TITLE, KEY_TYPE, 
      KEY_BEDROOMS, KEY_STATUS, KEY_CITY, KEY_LOCALITY, KEY_PRICE, 
      KEY_TOTPRICE, KEY_FURNISHING, KEY_BALCONIES, KEY_BATHROOM, 
      IMAGE }; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_BEDROOMS 
      + "=" + l, null, null, null, null); 
    return c; 

} 

而且在logcat的錯誤是

  05-16 20:33:43.803: E/AndroidRuntime(31444): FATAL EXCEPTION: main 
      05-16 20:33:43.803: E/AndroidRuntime(31444): java.lang.NullPointerException 
      05-16 20:33:43.803: E/AndroidRuntime(31444): at 

      com.project.realestate.SearchBedView.displaydemo(SearchBedView.java:299) 
      05-16 20:33:43.803: E/AndroidRuntime(31444): at 

     com.project.realestate.SearchBedView$loadSomeStuff.onPostExecute(SearchBedView.java:281) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 

     com.project.realestate.SearchBedView$loadSomeStuff.onPostExecute(SearchBedView.java:1) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     android.os.AsyncTask.finish(AsyncTask.java:631) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     android.os.AsyncTask.access$600(AsyncTask.java:177) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     android.os.Handler.dispatchMessage(Handler.java:99) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     android.os.Looper.loop(Looper.java:137) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     android.app.ActivityThread.main(ActivityThread.java:5041) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     java.lang.reflect.Method.invokeNative(Native Method) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     java.lang.reflect.Method.invoke(Method.java:511) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
     05-16 20:33:43.803: E/AndroidRuntime(31444): at 
     dalvik.system.NativeStart.main(Native Method) 

請幫我看看我的代碼

+0

hey somebody plz helppp – dan

+0

你應用了AsyncTask嗎? –

+0

@bhavana是的,因爲之前的數據從數據庫獲取加載我在onpreexecute和onpostexecute顯示進度條我調用方法名稱displaydemo其完整的代碼寫在頂部 – dan

回答

0
data.setText(name); 

我猜測數據的EditText或一個TextView你有對它的引用與findViewById

你有單引號括起來的字符串值,轉義序列我修改了行

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_BEDROOMS 
     + "=\'" +l+"\'", null, null, null, null); 
+0

好吧,我已經刪除數據(textview)小部件,因爲我以前的代碼格式使用它,以便問題解決,但雖然我的數據庫這樣的數據it劑量顯示,記錄 – dan

+0

我編輯了我的答 – prvn

0

其中你已經定義了「數據」的TextView(或任何部件)?

應該像來定義:

TextView的數據=(TextView的)findViewById(R.id yourTextView);

+0

好吧,我有rem覆蓋的數據(textview)小部件,因爲我以前的代碼格式使用它,以便問題得到解決,但雖然我的db有這樣的數據它dosent顯示該記錄 – dan

相關問題