gpt4 book ai didi

java - 回收者 View 不断刷新

转载 作者:行者123 更新时间:2023-12-02 13:35:25 25 4
gpt4 key购买 nike

我在 MainActivity 中有一个回收器 View 。我正在从 sqlite 数据库向该回收器 View 添加项目。问题是,当我们从应用程序按后退按钮并再次返回应用程序时,它会不断刷新。它正在添加现有项目的重复项。

这是我的 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());
}
}

}

如何防止重复项添加到此回收器 View 中。提前致谢

最佳答案

因为public static List<ListItem> list = new ArrayList<>(); ,始终会添加,因此您需要在添加项目之前清除列表。

    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;
}

关于java - 回收者 View 不断刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43042011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com