标签云

微信群

扫码加入我们

WeChat QR Code

I have a variable currentDB in my MainActivity.java. I am initiating this variable to different values depending upon the user choosing different tabs. The corresponding code is :@overridepublic void tab_selector(View v) {switch (v.getId()) {case R.id.veg_tab:setContentView(R.layout.vegetable);currentListView = (ListView) findViewById(R.id.veg_list);currentDB = "veg";break;case R.id.meat_tab :setContentView(R.layout.meat_drawer);currentListView = (ListView) findViewById(R.id.meat_list);currentDB = "meat";break;default:break;}currentList = mydb.getAllItems(currentDB);// Definition below, works perfectitemAdder2.updateData(currentList);currentListView.setAdapter(itemAdder2);}I have a method in DBHelper.java which ideally takes a table name and the item id as input and deletes the corresponding row from the specified table.public Integer deleteItem (String dbName, int id){SQLiteDatabase db = this.getWritableDatabase();return db.delete(dbName,"id = ? ",new String[] { Integer.toString(id) });}This is one more function to obtain all data from a table and it works perfectly. public ArrayList<String> getAllItems(String dbName){ArrayList<String> array_list = new ArrayList<String>();//hp = new HashMap();SQLiteDatabase db = this.getReadableDatabase();Cursor res =db.rawQuery( "select * from " + dbName, null );res.moveToFirst();while(res.isAfterLast() == false){array_list.add(res.getString(res.getColumnIndex(COLUMN_NAME)));res.moveToNext();}return array_list;}I have created a customized ListViewAdapter in order to display the data stored in the databases. I have provided with relevant buttons for editing and deletion through onClick() functionality. Now, my problem is identifying which database to refer inside this ListViewAdapter. I cannot pass the currentDB information from MainActivity.java to ListViewAdapter.java. Please help. Thanks in advance. Code snippet for ListViewAdapter.java as follows :public class ListViewAdapter extends BaseAdapter {Context mContext;LayoutInflater mInflater;ArrayList mArray;DBHelper mydb;String dbName;public ListViewAdapter(Context context, LayoutInflater inflater) {mContext = context;mInflater = inflater;mArray = new ArrayList();mydb = new DBHelper(mContext);dbName = "veg";// Didn't know what to do, tried to initialize manually, din't work :(}@Overridepublic int getCount() {return mArray.size();}@Overridepublic Object getItem(int position) {return mArray.get(position);}@Overridepublic long getItemId(int position) {// your particular data set uses String IDs// but you have to put something in this methodreturn position;}@Overridepublic 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 subviewsholder = new ViewHolder();holder.itemNameView = (TextView) convertView.findViewById(R.id.item_name);holder.itemExpiryView = (TextView) convertView.findViewById(R.id.item_expiry);// Taking care of the buttonsholder.editButton = (Button) convertView.findViewById(R.id.button_edit);holder.deleteButton = (Button) convertView.findViewById(R.id.button_delete);// hang onto this holder for future recyclingconvertView.setTag(holder);} else {// skip all the expensive inflation/findViewById// and just get the holder you already madeholder = (ViewHolder) convertView.getTag();}// Set listener on the buttonsholder.editButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(mContext, "Edit Button CLicked", Toast.LENGTH_SHORT).show();}});holder.deleteButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {deleteItem(position);// Potential problem here??Toast.makeText(mContext, "Item deleted", Toast.LENGTH_SHORT).show();}});// More code after this// Grab the title and author from the JSONString name = "";String expiry = "7 days";// Write appropriate codes to obtain values for the string variables abovename = (String) getItem(position);// Send these Strings to the TextViews for displayholder.itemNameView.setText(name);holder.itemExpiryView.setText(expiry);return convertView;}// this is used so you only ever have to do// inflation and finding by ID once ever per Viewprivate static class ViewHolder {public TextView itemNameView;public TextView itemExpiryView;public Button editButton;public Button deleteButton;}public void updateData(ArrayList arrayPassed) {// update the adapter's data setmArray = arrayPassed;notifyDataSetChanged();}public void deleteItem(int pos){mArray.remove(pos);// Ideally, shouldn't be theremydb.deleteItem(dbName, pos);// Here I need to pass the appropriate dbName// mArray = mydb.getAllItems(); // Ideally should be therenotifyDataSetChanged();}}Please let me know if you need anything extra. Any rephrase or topic, tag changes are welcome. Being a noob, not capable how exactly to convey. Appreciate your help. :)


Thanks a lot. Simple, easy and worked beautifully. :)

2019年04月20日48分46秒

You are welcome friend, happy to hear that :)

2019年04月19日48分46秒