刷新android活动并在同一活动中显示更新的详细信息。当我按下更新按钮时

时间:2017-08-03 08:41:12

标签: java android sqlite

我更新了sqlite数据库中的表, 但我不知道如何获得更新的细节 在按下更新按钮时刷新的同一活动。

我试过下面的代码

  finish();
  startActivity(getintent());

它再次加载相同的Activity并且什么也没显示。

我知道这不是正确的方法。我需要你的帮助。

提前致谢。

这是我的代码。

package com.example.eightyeight.monthmaths;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;


public class secondactivity extends AppCompatActivity {
String selected = "", yy = "", mm = "";
FloatingActionButton search;
Spinner sid;
List<String> li;

TableLayout tbid;


EditText year;
Cursor cur;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactivity);
    sid = (Spinner) findViewById(R.id.monthspinid);
    year = (EditText) findViewById(R.id.yearid);

    search = (FloatingActionButton) findViewById(R.id.goid);

    tbid = (TableLayout) findViewById(R.id.tbid);


    li = new ArrayList<String>();
    li.add("Month...");
    li.add("Jan");
    li.add("Feb");
    li.add("Mar");
    li.add("Apr");
    li.add("May");
    li.add("Jun");
    li.add("Jul");
    li.add("Aug");
    li.add("Sep");
    li.add("Oct");
    li.add("Nov");
    li.add("Dec");


    ArrayAdapter<String> ad;
    ad = new ArrayAdapter<String>(secondactivity.this, android.R.layout.simple_spinner_item, li);
    ad.setDropDownViewResource(android.R.layout.simple_spinner_item);
    sid.setAdapter(ad);

    sid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            selected = (String) sid.getSelectedItem();
            Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            yy = year.getText().toString();
            mm = (String) sid.getSelectedItem();

            SQLiteDatabase db;
            db = openOrCreateDatabase("monthapp", MODE_PRIVATE, null);
            cur = db.rawQuery("SELECT * FROM monthvaluesOrgin WHERE curmonth='" + mm + "' AND curyear='" + yy + "'", null);
            while (cur.moveToNext()) {


                TableRow tr = new TableRow(secondactivity.this);
                TextView tv1 = new TextView(secondactivity.this);
                tv1.setWidth(100);
                tv1.setText(cur.getString(1));
                tr.addView(tv1);


                final TextView tv2 = new TextView(secondactivity.this);
                tv2.setText(cur.getString(2));
                tv2.setWidth(100);
                tr.addView(tv2);


                final EditText et = new EditText(secondactivity.this);
                tr.addView(et);

                final TextView t21 = new TextView(secondactivity.this);
                t21.setText(cur.getString(0));

                tr.addView(t21);


                final TextView tv3 = new TextView(secondactivity.this);
                tv3.setText(cur.getString(3));
                tv3.setWidth(100);
                tr.addView(tv3);


                Button b = new Button(secondactivity.this);
                b.setText("update");
                tr.addView(b);

                tbid.addView(tr);
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String a = (String) tv2.getText();
                        String totl = (String) tv3.getText();
                        int totv = Integer.parseInt(totl);

                        int b = Integer.parseInt(a);
                        Editable edi = et.getText();
                        int c = Integer.parseInt(String.valueOf(edi));

                        String id = (String) t21.getText();

                        int add = b + c;


                        if (add >= totv) {
                            Toast.makeText(getApplicationContext(), "large", Toast.LENGTH_SHORT).show();

                        } else {


                            SQLiteDatabase dbup;

                            dbup = openOrCreateDatabase("monthapp", MODE_PRIVATE, null);
                            dbup.execSQL("UPDATE monthvaluesOrgin SET currentpaid='" + add + "' WHERE id='" + id + "'");
                            Toast.makeText(getApplicationContext(), "added" + add + "\n" + id, Toast.LENGTH_LONG).show();
                            finish();
                            startActivity(getIntent());


                        }


                        // Toast.makeText(getApplicationContext(),"add="+add,Toast.LENGTH_SHORT).show();


                    }
                });


            }


        }
    });


}

}

2 个答案:

答案 0 :(得分:2)

为什么要再次呼叫 startActivity &amp;再次?

好方法

使用 notifyDataSetChanged()

  

通知附加的观察者基础数据已经存在   已更改,任何反映数据集的视图都应自行刷新。

ad.notifyDataSetChanged();

<强>代码

 dbup = openOrCreateDatabase("monthapp", MODE_PRIVATE, null);
                        dbup.execSQL("UPDATE monthvaluesOrgin SET currentpaid='" + add + "' WHERE id='" + id + "'");
                        Toast.makeText(getApplicationContext(), "added" + add + "\n" + id, Toast.LENGTH_LONG).show();
                         ad.notifyDataSetChanged(); // This

答案 1 :(得分:-1)

创建方法以更新适配器中的列表,并在更新DB后调用它

 public void refresh(Cursor newCursor) {
   if (mCursor != null) mCursor.close();
        mCursor = newCursor;
      if (newCursor != null) 
        this.notifyDataSetChanged();
       }