0

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 :

@override
public 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 perfect
    itemAdder2.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 :(
}

@Override
public int getCount() {
    return mArray.size();
}

@Override
public Object getItem(int position) {
    return mArray.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) {
            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 JSON
    String name = "";
    String expiry = "7 days";

    // Write appropriate codes to obtain values for the string variables above
    name = (String) getItem(position);

    // Send these Strings to the TextViews for display
    holder.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 View
private 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 set
    mArray = arrayPassed;
    notifyDataSetChanged();
}

public void deleteItem(int pos){
    mArray.remove(pos);            // Ideally, shouldn't be there
    mydb.deleteItem(dbName, pos);  // Here I need to pass the appropriate dbName
    // mArray = mydb.getAllItems(); // Ideally should be there
    notifyDataSetChanged();
}

}

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. :)

Reutzesen
  • 180
  • 11

4 Answers4

1

Create a public method in the adapter to set the Db name, Eg : in the ListViewAdapter class,

public void setDbName(String dbName){
this.dbName = dbName;
}

And on the tab_selector(View v) method set the db Name to the adapter

Eg :

@override
public 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); 
    itemAdder2.updateData(currentList);
    currentListView.setAdapter(itemAdder2);
    itemAdder2.setDbName(currentDB );
}
J.R
  • 2,113
  • 19
  • 21
0

It can be bad practice but what about setting the currentDB variable public and static, so you can access it from the other class!

Roberto Anić Banić
  • 1,411
  • 10
  • 21
0

I think you can use SharedPreferences in here

Melvin Mauricio
  • 387
  • 1
  • 6
0

To me this looks like your just trying to send a String variable from one Activity to another class. Also, the string being sent is not sensitive information. Now, This is very simple and can be done using LocalBroadCastManager. The use has been well discussed here .

Community
  • 1
  • 1
Nitin Jain
  • 197
  • 1
  • 9