2013-08-17 78 views
1

叫我知道這個問題已經被問多次,但我沒有得到我的答案了.. 我認爲這個問題是從上下文..getView()不CustomListViewAdapter

的想法是我有一個自定義列表視圖,我可以檢查項目,然後按下顯示,它會顯示在另一個自定義列表中選擇的項目在自定義對話框中。

我通過使用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:

我可以遠程正確連接到我的數據庫並顯示正確的Ë項目得到了

2-已經嘗試過無效()和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

爲什麼的GetItem和getitemid返回null和0? –

+0

這不是問題所在。getItem和getItemID不是首先調用的。 無論如何,我實現SimpleCursorAdapter而不是使用更容易理解/調試和對數據庫有用的CustomListViewAdapter – Odin

回答

1

挫折後,我最終改變我所有的代碼,並使用SimpleCursorAdapter併爲我做的伎倆..

解決方案對我來說:

1,我刪除了CustomListView適配器並實現主要功能。的java

2開在OnCreate()在MainActivity的連接

3-創建將對DBAdapter的一個實例:將對DBAdapter MYDB =新將對DBAdapter(本);

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

感謝大家誰試圖幫助,並希望這個解決方案將是有用的人:)

0

嘗試在CustomListViewShowAdapter.getItem(int position),而不是null返回項目。

+0

已經嘗試過使用: ((CustomListViewOrderAdapter)items).getItem(position); 沒有工作... getItem的Log.i()首先不被調用 – Odin

+0

等待。它看起來不會將佈局分配給活動。您需要重寫Activity.onCreate()並調用setContentView(R.layout.custom_popup)。然後你可以在那裏調用findViewById()。這裏是一個例子:http://developer.android.com/training/basics/activity-lifecycle/starting.html#Create –

+0

我也試過這個.. onCreate永遠不會被調用。 非常感謝您的幫助 – Odin

0

我有相同類型的問題。對於其他人誰正在努力尋找這個問題的答案:我的回答是改變公共構造爲適配器形式

super(context, textViewResourceId); 

super(context, textViewResourceId, items); // The list of objects used to populate the listview