-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListAdapter.java
More file actions
66 lines (54 loc) · 1.91 KB
/
ListAdapter.java
File metadata and controls
66 lines (54 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.example.codelist;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private Context mContext;
private Cursor mCursor;
public ListAdapter (Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView mtextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mtextView = itemView.findViewById(R.id.textView);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.single_item_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (!mCursor.moveToPosition(position)) {
return;
}
Long id = mCursor.getLong(mCursor.getColumnIndex(IdentifiersClass.Collection._ID));
String name = mCursor.getString(mCursor.getColumnIndex(IdentifiersClass.Collection.COLUMN_NAME));
holder.mtextView.setText(name);
holder.itemView.setTag(id);
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor (Cursor newCursor) {
if (mCursor != null) {
mCursor.close();
}
mCursor = newCursor;
if (newCursor != null) {
notifyDataSetChanged();
}
}
}