1

我知道这个问题已被多次问过,但我还没有得到答案。我认为问题出在上下文中。

这个想法是我有一个customlistview,我可以用复选框检查项目,然后按显示,它将显示在自定义对话框中的另一个customlistview中选择的项目..

我通过使用 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;
}

}

注意:

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>
4

3 回答 3

1

受挫后,我最终更改了所有代码并使用SimpleCursorAdapter,这对我有用..

我的解决方案:

1-我去掉了CustomListView适配器,实现了mainactivity.java中的功能

2-在mainactivity的onCreate()中打开连接

3-创建 DBAdapter 的实例:DBAdapter myDB = new DBAdapter(this);

显示项目()

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);
    }
}
}

感谢所有尝试提供帮助的人,并希望此解决方案对其他人有用:)

于 2013-08-18T11:29:17.477 回答
0

尝试返回项目CustomListViewShowAdapter.getItem(int position)而不是null.

于 2013-08-17T16:55:20.433 回答
0

我有同样类型的问题。对于其他试图找到答案的人:我的答案是更改适配器表单的公共构造函数

super(context, textViewResourceId);

进入

super(context, textViewResourceId, items); // The list of objects used to populate the listview
于 2016-12-05T09:28:21.673 回答