ListView不显示自定义适配器的数据

时间:2015-07-26 19:14:08

标签: android android-listview

自定义getView()的{​​{1}}方法如下:

ListViewAdapter

public class ListViewAdapter extends BaseAdapter { Context mContext; LayoutInflater mInflater; ArrayList mArray; ArrayList<Item> mArray2; DBHelper mydb; String dbName; public ListViewAdapter(Context context, LayoutInflater inflater) { mContext = context; mInflater = inflater; mArray = new ArrayList(); mArray2 = new ArrayList<>(); mydb = new DBHelper(mContext); } @Override public int getCount() { return mArray.size(); } @Override public Object getItem(int position) { return mArray.get(position); } public Item getItem2(int position) { return mArray2.get(position); } @Override public long getItemId(int position) { // your particular data set uses String IDs // but you have to put something in this method return position; } @Override public View getView(final int position, View convertView, final ViewGroup parent) { ViewHolder holder; // check if the view already exists // if so, no need to inflate and findViewById again! if (convertView == null) { // Inflate the custom row layout from your XML. convertView = mInflater.inflate(R.layout.list_item, null); // create a new "Holder" with subviews holder = new ViewHolder(); holder.itemNameView = (TextView) convertView.findViewById(R.id.item_name); holder.itemExpiryView = (TextView) convertView.findViewById(R.id.item_expiry); // Taking care of the buttons holder.editButton = (Button) convertView.findViewById(R.id.button_edit); holder.deleteButton = (Button) convertView.findViewById(R.id.button_delete); // hang onto this holder for future recycling convertView.setTag(holder); } else { // skip all the expensive inflation/findViewById // and just get the holder you already made holder = (ViewHolder) convertView.getTag(); } // Set listener on the buttons holder.editButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Edit Button CLicked", Toast.LENGTH_SHORT).show(); } }); holder.deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = getItem(position).toString(); int id = mydb.getID(dbName, name); mydb.deleteItem(dbName, id); mArray2 = mydb.getAllItemsAsCollection(dbName); notifyDataSetChanged(); Toast.makeText(mContext, "Item deleted", Toast.LENGTH_SHORT).show(); } }); // Doing for 2nd case Item _item = getItem2(position); String name2 = _item.name; System.out.println(name2); String ex = _item.expiry; System.out.println(ex); // For the second case holder.itemNameView.setText(name2); holder.itemExpiryView.setText(ex); return convertView; } // this is used so you only ever have to do // inflation and finding by ID once ever per View private static class ViewHolder { public TextView itemNameView; public TextView itemExpiryView; public Button editButton; public Button deleteButton; } public void updateData2(ArrayList<Item> arrayPassed) { // update the adapter's data set mArray2 = arrayPassed; notifyDataSetChanged(); } public void setDbName(String dbName){ this.dbName = dbName; } } 类函数DBHelper的定义如下:

getAllItemsAsCollection()

此外,public ArrayList<Item> getAllItemsAsCollection(String dbName) { ArrayList<Item> array_list = new ArrayList<Item>(); SQLiteDatabase db = this.getReadableDatabase(); Cursor res = db.rawQuery( "select * from " + dbName, null ); res.moveToFirst(); while(res.isAfterLast() == false){ String n = res.getString(res.getColumnIndex(COLUMN_NAME)); String e = res.getString(res.getColumnIndex(COLUMN_EXPIRY)); String c = dbName; Item _item = new Item(n, e, c); array_list.add(_item); res.moveToNext(); } return array_list; } 内的insertItem()函数是:

DBHelper

我为可自定义的对象处理添加了一个单独的类:

public boolean insertItem  (String dbName, String name, String expiry)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("expiry", expiry);
    db.insert(dbName, null, contentValues);
    return true;
}

public class Item { String name; String expiry; String category; Item(String n, String e, String c){ this.name = n; this.expiry = e; this.category = c; } } 内的addItem()方法就像:

MainActivity.java

我已正确地初始化每个变量。当我尝试运行我的应用程序并尝试添加新项目时,public void addItem(final View v) { AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); LinearLayout lila1 = new LinearLayout(this); lila1.setOrientation(LinearLayout.VERTICAL); final EditText name = new EditText(this); name.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS); final EditText days = new EditText(this); days.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS); TextView text_ex = new TextView(this); text_ex.setText("In how many days will it expire.."); alert.setTitle("Hello!"); alert.setMessage("What did you buy today.."); lila1.addView(name); lila1.addView(text_ex); lila1.addView(days); alert.setView(lila1); // Make an "OK" button to save the name alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Grab the EditText's input String inputName = name.getText().toString(); String daysToExpiry = days.getText().toString(); System.out.println(daysToExpiry); mydb.insertItem(currentDB, inputName, daysToExpiry); System.out.println("Worked"); // For 2nd Case currentList2 = mydb.getAllItemsAsCollection(currentDB); System.out.println("Random Musings"); itemAdder2.updateData2(currentList2); // addItemToList(inputName, v); dialog.dismiss(); } }); // Make a "Cancel" button // that simply dismisses the alert alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); alert.show(); } 只是消失,并且布局中没有显示任何内容。我之前尝试过一个简单的Dialog box,它完美无缺。这就是为什么我认为ArrayList<Strings>不应该有任何问题。可能是.xml Layout函数。请帮忙。通过这些长代码感谢您的耐心。如果需要进一步的信息,请告诉我。非常感谢。 :)

忘记为实际视图附加ListViewAdapter.updateData2()。这与.xml完美配合。我已经测试过了。当我尝试传递复杂对象时,在这种情况下,ArrayList<String>,相应地Item-class object,我的猜测是,我无法正确编写适配器部分代码。

ArrayList<Item>

0 个答案:

没有答案