如果按下后退按钮,则刷新上一个活动

时间:2018-04-01 15:24:27

标签: android refresh

我的应用有2项活动。第一个Activity(称为MainActivity)有一个RecyclerView,它显示数据库中的一些数据,以及一个启动第二个活动的按钮

MainActivity.java

class MainActivity extends AppCompatActivity {

    RecyclerView recView;
    MyAdapter adapter;
    ArrayList<String> data;
    Button btn1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data = new ArrayList<String>();
        btn1 = (Button) findViewById(R.id.btn1);
        recView = (RecyclerView) findViewById(R.id.my_recycler_view);
        recView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent iii = new Intent(MainActivity.this, SecondActivity.class);

                // position is the index of the selected item on RecyclerView
                Bundle bnd = new Bundle();
                bnd.putString("selected_postition", ""+position);

                iii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                iii.putExtras(bnd);

                startActivity(iii, 2112);
            }
        });

        fetchData();
    }

    public void fetchData(){
        data.clear()
        String mURL = "http://my.dev.server/api/data";

        // read table userData and display it on
        for (int x = 0; x < .....){
            data.add(....)
        }

        adapter = new MyAdapter(data);
        recView.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 2112){
            if (resultCode == Activity.RESULT_OK){
                int idx = Integer.parseInt(intent.getStringExtra("result"));
                data.remove(idx);
                adapter = new MyAdapter(data);
                adapter.notifyDataSetChanged();

            }
            else if (resultCode == Activity.RESULT_CANCELED){
            }
        }

    }
}

SeccondActivity.java

AlertDialog.Builder builder = new AlertDialog.Builder(FeedbackActivity.this);
builder.setTitle("");
builder.setMessage("Thank you for your feedback");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        Intent returnIntent = getIntent();
        returnIntent.putExtra("result", selected_position);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }
    });

AlertDialog alert = builder.create();
alert.show();

它不会丢弃已删除的行,而是会重复显示现有项目(在Android 4.4.2上测试)。那么如何正确刷新RecyclerView呢?

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

<ul id="stt">
    <li>First</li>
    <li></li>
    <li>Third</li>
    <li>Fourth</li>
    <li></li>
    <li>Fifth</li>
</ul>

尝试将 btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fetchData(); Intent iii = new Intent(MainActivity.this,SecondActivity.class); // position is the index of the selected item on RecyclerView Bundle bnd = new Bundle(); bnd.putString("selected_postition", ""+position); startActivity(iii, 2112); } }); } 移到fetchData()内。在onClick

fetchData();