2012-10-28 204 views
0

我做的應用程序,它可以顯示所有的數據在listView。但是,當我編譯我的應用程序,沒有任何顯示在ListView中。程序中沒有任何錯誤,我真的不確定哪個部分是錯誤的。沒有項目顯示在輸出ListView

任何人都知道我的程序中有什麼問題或錯誤?

main.java 

package com.example.shoppingassistantnew; 

import java.util.ArrayList; 



import greendroid.app.GDActivity; 
import greendroid.app.GDListActivity; 
import greendroid.widget.ActionBar; 
import greendroid.widget.ActionBarItem; 
import greendroid.widget.ActionBarItem.Type; 
import greendroid.widget.QuickAction; 
import greendroid.widget.QuickActionBar; 
import greendroid.widget.QuickActionGrid; 
import greendroid.widget.QuickActionWidget; 
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.graphics.Color; 
import android.graphics.ColorFilter; 
import android.graphics.LightingColorFilter; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CursorAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.support.v4.app.NavUtils; 

public class main extends GDActivity{ 

    private static final int HOME = 2; 

    private static final int ADD = 1; 

    private static final int EXPAND = 3; 



    Cursor model = null; 
    ProductDatabaseHelper helper = null; 
    ShoppingListAdapter adapter=null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setActionBarContentView(R.layout.shoppinglist); 


     initActionBar(); 

     helper = new ProductDatabaseHelper(this); 
     ListView listView = (ListView) findViewById(R.id.shoppingList); 

     model = helper.getAll(); 
     startManagingCursor(model); 
     adapter = new ShoppingListAdapter(model); 
     listView.setAdapter(adapter); 


    } 
    public void onDestroy(){ 
     super.onDestroy(); 
     helper.close(); 
    } 





    private OnQuickActionClickListener mActionListener = new OnQuickActionClickListener() { 
     public void onQuickActionClicked(QuickActionWidget widget, int position) { 
      if (position == 0) { 
       Intent intent2 = new Intent(main.this, AddShoppingList.class); 
       startActivity(intent2); 
      } 
     } 
    }; 



    private void initActionBar() { 
     // TODO Auto-generated method stub 
     // this.getActionBar().setType(ActionBar.Type.Empty); 
     addActionBarItem(Type.Add, ADD); 
     addActionBarItem(Type.Edit, EXPAND); 
    } 

    @Override 
    public boolean onHandleActionBarItemClick(ActionBarItem item, int position) { 
     // TODO Auto-generated method stub 
     switch (item.getItemId()) { 
     case ADD: 
      Intent intent = new Intent(main.this, AddShoppingList.class); 
      startActivity(intent); 
      break; 
     case HOME: 
      Intent intent1 = new Intent(main.this, main.class); 
      startActivity(intent1); 
      break; 
     case EXPAND: 
      onShowGrid(item.getItemView()); 
      break; 

     } 
     return super.onHandleActionBarItemClick(item, position); 
    } 

    class ShoppingListAdapter extends CursorAdapter{ 

     public ShoppingListAdapter(Cursor c) { 
      super(main.this, c); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public void bindView(View row, Context ctxt, Cursor c) { 
      // TODO Auto-generated method stub 
      ShoppingListHolder holder=(ShoppingListHolder)row.getTag(); 
     } 


     @Override 
     public View newView(Context ctxt, Cursor cursor, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      LayoutInflater inflater=getLayoutInflater(); 
      View row=inflater.inflate(R.layout.row,parent,false); 
      ShoppingListHolder holder=new ShoppingListHolder(row); 
      row.setTag(holder); 
      return(row); 

     } 
    } 
     static class ShoppingListHolder{ 
      private TextView barcode=null; 
      private TextView format=null; 

      ShoppingListHolder(View row){ 
       barcode=(TextView)row.findViewById(R.id.barcode); 
       format=(TextView)row.findViewById(R.id.format); 

      } 
      void populateFrom(Cursor c,ProductDatabaseHelper helper){ 
       barcode.setText(helper.getBarcode(c)); 
       format.setText(helper.getFormat(c)); 


      } 

     } 




    } 

addShoppingList.java

package com.example.shoppingassistantnew; 

import com.cyrilmottier.android.greendroid.R; 

//import com.example.shoppingassistantnew.main.ProductData; 


import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.ListView; 
import greendroid.app.GDActivity; 

public class AddShoppingList extends Activity implements OnClickListener{ 
    //private static final ProductData mProductData = new ProductData(); 
    ProductData mProductData=new ProductData(); 
    Button mSaveBttn; 
    EditText mBarcodeEdit; 
    EditText mFormatEdit; 
    ProductDatabaseHelper helper=null; 

    //Cursor model=null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.addshoppinglist); 

     mSaveBttn = (Button) findViewById(R.id.saveBttn); 
     mSaveBttn.setOnClickListener(this); 
     dt=(DatePicker)findViewById(R.id.datePicker1); 
     helper=new ProductDatabaseHelper(this); 

    } 


    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch(v.getId()){ 
     case R.id.saveBttn: 

      mBarcodeEdit = (EditText) findViewById(R.id.shoppingListNameEdit); 
      mFormatEdit = (EditText) findViewById(R.id.shoppingDateEdit); 
      mProductData.setBarcode(mBarcodeEdit.getText().toString()); 
      mProductData.setFormat(mFormatEdit.getText().toString()); 
      helper.insert(mBarcodeEdit.getText().toString(),mFormatEdit.getText().toString()); 
      showInfoDialog(this, "Success", "Product saved successfully"); 
      //model.requery(); 
     break; 
     } 
    } 
     private void showInfoDialog(Context context, String title, String information) { 
      new AlertDialog.Builder (context) 
      .setMessage(information) 
      .setTitle(title) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 


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

       } 
      }).show(); 
     } 

} 

ProductDatabase.java 



    package com.example.shoppingassistantnew; 

    import android.content.ContentValues; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteDatabase.CursorFactory; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.util.Log; 

    class ProductDatabaseHelper extends SQLiteOpenHelper { 
     private static final String PRODUCT_TABLE = "products"; 
     private static final String DATABASE_NAME = "spot_pay.db"; 
     private static final int SCHEMA_VERSION = 1; 
     private SQLiteDatabase db; 

     private static final String TAG = null; 

     public ProductDatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null,SCHEMA_VERSION); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
      db.execSQL("CREATE TABLE products (_id INTEGER PRIMARY KEY AUTOINCREMENT,barcode TEXT ,format TEXT);"); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
      db.execSQL("drop table if exists " + PRODUCT_TABLE); 
      onCreate(db); 
     } 

     /* 
     * public ProductDatabase(Context context) { ProductDatabaseHelper helper = 
     * new ProductDatabaseHelper(context); db = helper.getWritableDatabase(); 
     * 
     * } 
     */ 
     public Cursor getAll() { 
      // TODO Auto-generated method stub 
      return (getReadableDatabase() 
        .rawQuery("SELECT _id, barcode, format FROM products ORDER BY barcode", 
        null)); 
     } 

     public void insert(String barcode, String format) { 
      ContentValues vals = new ContentValues(); 
      vals.put("barcode", barcode); 
      vals.put("format", format); 
     getWritableDatabase().insert("products", null, vals); 


     } 





     public String getBarcode(Cursor c) { 
      // TODO Auto-generated method stub 
      return(c.getString(1)); 
     } 

     public String getFormat(Cursor c) { 
      // TODO Auto-generated method stub 
      return(c.getString(2)); 
      } 
     } 

    row.xml 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="4dip"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     <TextView 
      android:id="@+id/barcode" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
      android:textStyle="bold" 
      android:singleLine="true" 
      android:ellipsize="end" 

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

</LinearLayout> 

ShoppingList.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="fill_parent" 
    android:orientation="vertical" > 

<ListView 
    android:id="@+id/shoppingList" 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    /> 




</LinearLayout> 

output screenshot

+0

你有數據的調用helper.getAll()後的遊標; –

回答

0

你是不是在做你的CursorAdapter的bindView()什麼,所以你永遠不會看到任何數據你的行。你需要做這樣的事情:調用helper.getAll();

@Override 
public void bindView(View row, Context ctxt, Cursor c) { 
    ShoppingListHolder holder=(ShoppingListHolder)row.getTag(); 
    holder.barcode.setText(c.getString(c.getColumnIndex("barcode"))); 
    holder.format.setText(c.getString(c.getColumnIndex("format"))); 
} 
+0

是的,這應該是甚至完成。 –

+0

終於解決了它...我添加代碼holder.populateFrom(c,helper);謝謝你們...... – jacky

+0

我有另一個問題...在我添加數據後,我的listview不會自動升級......有什麼方法可以解決它? – jacky

0

你在你的光標的數據,如果是試圖前行後和之前使用model.moveToFirst()

adapter = new ShoppingListAdapter(model);