我的currentDB
中有一个变量MainActivity.java
。我根据用户选择不同的选项卡将此变量启动到不同的值。相应的代码是:
@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);
}
我在DBHelper.java
中有一个方法,理想情况下将表名和项id作为输入,并删除指定表中的相应行。
public Integer deleteItem (String dbName, int id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(dbName,
"id = ? ",
new String[] { Integer.toString(id) });
}
这是从表中获取所有数据的另一个功能,它可以很好地工作。
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;
}
我创建了一个自定义的ListViewAdapter
,以显示存储在数据库中的数据。我已通过onClick()
功能提供了相关按钮以进行编辑和删除。现在,我的问题是确定在ListViewAdapter
内引用哪个数据库。我无法将currentDB
信息从MainActivity.java
传递给ListViewAdapter.java
。请帮忙。提前致谢。 ListViewAdapter.java
的代码段如下:
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();
}
}
如果您需要额外的东西,请告诉我。任何改述或主题,标签更改都是受欢迎的。作为一个菜鸟,没有能力如何传达。感谢您的帮助。 :)
答案 0 :(得分:1)
在适配器中创建一个公共方法来设置Db名称, 例如:在ListViewAdapter类中,
public void setDbName(String dbName){
this.dbName = dbName;
}
在tab_selector(View v)方法中,将db Name设置为适配器
例如:
@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 );
}
答案 1 :(得分:0)
这可能是不好的做法但是如何将currentDB变量设置为public和static,以便可以从其他类访问它!
答案 2 :(得分:0)
我认为你可以在这里使用SharedPreferences
答案 3 :(得分:0)
对我来说,这看起来就像是你试图将一个String变量从一个Activity发送到另一个类。此外,发送的字符串不是敏感信息。现在,这非常简单,可以使用LocalBroadCastManager完成。该用法已得到充分讨论 here .