My Recyclerview list stopped working after implementing SQLite database - java

I'm building a to do list app. I'm learning SQLite databases and I'm trying to save the user input into an SQLite database table and also display it in my recyclerview list. When I did this without the SQLite, it worked so the problem is obviously with my implementation of the database table. The app is supposed to add the data into the list per button click:
addingItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(itemsInput.getText() != null){
items.add(new todo(itemsInput.getText().toString()));
itemsInput.setText("");
}else {
Toast.makeText(MainActivity.this, "Please enter something to do", Toast.LENGTH_SHORT).show();
}
itemAdapter.notifyDataSetChanged();
}
});
But now it does not do that. The app does not crash. The problem is just that my list shows absolutely no data. The following are my relevant Java files:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase sqLiteDatabase;
private List<todo> items = new ArrayList<>();
private ItemAdapter itemAdapter;
private RecyclerView listItemsRecyclerView;
EditText itemsInput;
Button addingItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemsInput = (EditText)findViewById(R.id.to_do_editText);
addingItems = (Button)findViewById(R.id.to_do_btn);
listItemsRecyclerView = (RecyclerView) findViewById(R.id.to_do_list);
ToDoListDatabaseHelper databaseHelper = new ToDoListDatabaseHelper(this);
sqLiteDatabase = databaseHelper.getWritableDatabase();
items = new ArrayList<>();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
listItemsRecyclerView.setLayoutManager(layoutManager);
listItemsRecyclerView.setItemAnimator(new DefaultItemAnimator());;
itemAdapter = new ItemAdapter(items);
listItemsRecyclerView.setAdapter(itemAdapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
});
addingItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(itemsInput.getText().length() > 0){
addNewToDo(itemsInput.getText().toString());
itemsInput.setText("");
}else {
Toast.makeText(MainActivity.this, "Please enter something to do", Toast.LENGTH_SHORT).show();
}
itemAdapter.notifyDataSetChanged();
}
});
}
private List<todo> getAllToDos(){
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + ToDoContract.ToDoEntry.TABLE_NAME, new String[]{});
List<todo> todos = new ArrayList<>();
todo todo;
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_NAME));
int priority = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_PRIORITY));
int timestamp = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_TIMESTAMP));
todo = new todo(name);
todos.add(todo);
} while (cursor.moveToNext());
}
cursor.close();
}
return todos;
}
private long addNewToDo(String name){
ContentValues cv = new ContentValues();
cv.put(ToDoContract.ToDoEntry.COLUMN_TODO_NAME, name);
return sqLiteDatabase.insert(ToDoContract.ToDoEntry.TABLE_NAME, null, cv);
}
}
My custom adapter:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<todo> todoList;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView toDoTextView;
public ViewHolder(View itemView) {
super(itemView);
toDoTextView = (TextView) itemView.findViewById(R.id.to_do);
}
}
public ItemAdapter(List<todo> todoList) {
this.todoList = todoList;
}
#Override
public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
todo toDo = todoList.get(position);
holder.toDoTextView.setText(toDo.getToDo());
}
#Override
public int getItemCount() {
return todoList.size();
}
}
I ran unit tests on my SQLite classes so there is no issue with them.

You should not use Cursor as the data source for Adapter.Why don't you just fetch data(todos) from cursor then put the data into Adapter? As we known, cursor should be CLOSED at the end of data reading. You should code like that:
private List<todo> getAllToDos() {
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + ToDoContract.ToDoEntry.TABLE_NAME, new String[]{});
List<todo> todos = new ArrayList<>();
todo todo;
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_NAME));
int priority = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_PRIORITY));
int timestamp = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_TIMESTAMP));
todo = new todo(name);
todo.setPriority(priority);
todo.setTimestamp(timestamp);
todos.add(todo);
} while (cursor.moveToNext());
}
cursor.close();
}
return todos;
}
And your ItemAdapter will be simpler.
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<todo> todoList;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView toDoTextView;
public ViewHolder(View itemView) {
super(itemView);
toDoTextView = (TextView) itemView.findViewById(R.id.to_do);
}
}
public ItemAdapter(List<todo> todoList) {
this.todoList = todoList;
}
#Override
public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
todo toDo = todoList.get(position);
holder.toDoTextView.setText(toDo.getToDo());
}
#Override
public int getItemCount() {
return todoList.size();
}
}
There are some other changes.But I think you can fix them as I told you above.If you have any other question about that, please tell me.I will help you.

Try this
public void update()
{
cursor.requery();
notifyDataSetChanged();
}

Related

Fetching image using Picasso

Here in the given below code, I am trying to display in the image in recyclerview in the imageview after fetching it using picasso, it won't show error or app won't crash either but image won't be displayed, it either shows:
"I/Choreographer: Skipped 30 frames! The application may be doing too much work on its main thread."
Or
W/RecyclerView: No adapter attached; skipping layout
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
LayoutInflater inflater;
List<Lists> lst;
private ItemClickListsner mItemListener;
public Adapter(Context ctx, List<Lists> lst, ItemClickListsner itemClickListsner) {
this.inflater = LayoutInflater.from(ctx);
this.lst = lst;
this.mItemListener = itemClickListsner;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// final Lists temp = lst.get(position);
holder.id.setText(lst.get(position).getId());
holder.name.setText(lst.get(position).getName());
holder.prc.setText(lst.get(position).getPrc());
holder.add.setText(lst.get(position).getAddress());
holder.description.setText(lst.get(position).getDes());
holder.park.setText(lst.get(position).getGarage());
holder.net.setText(lst.get(position).getNet());
holder.email.setText(lst.get(position).getMail());
holder.number.setText(lst.get(position).getPnumber());
holder.post.setText(lst.get(position).getTle());
holder.date.setText(lst.get(position).getCrt());
Picasso.with(inflater.getContext())
.load(lst.get(position).getMimage())
.placeholder(R.drawable.bed)
.fit()
.into(holder.ImgView);
holder.itemView.setOnClickListener(view -> {
mItemListener.onItemClick(lst.get(position));
});
}
#Override
public int getItemCount() {
return lst.size();
}
public interface ItemClickListsner{
void onItemClick(Lists lst);
}
public void filterList(ArrayList<Lists> filteredList) {
lst = filteredList;
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView id, name, add, prc, description, park, net, post,email,number,date;
ImageView ImgView;
// CardView cardView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
id = itemView.findViewById(R.id.textViewRecy1);
name = itemView.findViewById(R.id.textViewRecy2);
prc = itemView.findViewById(R.id.textViewRecy3);
add = itemView.findViewById((R.id.textViewRecy4));
description = itemView.findViewById(R.id.textViewRecy5);
park = itemView.findViewById(R.id.textViewRecy6);
net = itemView.findViewById(R.id.textViewRecy7);
post = itemView.findViewById(R.id.textViewRecy8);
email = itemView.findViewById(R.id.textViewRecy9);
number = itemView.findViewById(R.id.textViewRecy10);
date = itemView.findViewById(R.id.textViewRecy11);
ImgView = itemView.findViewById(R.id.ImageRecy);
}
}
}
Activity Code
private void extractList() {
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
//Log.d("tags", "error: " + jsonObject);
Lists lists = new Lists();
lists.setName(jsonObject.getString("title").toString());
lists.setAddress(jsonObject.getString("location").toString());
lists.setPrc(jsonObject.getString("price").toString());
lists.setDes(jsonObject.getString("description").toString());
lists.setGarage(jsonObject.getString("parking").toString());
lists.setNet(jsonObject.getString("internet").toString());
lists.setMail(jsonObject.getString("email").toString());
lists.setPnumber(jsonObject.getString("phone_number").toString());
lists.setTle(jsonObject.getString("poster").toString());
lists.setCrt(jsonObject.getString("created").toString());
lists.setMimage(jsonObject.getString("photo1").toString());
String p = jsonObject.getString("photo1").toString();
Log.e("kk","msg"+p);
lst.add(lists);
} catch (JSONException e) {
e.printStackTrace();
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter = new Adapter(getApplicationContext(), lst, new Adapter.ItemClickListsner() {
#Override
public void onItemClick(Lists lst) {
// startActivity(new Intent(HomeActivity.this,roomsDesc.class));
Intent intent = new Intent(HomeActivity.this, roomsDesc.class);
intent.putExtra("title",lst.getName());
intent.putExtra("price",lst.getPrc());
intent.putExtra("location",lst.getAddress());
intent.putExtra("description",lst.getDes());
intent.putExtra("parking",lst.getGarage());
intent.putExtra("internet",lst.getNet());
intent.putExtra("email",lst.getMail());
intent.putExtra("phone_number",lst.getPnumber());
intent.putExtra("poster",lst.getTle());
intent.putExtra("created",lst.getCrt());
intent.putExtra("photo1",lst.getMimage());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
HomeActivity.this.startActivity(intent);
}
});
recyclerView.setAdapter(adapter);
}

How to load pdf files to recycler view in fragment?

ello guys good day I'm trying to make an app using android java but I'm stuck any idea would be appreciated
I'm trying to access all pdf files in android device using the media store and cursor but it's not working the aim is to display all pdf files in a recycler view on a fragment the issue here is that it never displays meaning maybe the data wasn't passed because when the arraylist is populated manually it displays otherwise non I've checked the log but I can't seem to find anything.
This my fragment code
public class AllFragment extends Fragment {
View view;
private static final String TAG = "MyActivity";
ArrayList<String> pdfList;
private final int STORAGE_PERMISSION_CODE = 1;
private Activity mActivity;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
mActivity = (Activity) context;
}
public void checkPermission(){
if ((ContextCompat.checkSelfPermission(mActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED))
{
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(mActivity)
.setTitle("Permission needed")
.setMessage("Allow "+getResources().getString(R.string.app_name)+" to access your storage?")
.setPositiveButton("ok", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
)
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
Toast.makeText(mActivity, "Please allow this permission!", Toast.LENGTH_SHORT).show();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
}
//checkPermission();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.AllFragment, container, false);
//pdfList = getPdfList();
//buildData();
//LinearLayoutManager
//pdfList = new String[] {"one", "two", "three", "fou4", "five", "six"};
getPdfList();
initRecyclerView(view);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkPermission();
}
//RecyclerView
public void initRecyclerView(View view){
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.allPdfsRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
AllPdfAdapter adapter = new AllPdfAdapter(pdfList);
recyclerView.setAdapter(adapter);
}
public ArrayList<String> getPdfList() {
pdfList = new ArrayList<>();
Uri collection;
final String[] projection = new String[]{
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.MIME_TYPE,
};
final String sortOrder = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
final String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ?";
final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
final String[] selectionArgs = new String[]{mimeType};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
}else{
collection = MediaStore.Files.getContentUri("external");
}
try (Cursor cursor = getActivity().getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder)) {
assert cursor != null;
if (cursor.moveToFirst()) {
int columnData = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
int columnName = cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME);
//ArrayList pdfList = new ArrayList<>();
do {
pdfList.add((cursor.getString(columnData)));
System.out.println(cursor.getString(columnData));
Log.d(TAG, "getPdf: " + cursor.getString(columnData));
//you can get your pdf files
} while (cursor.moveToNext());
}
}
return pdfList;
}
}
This my adapter code
public class AllPdfAdapter extends RecyclerView.Adapter<AllPdfAdapter.My_ViewHolder>
{
private ArrayList<String> data;
public AllPdfAdapter(ArrayList<String>data){
this.data = data;
}
#Override
public AllPdfAdapter.My_ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
{
LayoutInflater li = LayoutInflater.from(p1.getContext());
View v = li.inflate(R.layout.allList,p1,false);
// TODO: Implement this method
return new My_ViewHolder(v);
}
#Override
public void onBindViewHolder(AllPdfAdapter.My_ViewHolder holder, int position)
{
// TODO: Implement this method
String title = data.get(position);
holder.ttt.setText(title);
}
#Override
public int getItemCount()
{
// TODO: Implement this method
return data.size();
}
public class My_ViewHolder extends RecyclerView.ViewHolder{
ImageView iii;
TextView ttt;
public My_ViewHolder (View v){
super(v);
iii = (ImageView) v.findViewById(R.id.img);
ttt = (TextView) v.findViewById(R.id.title);
}
}
}
Your answers will be appreciated.

How to load data into recyclerView

In my application i want get some data from server and show into recyclerView. For application architecture i used MVP
I wrote below codes, but after loaded data from server, not show any data into recyclerView!
I used debug mode and show me data in this break point
public void add(List<DataItem> list) {
list.addAll(list);
notifyDataSetChanged();
}
but not show me data into recyclerView!
My Activity codes :
public class ListFragment extends Fragment implements ActiveTestsContract.View {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tester_active_tests, container, false);
//Initialize
init();
//User token log
if (!App.isEmptyString(App.getPrefs("JWT")) || !App.getPrefs("JWT").equals(ConstKeys.EMPTY)) {
userToken = App.getPrefs("JWT");
}
//Load data
getData();
return view;
}
#Override
public void updateTestsList(Data data, int page) {
testerDashboard_emptyLay.setVisibility(View.GONE);
activeTests_pullToLoader.setVisibility(View.VISIBLE);
//Get lasted page
if (page == data.getLastPage()) {
isHasLoadedAll = true;
activeTests_pullToLoader.setComplete();
}
adapter.add(data.getData());
//Complete items
isLoading = false;
nextPage = page + 1;
activeTests_pullToLoader.setComplete();
}
#Override
public void init() {
context = getActivity();
handler = new Handler(Looper.getMainLooper());
testsPresenter = new ActiveTestsPresenter(this, 3);
testerDashboard_loader = view.findViewById(R.id.testerDashboard_loader);
activeTests_pullToLoader = view.findViewById(R.id.activeTests_pullToLoader);
testerDashboard_emptyLay = view.findViewById(R.id.testerDashboard_emptyLay);
emptyLayout_editProfileBtn = view.findViewById(R.id.emptyLayout_editProfileBtn);
layoutManager = new LinearLayoutManager(context);
recyclerView = activeTests_pullToLoader.getRecyclerView();
//Adapter
adapter = new TesterActiveRecyclerAdapter(activeModel, context);
//Init recycler and adapter
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
activeTests_pullToLoader.setColorSchemeResources(R.color.colorPrimary);
}
private void getData() {
activeTests_pullToLoader.isLoadMoreEnabled(true);
activeTests_pullToLoader.setPullCallback(new PullCallback() {
#Override
public void onLoadMore() {
isLoading = true;
//Call api
testsPresenter.testsListResponse(App.recipesApi, userToken, nextPage);
}
#Override
public void onRefresh() {
adapter.clear();
isHasLoadedAll = false;
isLoading = true;
//Call api
testsPresenter.testsListResponse(App.recipesApi, userToken, 1);
}
#Override
public boolean isLoading() {
return isLoading;
}
#Override
public boolean hasLoadedAllItems() {
return isHasLoadedAll;
}
});
activeTests_pullToLoader.initLoad();
}
Adapter codes:
public class TesterActiveRecyclerAdapter extends RecyclerView.Adapter<TesterActiveRecyclerAdapter.ViewHolder> {
private List<DataItem> list;
private Context context;
public TesterActiveRecyclerAdapter(List<DataItem> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_tester_test_list, parent, false);
return new ViewHolder(view);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DataItem item = list.get(position);
//Name
holder.txt_name.setText(item.getTitle());
//Conditions
if (!App.isEmptyString(item.getOs()) && !App.isEmptyString(item.getType()) && !App.isEmptyString(item.getBrowser())) {
holder.txt_condition.setText(item.getType() + " | " + item.getOs() + " | " + item.getBrowser());
}
//Button actions
holder.setState(item.getState(), position);
//Price
holder.rowTests_priceTxt.setText(item.getPrice() + " Dollar");
//Animate items
Animation animation = AnimationUtils.loadAnimation(App.context,
(position > list.size() - 1) ? R.anim.down_from_top : R.anim.up_from_bottom);
holder.itemView.startAnimation(animation);
Toast.makeText(context, ""+list.get(0).getId(), Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return list.size();
}
public void add(List<DataItem> list) {
list.addAll(list);
notifyDataSetChanged();
}
public void clear() {
list.clear();
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ViewGroup root;
private TextView txt_name, txt_value, txt_condition, rowTests_priceTxt;
private RoundTextView rowTests_button;
ViewHolder(View view) {
super(view);
root = (ViewGroup) view;
txt_name = view.findViewById(R.id.txtTestListTitle);
txt_value = view.findViewById(R.id.txtTestListSublist);
txt_condition = view.findViewById(R.id.txtTestListSublist2);
rowTests_priceTxt = view.findViewById(R.id.rowTests_priceTxt);
rowTests_button = view.findViewById(R.id.rowTests_button);
}
}
How can i fix it?
You are adding data again in the same list passed in the parameter. Try to replace the below code
public void add(List<DataItem> list) {
list.addAll(list);
notifyDataSetChanged();
}
with
public void add(List<DataItem> list) {
this.list.addAll(list);
notifyDataSetChanged();
}
Clear the current list
Add the data to the adapter's list.
public void add(List<DataItem> list) {
clear();
this.list.addAll(list);
notifyDataSetChanged();
}
Your approach didn't work because you were updating the very list that you were passing in the function and not the adapter's list.
First add all data into list & than notify your adapter , after this set your adapter to recyclerview.
list.addAll(data)
adapter.notifyDataSetChanged()
val layoutManager = LinearLayoutManager(activity)
recyclerview.isNestedScrollingEnabled = false
recyclerview.layoutManager = layoutManager
recyclerview.itemAnimator = DefaultItemAnimator()
recyclerview.adapter = mAdapter

Why do I have to restart my app for my RecyclerView to display data from SQLite?

EDIT 1: I used debugging statements and it turns out that when I add an item to the database it is successful. However, the updateUI() method is not called because what I am launching is a DialogFragment() so my ListActivity does not enter a paused state. But, if I exit the app and launch again the adapter gets updated. So now the question becomes how do I update the adapter after exiting the dialog fragment?
I'm building a simple list app and added SQLite database to store task object. I am successfully adding items to the database, but they do not show up immediately in the RecyclerView I am using to show all tasks. It is only when I completely exit from the app and reload that tasks items show up in the RecyclerView.
ListFragment.java this will create the RecyclerView
public class ListFragment extends Fragment {
private static final String DEBUG_LOG = "debug_message";
private static final String ADD_DIALOG = "add_dialog";
private static final int REQUEST_TITLE = 0;
private RecyclerView mRecyclerView;
private TaskAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list, container, false);
mRecyclerView = view.findViewById(R.id.the_task_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), linearLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
updateUI();
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == ListFragment.REQUEST_TITLE) {
UUID taskId = (UUID) data.getSerializableExtra(EditTaskFragment.EXTRA_TASK_ID);
String newTaskName = data.getStringExtra(EditTaskFragment.EXTRA_TASK_TITLE);
if(newTaskName != null) {
Task task = TaskLab.get(getActivity()).getTask(taskId);
task.setTitle(newTaskName);
TaskLab.get(getActivity()).updateTask(task);
}
updateUI();
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_list_item, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.add_new_task:
FragmentManager manager = getFragmentManager();
AddTaskFragment dialog = new AddTaskFragment();
dialog.show(manager, ADD_DIALOG);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onResume() {
super.onResume();
updateUI();
}
public void updateUI() {
// Retrieves ArrayList<Task>
List<Task> taskList = TaskLab.get(getActivity()).getTaskList();
if(mAdapter == null) {
mAdapter = new TaskAdapter(taskList);
mRecyclerView.setAdapter(mAdapter);
}else {
mAdapter.setCrimes(taskList);
mAdapter.notifyDataSetChanged();
}
}
private class TaskAdapter extends RecyclerView.Adapter<TaskHolder>{
private List<Task> mTaskList;
public TaskAdapter(List<Task> taskList) {
mTaskList = taskList;
}
#Override
public TaskHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.task_list_layout, parent, false);
return new TaskHolder(view);
}
#Override
public void onBindViewHolder(TaskHolder holder, int position) {
final Task currentTask = mTaskList.get(position);
holder.bindData(currentTask);
final TextView taskTitle = holder.mTaskTitle;
holder.mSolved.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked) {
taskTitle.setPaintFlags(taskTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
currentTask.setSolved(true);
}else {
taskTitle.setPaintFlags(taskTitle.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
}
});
}
#Override
public int getItemCount() {
return mTaskList.size();
}
public void setCrimes(List<Task> taskList) {
mTaskList = taskList;
}
}
private class TaskHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Task mTask;
private TextView mTaskTitle;
private CheckBox mSolved;
private static final String EDIT_DIALOG = "edit_dialog";
public TaskHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mTaskTitle = itemView.findViewById(R.id.task_title);
mSolved = itemView.findViewById(R.id.task_solved);
}
public void bindData(Task task) {
mTask = task;
mTaskTitle.setText(mTask.getTitle());
mSolved.setChecked(mTask.isSolved());
}
#Override
public void onClick(View view) {
if(!mSolved.isChecked()) {
FragmentManager manager = getFragmentManager();
EditTaskFragment dialog = EditTaskFragment.newInstance(mTask.getId());
dialog.setTargetFragment(ListFragment.this, REQUEST_TITLE);
dialog.show(manager, EDIT_DIALOG);
}
}
}
AddTaskFragment.java this will allow the user to add a task item to the RecyclerView
public class AddTaskFragment extends DialogFragment {
private EditText mTaskTitle;
private void addTask() {
if(!mTaskTitle.getText().toString().equals("")) {
Task task = new Task();
task.setTitle(mTaskTitle.getText().toString());
TaskLab.get(getActivity()).addTask(task);
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//return super.onCreateDialog(savedInstanceState);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_task_dialog,null);
mTaskTitle = view.findViewById(R.id.task_title);
return new AlertDialog.Builder(getActivity())
.setView(view)
.setTitle(R.string.add_new_task_dialog_title)
.setPositiveButton(R.string.add_new_task_dialog_positive, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
addTask();
}
})
.create();
}
}
TaskLab.java this is a singleton class
public class TaskLab {
private static TaskLab sTaskLab;
private Context mContext;
private SQLiteDatabase mDatabase;
private TaskLab(Context context){
mContext = context.getApplicationContext();
mDatabase = new TaskBaseHelper(mContext).getWritableDatabase();
}
public static TaskLab get(Context context) {
if(sTaskLab == null) {
sTaskLab = new TaskLab(context);
}
return sTaskLab;
}
public List<Task> getTaskList(){
List<Task> taskList = new ArrayList<Task>();
TaskCursorWrapper cursor = queryTasks(null, null);
// If there is something in the table query it
try {
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
Task task = cursor.getTask();
taskList.add(task);
cursor.moveToNext();
}
} finally {
cursor.close();
}
return taskList;
}
public void addTask(Task task) {
ContentValues values = getContentValues(task);
mDatabase.insert(TaskTable.NAME, null, values);
}
public void updateTask(Task task) {
ContentValues values = getContentValues(task);
String uuidString = task.getId().toString();
mDatabase.update(TaskTable.NAME,values, TaskTable.Cols.UUID + " = ?", new String[] {uuidString});
}
public Task getTask(UUID id){
TaskCursorWrapper cursor = queryTasks(TaskTable.Cols.UUID + " = ?",
new String[] {id.toString()});
try {
if(cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return cursor.getTask();
} finally {
cursor.close();
}
}
private static ContentValues getContentValues(Task task) {
ContentValues values = new ContentValues();
values.put(TaskTable.Cols.UUID, task.getId().toString());
values.put(TaskTable.Cols.TITLE, task.getTitle());
values.put(TaskTable.Cols.SOLVED, task.isSolved());
return values;
}
private TaskCursorWrapper queryTasks(String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(TaskTable.NAME,
null,
whereClause,
whereArgs,
null,
null,
null);
return new TaskCursorWrapper(cursor);
}
}
It must be reCreate your adapter in updateUI every time from the beginning
Try it like this and see. In your onCreateView() of fragment before updateUI() add below line of code.
Note: Create taskList as global variable.
mRecyclerView = view.findViewById(R.id.the_task_list);
mAdapter = new TaskAdapter(taskList);
mRecyclerView.setAdapter(mAdapter);
updateUI();
After that create your updateUI() method as below
public void updateUI() {
taskList.clear;
taskList = TaskLab.get(getActivity()).getTaskList();
mAdapter.notifyDataSetChanged();
}
Update:
As per your updated question, declare one global variable in ListFragment as
public static ListFragment listFragment;
In your onCreate method of ListFragment after inflating view add below line of code.
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list, container, false);
listFragment = this;
Then change your addTask method in AddTaskFragment as below.
private void addTask() {
if(!mTaskTitle.getText().toString().equals("")) {
Task task = new Task();
task.setTitle(mTaskTitle.getText().toString());
TaskLab.get(getActivity()).addTask(task);
//import listfragment from your ListFragment
listFragment.updateUI();
}
}

How to pass id instead of position using recyclerView in android

I have pass position in recyclerView click but some kind of problem pass to position to display wrong data so how can i pass people id in recyclerView.
I m new in android programming
recyclerView Item Click
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
buildCustomAdapter = new BuildCustomAdapter(this, peopleList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
buildCustomAdapter.notifyDataSetChanged();
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(buildCustomAdapter);
buildCustomAdapter.setOnItemClickListener(new BuildCustomAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
detailPeople(position);
}
});
private void detailPeople(int position) {
Intent intent = new Intent(this, AddDetail.class);
intent.putExtra("peopleID", position);
startActivity(intent);
}
Model.class
public class People implements Serializable {
private String peopleImage;
private String peopleName;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}
public String getPeopleName() {
return peopleName;
}
public void setPeopleImage(String peopleImage) {
this.peopleImage = peopleImage;
}
public String getPeopleImage() {
return peopleImage;
}
}
Adapter code
public class BuildCustomAdapter extends RecyclerView.Adapter<BuildCustomAdapter.MyViewHolder> implements Filterable {
private List<People> peopleList;
private List<People> peopleListCopy;
private ItemFilter mFilter = new ItemFilter();
private OnItemClickListener mOnItemClickListener;
private Context mContext;
public BuildCustomAdapter(Context context, List<People> buildList) {
mContext = context;
this.peopleList = buildList;
this.peopleListCopy = new ArrayList<>();
peopleListCopy.addAll(buildList);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.build_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
People people = peopleList.get(position);
byte[] decodedString = Base64.decode(people.getPeopleImage(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.ivPeopleImage.setImageBitmap(decodedByte);
holder.tvPersonName.setText(people.getPeopleName());
holder.button.setSelected(people.getStatus() == 1);
holder.button.setOnClickListener(new onSelectListener(position));
}
#Override
public int getItemCount() {
return peopleList.size();
}
#Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView tvPersonName;
public Button button;
public CircularImageView ivPeopleImage;
public MyViewHolder(View itemView) {
super(itemView);
mContext = itemView.getContext();
ivPeopleImage = (CircularImageView) itemView.findViewById(R.id.ivPerson);
tvPersonName = (TextView) itemView.findViewById(R.id.tvPersonName);
button = (Button) itemView.findViewById(R.id.addbn);
tvPersonName.setOnClickListener(this);
ivPeopleImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mOnItemClickListener != null)
mOnItemClickListener.onItemClick(v, getPosition());
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
private class ItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<People> filterList = new ArrayList<>();
for (int i = 0; i < peopleListCopy.size(); i++) {
if ((peopleListCopy.get(i).getPeopleName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
People peopleName = peopleListCopy.get(i);
filterList.add(peopleName);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = peopleListCopy.size();
results.values = peopleListCopy;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
peopleList = (List<People>) results.values;
notifyDataSetChanged();
}
}
private class onSelectListener implements View.OnClickListener {
int mPosition;
public onSelectListener(int position) {
mPosition = position;
}
#Override
public void onClick(View view) {
People people = peopleList.get(mPosition);
view.setSelected(!view.isSelected());
people.setStatus(view.isSelected() ? 1 : 0);
notifyDataSetChanged();
}
}
}
Next Activity to get intent
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int peopleID = bundle.getInt("peopleID");
peopleList.clear();
BuildDataa();
People peopleDetailsObj = peopleList.get(peopleID);
}
Please replace
#Override
public void onClick(View v) {
if (mOnItemClickListener != null)
mOnItemClickListener.onItemClick(v, getPosition());
}
to
#Override
public void onClick(View v) {
if (mOnItemClickListener != null)
mOnItemClickListener.onItemClick(v, peopleList.get(getPosition()).getId());
}
in your adapter. And change signature of method onItemClick from
onItemClick(View v, int position)
to
onItemClick(View v, String id)
and use this to get People according to ID
People peopleDetailsObj=null;
for(People ple:peopleList)
{
if(ple.getId().equals(peopleID)){
peopleDetailsObj=ple;
break;
}}
Take the id from position.
String id=peopleList.get(position).getId();
detailPeople(id);
Pass that id in your method and you are done.
Use position to get People object from ArrayList. Change detailPeople() method with below code.
private void detailPeople(int position) {
Intent intent = new Intent(this, AddDetail.class);
intent.putExtra("peopleID", peopleList.get(position).getId());
startActivity(intent);
}
intent.putExtra("id", peopleList.get(position).getId());

Categories