所以我有2项活动。
第一个(ActivityOne)显示一个listview,其中包含来自SQLite游标的数据和一个按钮。 单击该按钮,我想将一个项目添加到列表视图中,因此我显示第二个活动(ActivityTwo),其中包含许多editTexts和一个保存按钮,用于在数据库中进行保存。
但我想要的是:
将新项目保存到数据库后,ActivityTwo应该关闭,并且应该显示ActivityOne以及来自数据库的刷新内容
。 这看似合理的工作流程。我该如何实现? ActivityOne代码:
public class ActivityOne extends Activity {
private ArrayList<String> idclient = new ArrayList<String>();
private ArrayList<String> numeclient = new ArrayList<String>();
private ArrayList<String> tipclient = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView mylist = (ListView) findViewById(R.id.lv_clienti);
LoadList();
Button btnex = (Button) findViewById(R.id.btnNewCli);
btnex.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View aView)
{
Toast.makeText(getApplicationContext(), "Add new client... " , Toast.LENGTH_SHORT).show();
Intent toAnotherActivity = new Intent(aView.getContext(), NewClientActivity.class);
startActivity(toAnotherActivity);
}
}
);
}
public void LoadList(){
SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor mCursor = db.rawQuery("select idclient,nameclient,typeclient from clienti order by numeclient" , null);
idclient.clear();
numeclient.clear();
tipclient.clear();
if (mCursor.moveToFirst()) {
do {
idclient.add(Integer.toString(mCursor.getInt(0)));
nameclient.add(mCursor.getString(1));
typeclient.add(mCursor.getString(2));
} while (mCursor.moveToNext());
}
DisplayClientiAdapter disadpt = new DisplayClientiAdapter(ClientiActivity.this,idclient,nameclient, typeclient);
ListView lv = (ListView) findViewById(R.id.lv_clienti);
lv.setAdapter(disadpt);
mCursor.close();
db.close();
}
}
在ActivityTwo中,我点击了一下按钮:
db.execSQL("insert into clients (idclient, nameclient,typeclient,...");
DisplayClientiAdapter da = new DisplayClientiAdapter(getApplicationContext());
da.notifyDataSetChanged();
finish();
displayAdapter也是这样的:
public class DisplayClientiAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> idclient;
private ArrayList<String> numeclient;
private ArrayList<String> tipclient;
public DisplayClientiAdapter(Context c){
this.mContext = c;
}
public DisplayClientiAdapter(Context c, ArrayList<String> idclient, ArrayList<String> numeclient, ArrayList<String> tipclient) {
this.mContext = c;
this.idclient = idclient;
this.numeclient = numeclient;
this.tipclient = tipclient;
}
public int getCount() {
return idclient.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.clienti_item, null);
mHolder = new Holder();
mHolder.txt_idclient = (TextView) child.findViewById(R.id.tv_cl_id);
mHolder.txt_numeclient = (TextView) child.findViewById(R.id.tv_cl_nume);
mHolder.txt_tipclient = (TextView) child.findViewById(R.id.tv_cl_tip);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_idclient.setText(idclient.get(pos));
mHolder.txt_numeclient.setText(numeclient.get(pos));
mHolder.txt_tipclient.setText(tipclient.get(pos));
return child;
}
public class Holder {
TextView txt_idclient;
TextView txt_numeclient;
TextView txt_tipclient;
}
当然它不会像这样工作。列表没有刷新...我认为它与displayAdapter有关!?!?! 我无法调用LoadList方法,因为它是静态的或类似的......
请帮忙。 谢谢
答案 0 :(得分:1)
适配器不是问题。您必须在onresume方法中调用Loadlist()而不是ActivityOne中的oncreate方法。它会工作。
答案 1 :(得分:0)
首先,看看这两篇文章:
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
http://www.doubleencore.com/2013/06/context/
如果您有父视图,则不应在inflate方法中使用null
来扩充您的观看次数。
此外,使用应用程序上下文进行充气可能会导致奇怪的行为,因为它可能无法使用您在Activity
的应用清单中设置的正确主题。
另一方面 - 为什么不使用CursorAdapter代替BaseAdapter
?
适配器的问题是,您没有设置数据! :)
///编辑: 我检查了错误的活动 - 你为什么要在那里创建第二个适配器?
最简单的解决方案是将LoadList()移动到onStart
。
如果你想做得对,你应该使用ContentObserver
和(可能)CursorAdapter
。