我知道这个问题已被多次询问但我还没有得到答案.. 我认为问题来自背景..
我的想法是,我有一个自定义列表视图,我可以使用复选框查看项目然后按“显示”,它将在自定义对话框中显示在另一个自定义列表视图中选择的项目。
我正在调试代码,使用Log.i()来了解代码“中断”的位置,并注意到getView()根本没有被调用
OnShow.java
public class OnShow extends Activity{
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
SQLiteDatabase db;
String name,quantity,price;
ListView myList;
List<ListViewItemShow> items;
View v;
public static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context,"myitems", null, 1);
Log.i("DBHelper","constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("DBHelper","onCreate()");
db.execSQL("CREATE TABLE IF NOT EXISTS items(name VARCHAR, quantity VARCHAR, price VARCHAR);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("DBHelper","onUpgrade()");
db.execSQL("DROP TABLE IF EXISTS items");
onCreate(db);
}
}
public OnShow(Context c){
Log.i("OnShow","constructor");
ourContext = c;
}
public OnShow open(){
Log.i("OnShow","open()");
ourHelper = new DBHelper(ourContext);
Log.i("OnShow","open()1");
ourDatabase = ourHelper.getReadableDatabase();
Log.i("OnShow","open()2");
return this;
}
public void close(){
Log.i("OnShow","close()");
ourHelper.close();
}
public void Add(String[][] T,int i){
for(int k=0;k<i;k++){
ContentValues cv = new ContentValues();
cv.put("name", T[k][0].toString());
cv.put("quantity",T[k][1].toString());
cv.put("price",T[k][2].toString());
ourDatabase.insert("items",null,cv);
}
}
public void showItems(Context context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.custom_popup, null);
myList = (ListView) v.findViewById(R.id.checkedItems);
items = new ArrayList<ListViewItemOrder>();
final Cursor cr= ourDatabase.rawQuery("Select * from items", null);
int count =cr.getCount();
cr.moveToFirst();
for(Integer j=0; j< count;j++){
Log.i("---------Item"+j,""+cr.getString(cr.getColumnIndex("name"))+" "+cr.getString(cr.getColumnIndex("quantity"))+" "+cr.getString(cr.getColumnIndex("price")));
items.add(new ListViewItemOrder(){{
name = cr.getString(cr.getColumnIndex("name"));
quantity = cr.getString(cr.getColumnIndex("quantity"));
price= cr.getString(cr.getColumnIndex("price"));
}});
}
CustomListViewShowAdapter listadapter = new CustomListViewShowAdapter(context, R.layout.itemorder, items);
myList.setAdapter(listadapter);
Log.i("teme","sssssssssss");
ourDatabase.close();
}
}
CustomListViewShowAdapter.java
public class CustomListViewShowAdapter extends ArrayAdapter<Item>
{
LayoutInflater inflater;
Context context;
List<ListViewItemShow> items;
public CustomListViewShowAdapter(Context context, int textViewResourceId, List<ListViewItemShow> items) {
super(context, textViewResourceId);
this.items = items;
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.i("CLVOA","Constructor");
}
@Override
public int getCount() {
Log.i("CLVOA","getCount()");
return items.size();
}
@Override
public Item getItem(int position) {
Log.i("Item","getItem");
return null;
}
@Override
public long getItemId(int position) {
Log.i("CLVOA","getItemId");
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.i("sssssssss","sssssssssss");
final ListViewItemShow holder;
View vi=convertView;
if(vi==null){
vi = inflater.inflate(R.layout.itemshow, null);
holder = new ListViewItemShow();
holder.nametext = (TextView) vi.findViewById(R.id.nameItemShow);
holder.quantitytext = (TextView) vi.findViewById(R.id.quantityItemShow);
holder.pricetext = (TextView) vi.findViewById(R.id.priceItemShow);
vi.setTag(holder);
}else{
holder = (ListViewItemShow) vi.getTag();
}
holder.nametext.setText(""+holder.name);
holder.quantitytext.setText(""+holder.quantity);
holder.pricetext.setText(""+holder.price);
return vi;
}
}
NB:
1 - 我可以远程正确连接到我的数据库并正确显示所有项目
2 - 已经尝试过invalidate()和notifyDataSetChanged()
3 - Log.i()显示它正在传递除getView()之外的所有内容
4 - getCount()返回正确的项目大小
5 - 在showItems()参数中传递的 上下文 是mainactivity.java的上下文...我想这是我的问题在哪里,但无法弄清楚如何解决它
修改
custom_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="horizontal"
>
<ListView
android:id="@+id/checkedItems"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="83dp" >
</ListView>
<Button
android:id="@+id/order_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/checkedItems"
android:layout_marginTop="775dp"
android:text="Order" />
<Button
android:id="@+id/popup_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/show_button"
android:text="Cancel" />
</RelativeLayout>
itemorder.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/nameItemShow"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="33dp"
android:layout_toLeftOf="@+id/quantityItemShow"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/quantityItemShow"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="310dp"
android:layout_toLeftOf="@+id/priceItemShow"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/priceItemShow"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="67dp"
android:layout_toRightOf="@+id/quantityShowItem"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
答案 0 :(得分:1)
在沮丧之后,我最终改变了所有代码并使用 SimpleCursorAdapter ,这对我来说很有用..
我的解决方案:
1 - 我删除了 CustomListView 适配器并在mainactivity.java中实现了这些功能
2 - 在mainactivity的onCreate()中打开连接
3 - 创建DBAdapter实例: DBAdapter myDB = new DBAdapter(this);
<强> showItems()强>
public View showItems(){
LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.custom_popup, null);
myList = (ListView) v.findViewById(R.id.checkedItems);
final Cursor cr = myDB.getAllRows();
startManagingCursor(cr);
String[] fromFieldNames = new String[]{
DBAdapter.KEY_NAME,
DBAdapter.KEY_QUANTITY,
DBAdapter.KEY_PRICE
};
int[] toViewIDs = new int [] {
R.id.nameItemShow,
R.id.quantityItemShow,
R.id.priceItemShow
};
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this,
R.layout.itemshow,
cr,
fromFieldNames,
toViewIDs
);
myList.setAdapter(myCursorAdapter);
return v;
}
<强> DBAdapter.java 强>
public class DBAdapter {
private static final String TAG = "DBAdapter";
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
public static final String KEY_NAME = "name";
public static final String KEY_QUANTITY = "quantity";
public static final String KEY_PRICE = "price";
public static final int COL_NAME = 1;
public static final int COL_QUANTITY = 2;
public static final int COL_PRICE = 3;
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_PRICE};
public static final String DATABASE_NAME = "checked";
public static final String DATABASE_TABLE = "items";
public static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE_SQL =
"create table " + DATABASE_TABLE
+ " (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_NAME + " string not null, "
+ KEY_QUANTITY + " string not null, "
+ KEY_PRICE + " string not null"
+ ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
public void close() {
myDBHelper.close();
}
public long insertRow(String name, String quantity, String price) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_QUANTITY, quantity);
initialValues.put(KEY_PRICE, price);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public boolean updateRow(long rowId, String name, String quantity, String price) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, name);
newValues.put(KEY_QUANTITY, quantity);
newValues.put(KEY_PRICE, price);
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
Log.i("Database","Created");
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(_db);
}
}
}
感谢所有试图帮助并希望此解决方案对其他人有用的人:)
答案 1 :(得分:0)
尝试返回CustomListViewShowAdapter.getItem(int position)
中的项目,而不是null
。