我有一个Android Notes添加,删除,更新应用程序。
在我的主要活动中,我有一个Recycler视图,该视图正在使用arraylist从数据库类中检索数据。
下面是DatabaseValues类:
DatabaseValues.java
public static boolean isBalanced(String input) {
if (input == null || input.length() == 1) return false;
int size = input.length();
if (size % 2 == 1) return false;
Stack<Character> stack = new Stack<Character>();
HashMap<Character, Character> map = new HashMap<Character,
Character>();
map.put('(', ')');
map.put('[', ']');
map.put('{', '}');
for (int i = 0; i < size; i++){
char temp = input.charAt(i);
if (map.containsKey(temp)) stack.push(temp);
else if (stack.isEmpty() || map.get(stack.pop()) != temp)
return false;
}
return true;
}
此notesList将返回到MainActivity: MainActivity.java
....
initial code
....
public List getAllNotes()
{
List<Note> notesList = new ArrayList<Note>();;
Cursor cursor = database.rawQuery("SELECT * FROM note", null);
if (cursor.moveToFirst()) {
do {
Note n = new Note();
n.setId(cursor.getInt(0));
n.setTitle(cursor.getString(1));
n.setDescription(cursor.getString(2));
notesList.add(n);
} while (cursor.moveToNext());
}
return notesList;
}
我已经有一个单独的NotesAdapter类:
public class MainActivity extends AppCompatActivity
{
protected RecyclerView recyclerView;
protected DatabaseValues dbvalues;
protected List<Note> noteList;
protected NotesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent newNote = new Intent(MainActivity.this, EditNoteActivity.class);
startActivity(newNote);//onclick new note activity opens
}
});
recyclerView = findViewById(R.id.recyclerview);
noteList = new ArrayList<>();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
//ANR
final List<Note> n = dbvalues.getAllNotes(); //retrieving values from DB
if (!n.isEmpty()) //error occurs since there are no notes in db
{
for (int i = 0; i < n.size(); i++)
{
noteList.add(n.get(i));
}
}
if(noteList.isEmpty())
{
recyclerView.setAdapter(null);
}
else
{
adapter = new NotesAdapter(noteList, this);
recyclerView.setAdapter(adapter);
}
}
请帮助!当最初为空时,如何通过arraylist将数据库值正确加载到recyclerview中。也是上面应用的正确方法吗??
这是代码出错的主要区域:
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder>
{
protected List<Note> notesList;
protected Context context;
public NotesAdapter(List<Note> notesList, Context context)
{
this.notesList = notesList;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row_layout, viewGroup, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i)
{
Note note = notesList.get(i);
myViewHolder.id.setText(note.getId());
myViewHolder.title.setText(note.getTitle());
myViewHolder.description.setText(note.getDescription());
}
@Override
public int getItemCount() {
return notesList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
public TextView title, description, id;
public MyViewHolder(View view)
{
super(view);
id = (TextView) view.findViewById(R.id.idTextView);
title = (TextView) view.findViewById(R.id.titleTextView);
description = (TextView) view.findViewById(R.id.descriptionTextView);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Toast.makeText(context, "Clicked Row :" + getAdapterPosition(), Toast.LENGTH_LONG).show();
Intent editNote = new Intent(context,EditNoteActivity.class);
Bundle extras = new Bundle();
context.startActivity(editNote);
}
});
}
}
}