Recycler视图持续刷新

时间:2017-03-27 08:59:14

标签: java android database sqlite android-recyclerview

我在MainActivity中有一个回收站视图。我正在从sqlite数据库向该回收站视图添加项目。问题是,当我们从应用程序按下按钮并再次返回应用程序时,它会不断刷新。它正在添加现有项目的副本。

这是我的MainActivity.java文件

package bk.acs;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

import bk.acs.AddingToDatabase.MyDialog;
import bk.acs.RecyclerView.Data;
import bk.acs.RecyclerView.ListItem;
import bk.acs.RecyclerView.MyAdapter;

public class MainActivity extends AppCompatActivity {
    private static final int CONTACTS_PERMISSION = 10;
    RecyclerView recview;
    MyAdapter adapter;
    Data d;
    Toolbar toolbar;
    NavigationDrawer drawer;
    String appName;
    LinearLayout main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        appName = getResources().getString(R.string.app_name);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        recview = (RecyclerView) findViewById(R.id.recview);
        drawer = (NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.navDrawer);
        drawer.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
        d = new Data(this);
        adapter = new MyAdapter(d.getList(), this);
        recview.setLayoutManager(new LinearLayoutManager(recview.getContext()));
        recview.setAdapter(adapter);
        main = (LinearLayout) findViewById(R.id.MainLayout);
        try {
            createFolders();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int res_id = item.getItemId();
        if (res_id == R.id.refresh) {
            MyDialog dialog = new MyDialog();
            dialog.show(getFragmentManager(), "myDialog");
        }
        return true;
    }

    public void updateRecView(long num, String subname) {
        disableRecView();
        adapter.listdata.add(new ListItem(subname, num + ""));
        adapter.notifyItemInserted(adapter.listdata.size());
    }

    public void disableRecView() {
        main = (LinearLayout) findViewById(R.id.MainLayout);
        main.setVisibility(View.GONE);
    }

    public void createFolders() throws IOException {
        File mainFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(), appName);
        File inputs = new File(mainFolder, "Inputs");
        File outputs = new File(mainFolder, "Outputs");
        if (mainFolder.exists()) {
            Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
            File temp = new File(inputs, "temp.txt");
            temp.createNewFile();
        } else {
            Toast.makeText(this, "Creating Folders", Toast.LENGTH_SHORT).show();
            mainFolder.mkdir();
            inputs.mkdir();
            outputs.mkdir();
            File temp = new File(inputs, "temp.txt");
            temp.createNewFile();
        }
    }

    public void fetchDataForSub(String s) {
        Intent intent = new Intent(this, SubActivity1.class);
        intent.putExtra("tv", s);
        startActivity(intent);
    }

}

这是我从我从数据库中获取项目的Data.java文件

package bk.acs.RecyclerView;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import bk.acs.MainActivity;
import bk.acs.databases.Main;

/**
 * Created by bk on 01-03-2017.
 */

public class Data {
    public static ArrayList<String> sample = new ArrayList<>();
    public static List<ListItem> list = new ArrayList<>();
    String[] cols = {"SubjectName"};
    SQLiteDatabase db;
    Context ctx;

    public Data(Context context) {
        this.ctx = context;
        Main main = new Main(context, "subjects_db", 1, 1);
        db = main.getReadableDatabase();
        Cursor cursor = db.query("SubjectsTable", cols, null, null, null, null, null);
        while (cursor.moveToNext()) {
            sample.add(cursor.getString(0));
        }
        if (sample.size() != 0) {
            ((MainActivity) context).disableRecView();
        }
    }

    public List getList() {

        for (int i = 0; i < sample.size(); i++) {
            ListItem listItem = new ListItem(sample.get(i), (i + 1) + ""); //Fixed crash
            list.add(listItem);
        }
        return list;
    }
}

这是MyAdapter.java文件

package bk.acs.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import bk.acs.MainActivity;
import bk.acs.R;
import bk.acs.SubActivity1;

/**
 * Created by bk on 01-03-2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {
    public List<ListItem> listdata;
    LayoutInflater inflater;
    Context c;

    public MyAdapter(List<ListItem> list, Context c) {
        this.inflater = LayoutInflater.from(c);
        listdata = list;
        this.c = c;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.subjects_li, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        ListItem item = listdata.get(position);
        holder.subjectName.setText(item.subName);
        holder.serialNo.setText(item.sno);
    }

    @Override
    public int getItemCount() {
        return listdata.size();
    }

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView subjectName;
        TextView serialNo;
        View container;

        public Holder(View itemView) {
            super(itemView);
            subjectName = (TextView) itemView.findViewById(R.id.subjectTitle);
            serialNo = (TextView) itemView.findViewById(R.id.sno);
            container = itemView.findViewById(R.id.cont_root);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            ((MainActivity) c).fetchDataForSub(subjectName.getText().toString());
        }
    }

}

如何防止重复项添加到此回收站视图中...提前谢谢

1 个答案:

答案 0 :(得分:1)

因为 public List getList() { list.clear(); for (int i = 0; i < sample.size(); i++) { ListItem listItem = new ListItem(sample.get(i), (i + 1) + ""); //Fixed crash list.add(listItem); } return list; } , 始终添加,因此您需要在添加项目之前清除列表。

{{1}}