叫我知道這個問題已經被問多次,但我沒有得到我的答案了.. 我認爲這個問題是從上下文..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>
爲什麼的GetItem和getitemid返回null和0? –
這不是問題所在。getItem和getItemID不是首先調用的。 無論如何,我實現SimpleCursorAdapter而不是使用更容易理解/調試和對數據庫有用的CustomListViewAdapter – Odin