如何使用数据库在选项卡菜单中解决此错误?

时间:2012-05-16 14:58:48

标签: android database android-listview android-inflate

在我的应用程序中,我有标签菜单:添加标签,查看标签。

在“添加”选项卡中:我将数据添加到数据库 在“视图”选项卡中:我从数据库中选择数据到listview

现在我有问题:当我从添加标签添加数据并切换到viewtab时,我无法查看上次添加到数据库中,但如果我关闭应用并打开新的时间,我可以最后添加

请解决这个问题的任何想法

代码:

`查看标签代码:

公共类ViewEvents扩展了Activity {

DBAdapter DB=new DBAdapter(this);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewevents);

    final ListView myListView = (ListView)findViewById(R.id.MyList);

    //  final EditText myEditText = (EditText)findViewById(R.id.myEditText);

      // Create the array list of to do items
      final ArrayList<String> todoItems = new ArrayList<String>();
      final ArrayAdapter<String>  aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems);

       DB.open();
     Cursor c=DB.select();
      c.moveToFirst();
      Integer n=new Integer(c.getCount());

    for(int i=0;i<c.getCount();i++)
    {
         todoItems.add(0, c.getString(0));
         c.moveToNext();
    }


       // todoItems.add(0, c.getString(0));
        aa.notifyDataSetChanged();
        myListView.setAdapter(aa);








}

}

添加标签代码:

public View getView(int position,View convertView,ViewGroup parent){

        ButtonHolder holder2;
        if (convertView == null) {

            holder2 = new ButtonHolder();       


            convertView = mInflater.inflate(R.layout.item, null);

            holder2.caption = (Button) convertView.findViewById(R.id.Bte);

            convertView.setTag(holder2);


        } else {

            holder2 = (ButtonHolder) convertView.getTag();

        }


        holder2.caption.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub


                TextView names = holderpartner.caption;
                TextView date = holderdv.caption;
                TextView time = holderviewmtime.caption;
                String address = "sdfgkhj";

                event e=new event( names.getText().toString(), address,date.getText().toString(), new java.sql.Time(9, 30, 30));


                DB.open();
                DB.insertTask(e);


                date.setText("");
                time.setText("");

                Toast.makeText(getBaseContext(), "Done",Toast.LENGTH_LONG).show();

            }
        });`

1 个答案:

答案 0 :(得分:1)

addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);中为您的意图添加TabHostActivity标记,因此每当您选择标签时,其内容都会刷新。做类似的事情:

intent = new Intent().setClass(this, Notices.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabHost.newTabSpec("notices").setIndicator(getString(R.string.notices),
                res.getDrawable(R.drawable.ic_tab_notices))
            .setContent(intent);
        tabHost.addTab(spec);

在我的情况下,它正在发挥作用。