我做的应用程序可以显示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>
答案 0 :(得分:0)
您没有在CursorAdapter的bindView()
中执行任何操作,因此您永远不会在行中看到任何数据。你需要做这样的事情:
@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")));
}
答案 1 :(得分:0)
在调用helper.getAll();
后,您是否在光标中有数据?如果是,请尝试在上一行之后和之前使用model.moveToFirst()
adapter = new ShoppingListAdapter(model);