Updates the same value to all items - android

My RecyclerView gets the same value as the other once I edited/updated a specifc item. Here is the example of what I am getting instead of update one specific item only. example
package com.example.crudrecycler;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ModelViewHolder> {
Context c;
ArrayList<StudentModel> studentArrayList= new ArrayList<>();
SQLiteDatabase sqlDB;
public CustomAdapter(Context c, int onedata, ArrayList<StudentModel> studentArrayList, SQLiteDatabase sqlDB) {
this.c = c;
this.studentArrayList = studentArrayList;
this.sqlDB = sqlDB;
}
#NonNull
#Override
public CustomAdapter.ModelViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(c);
View view = inflater.inflate(R.layout.onedata, null);
return new ModelViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CustomAdapter.ModelViewHolder holder, int position) {
final StudentModel model = studentArrayList.get(position);
holder.txtName.setText(model.getName());
holder.txtAddress.setText(model.getAddress());
holder.btn_Edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putInt("id", model.getId());
bundle.putString("name", model.getName());
bundle.putString("address", model.getAddress());
Intent intent = new Intent(c, MainActivity.class);
intent.putExtra("userdata", bundle);
c.startActivity(intent);
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return studentArrayList.size();
}
public class ModelViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtAddress;
Button btn_Edit;
public ModelViewHolder(#NonNull View itemView) {
super(itemView);
txtName = (TextView)itemView.findViewById(R.id.txtName);
txtAddress = (TextView)itemView.findViewById(R.id.txtAddress);
btn_Edit = (Button)itemView.findViewById(R.id.btn_Edit);
}
}
}
I think the problem here is that I am setting my intent to my MainActivity.class and once I click edit/update on my MainActivity it also updates other items inside the RecyclerView, but still I don't know how to fix where and which is the problem here since I am just new and exploring things inside android studio.

Related

working with edittext and recyclerview in Android

I want edittext content to show on a recyclerview but it is not working this is the adapter
package c.androidchatapp.recyclerview.Adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Model.Book;
import c.androidchatapp.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
public class BookAdapter extends
RecyclerView.Adapter<BookAdapter.BookViewHolder>{
private LayoutInflater layoutInflater;
private EditText edittext1;
private Button button1;
public static ArrayList<Book> bookList;
public BookAdapter(Context context, String[] mDataset) {
layoutInflater = LayoutInflater.from(context);
bookList = new ArrayList<Book>(mDataset.length);
}
#Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_list_row, parent, false);
return new BookViewHolder(itemView);
}
#Override
public void onBindViewHolder(BookViewHolder holder, int position) {
holder. editText.setText(bookList.get(position).getEditTextValue());
Log.d("print","yes");
}
#Override
public int getItemCount() {
return bookList.size();
}
public class BookViewHolder extends RecyclerView.ViewHolder {
protected EditText editText;
public BookViewHolder(View itemView) {
super(itemView);
editText = (EditText) itemView.findViewById(R.id.edittext1);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
bookList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
} }
and here is my main activity i use to get the code
package c.androidchatapp.recyclerview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Adapter.BookAdapter;
import c.androidchatapp.recyclerview.Model.Book;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button button1;
public ArrayList<Book> bookList;
private RecyclerView recyclerView;
private BookAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
button1 = (Button) findViewById(R.id.button1);
final TextView title = (TextView) findViewById(R.id.title);
bookList = populateList();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
// recyclerView.addItemDecoration(new ItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(mAdapter);
initBookData();
}
private ArrayList<Book> populateList() {
ArrayList<Book> list = new ArrayList<>();
for(int i = 0; i < 8; i++){
Book editModel = new Book();
editModel.setEditTextValue(String.valueOf(i));
list.add(editModel);
}
return list;
}
private void initBookData() {
}
}
I want the content to display onclick button but it is not working. I have tried different method but still the same thing. you can tell me where my error is and give me the correct to put there or edit the code I post and send me the correct one for easy correction.
You need to initialize your adapter before setting it to your recyclerview.
Something like
BookAdapter mAdapter = new BookAdapter(this, bookList);
before this line
recyclerView.setAdapter(mAdapter);
Also, you need to change your adapter implementation to receive an ArrayList<Book> instead of a String[] as a second argument
package c.androidchatapp.recyclerview.Adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Model.Book;
import c.androidchatapp.recyclerview.R;
import java.util.ArrayList;
import java.util.List;
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder>{
private LayoutInflater layoutInflater;
private EditText edittext1;
private Button button1;
public static ArrayList<Book> bookList;
public BookAdapter(Context context, ArrayList<Book> mDataset) {
layoutInflater = LayoutInflater.from(context);
bookList = new ArrayList<Book>(mDataset);
}
#Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.book_list_row, parent, false);
return new BookViewHolder(itemView);
}
#Override
public void onBindViewHolder(BookViewHolder holder, int position) {
holder. editText.setText(bookList.get(position).getEditTextValue());
Log.d("print","yes");
}
#Override
public int getItemCount() {
return bookList.size();
}
public class BookViewHolder extends RecyclerView.ViewHolder {
protected EditText editText;
public BookViewHolder(View itemView) {
super(itemView);
editText = (EditText) itemView.findViewById(R.id.edittext1);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
bookList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
} }
this is my new adapter code and this is the new mainactivity code too
package c.androidchatapp.recyclerview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Adapter.BookAdapter;
import c.androidchatapp.recyclerview.Model.Book;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button button1;
public ArrayList<Book> bookList;
private RecyclerView recyclerView;
private BookAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
button1 = (Button) findViewById(R.id.button1);
final TextView title = (TextView) findViewById(R.id.title);
bookList = populateList();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
// recyclerView.addItemDecoration(new ItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
BookAdapter mAdapter = new BookAdapter(this, bookList);
recyclerView.setAdapter(mAdapter);
initBookData();
}
private ArrayList<Book> populateList() {
ArrayList<Book> list = new ArrayList<>();
for(int i = 0; i < 8; i++){
Book editModel = new Book();
editModel.setEditTextValue(String.valueOf(i));
list.add(editModel);
}
return list;
}
private void initBookData() {
}
}
please i need quick respondse thanks

When I click on item in recyclerview, how can I retrieve data from firebase and show it in a list view in the next activity?

This is my recyclerview class where I have electrician, plumber, welder etc as my recyclerview items. I want whenever user click on any of these items the respective data that is retrieved from firebase should be shown in the next activity.
package com.example.hp.dc_project;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/** * Created by HP on 20-07-2018. */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext;
private List<category> mdata;
public RecyclerViewAdapter(Context context, List<category> mdata) {
this.mContext = context;
this.mdata = mdata;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.cardview_category,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(mdata.get(position).getName());
holder.iv.setImageResource(mdata.get(position).getThumnail());
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(mContext.getApplicationContext(),available_worker.class);
mContext.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return mdata.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView)itemView.findViewById(R.id.tv_category);
iv = (ImageView)itemView.findViewById(R.id.iv_category);
cardView = (CardView)itemView.findViewById(R.id.cardview);
}
}
}
This is the activity class where I want to display the listview containing the data that is retrieved from firebase after clicking on the recyclerview items:
package com.example.hp.dc_project;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class available_worker extends AppCompatActivity {
private ListView lv;
private FirebaseDatabase database;
private ArrayList<String>list;
private ArrayAdapter<String>adapter;
private Electrician elec;
// adapter = new ArrayAdapter<Electrician>(this,R.layout.list_of_work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_available_worker);
lv = (ListView)findViewById(R.id.listView_workeravailable);
elec = new Electrician();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?>parent, View view, int position, long id) {
Intent i = new Intent(available_worker.this,WorkerProfile.class);
startActivity(i);
}
});
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, R.layout.list_of_worker,R.id.employeeadd);
database = FirebaseDatabase.getInstance();
DatabaseReference rootref = database.getReference();
DatabaseReference electricianref = rootref.child("Employee").child("Electrician");
electricianref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String fName = ds.child("Fname").getValue(String.class);
String lName = ds.child("Lname").getValue(String.class);
String address = ds.child("Address").getValue(String.class);
list.add(fName);
list.add(lName);
list.add(address);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
In on click get your worker id and put in extra , in the next activity put value event listner and get the respective data but you have one thing is that you have to have the id or something which can uniquely identify the reference of firebase.
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(mContext.getApplicationContext(),available_worker.class);
i.putExtra("uid",workerid);
mContext.startActivity(i);
}
});
in the next activity
String s = getIntent().getStringExtra("uid");
firebaseref.child(s).addvalueEventListner(////) {//pupulate your list view}

Reload a RecyclerView after deleting some rows from a table

So when I delete a row in the SQLITE Database and the actual activity is closing, I see the CardView which is delete.
I really tried to google but nothing changed, so maybe you could help me out guys.
This is the SQLITE Delete function:
public void deleteEntry(String ID){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + DB_NAME + " WHERE ID = '" + ID + "'";
Log.d("QUERY",query);
db.execSQL(query);
db.close();
}
MyAdapter.java
package simplylabs.com.simplynote;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import static android.content.ContentValues.TAG;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
private List<ListItem> listItems;
private Context context;
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_design,parent,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position)
{
final ListItem list = listItems.get(position);
holder.insert_NOTIZENNAME.setText(list.getNOTIZENNAME());
holder.insert_NOTIZ.setText(list.getNOTIZ());
holder.insert_TIME.setText(list.getTIME());
if(list.getPRIORITAET().equals("Sehr wichtig")){
holder.prioColor.setBackgroundResource(R.color.rot);
}
if(list.getPRIORITAET().equals("Wichtig")){
holder.prioColor.setBackgroundResource(R.color.orange);
}
if(list.getPRIORITAET().equals("Nicht wichtig")){
holder.prioColor.setBackgroundResource(R.color.gruen);
}
}
#Override
public int getItemCount() {
return listItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView insert_NOTIZENNAME;
TextView insert_NOTIZ;
TextView insert_TIME;
View prioColor;
public MyViewHolder(View itemView) {
super(itemView);
insert_NOTIZENNAME = itemView.findViewById(R.id.showNOTIZENNAME);
insert_NOTIZ = itemView.findViewById(R.id.show_NOTIZ);
insert_TIME = itemView.findViewById(R.id.show_DATUM);
prioColor = itemView.findViewById(R.id.view_prioritaet);
}
}
}
This is when I click on the menu Item:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
DBHandler dbHandler = new DBHandler(this);
final Bundle extras = getIntent().getExtras();
switch(item.getItemId()){
case R.id.notiz_loeschen:
dbHandler.deleteEntry(extras.getString("ID"));
Toast.makeText(this, extras.getString("ID"), Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.notiz_aendern:
break;
}
return super.onOptionsItemSelected(item);
}
MainActivity.java
recyclerView = rootView.findViewById(R.id.recyclerview_alle_notizen);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new MyAdapter(listItems,getActivity());
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
if (data.moveToPosition(position))
{
Intent i = new Intent(getActivity(),NotizAnzeigen.class);
i.putExtra("NotizName",data.getString(1));
i.putExtra("Notiz",data.getString(2));
startActivity(i);
}
}
})
);
Could you please help me out please?
I've changed this, it is still only remove the cardview when i move to the last tab ...
package simplylabs.com.simplynote;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class AlleNotizen extends Fragment {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private List<ListItem> listItems;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alle_notizen, container, false);
recyclerView = rootView.findViewById(R.id.recyclerview_alle_notizen);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
return rootView;
}
#Override
public void onResume() {
listItems = new ArrayList<>();
ListItem listItem = new ListItem();
final DBHandler db = new DBHandler(getActivity());
final Cursor data = db.getData();
while(data.moveToNext()){
ListItem li = new ListItem(data.getString(0), data.getString(1),data.getString(2),data.getString(4),data.getString(3));
listItems.add(li);
}
adapter = new MyAdapter(listItems,getActivity());
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
if (data.moveToPosition(position))
{
Intent i = new Intent(getActivity(),NotizAnzeigen.class);
i.putExtra("NotizName",data.getString(1));
i.putExtra("Notiz",data.getString(2));
startActivity(i);
}
}
})
);
super.onResume();
}
}
As you are deleting data from your local storage you need to notify the adapter that data set has changed.To do that you could try this.
After deleting data from SQLite you need to read data again and store
it in a list/arraylist variable.
create a function inside your adapter like this,
public void loadWithNewData(List<items> newCollectedData) {
this.listItems.clear();
this.listItems = newCollectedData;
this.notifyDataSetChanged();
}
And then call that function with adapter variable like this.
adapter.loadWithNewData(newDataCollectedFromDatabase);
hope it will help.
You are deleting the row from the database, but you are not updating your listItems array. You should update listItems and then call adapter.notifyDataSetChanged();

RecyclerView giving errors

Hi i am new to android project so i am trying to use recycler view. but it is not working. i keep getting errors # this line " RecyclerAdapter adapter = new RecyclerAdapter(this, getData());" it says RecyclerAdapter (android.content.context, List) in RecyclerAdapter cannot be applied to my package class please help
statement.java
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import nnnnn.R;
import nnnnnn.RecyclerAdapter;
import nnnnnnnnnnnnn.Landscape;
public class Statements extends Fragment {
Toolbar toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.statements,container,false);
toolbar = (Toolbar) v.findViewById(R.id.toolbar);
toolbar.setTitle("Statements");
toolbar.inflateMenu(R.menu.menu_main);
setUpRecyclerView();
return v;}
private void setUpRecyclerView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RecyclerAdapter adapter = new RecyclerAdapter(this.getActivity(),Landscape.getData());
recyclerView.setAdapter(adapter);
final LinearLayoutManager mLinearLayoutManagerVertical = new LinearLayoutManager(this.getActivity()); // (Context context, int spanCount)
mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(mLinearLayoutManagerVertical);
recyclerView.setItemAnimator(new DefaultItemAnimator()); // Even if we dont use it then also our items shows default animation. #Check Docs
}
}
RecyclerAdapter code is below
RecyclerAdapter
import android.content.Context;
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.ImageView;
import android.widget.TextView;
import java.util.List;
import xx.xx.xx.xx.xx.R;
import xx.xx.xx.xx.model.Landscape;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private static final String TAG = RecyclerAdapter.class.getSimpleName();
private List<Landscape> mDataList;
private LayoutInflater inflater;
public RecyclerAdapter(Context context, List<Landscape> data) {
this.mDataList = data;
this.inflater = LayoutInflater.from(context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i(TAG, "onCreateViewHolder");
View view = inflater.inflate(R.layout.statementlist, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.i(TAG, "onBindViewHolder" + position);
Landscape current = mDataList.get(position);
holder.setData(current, position);
holder.setListeners();
}
#Override
public int getItemCount() {
return mDataList.size();
}
public void removeItem(int position) {
mDataList.remove(position);
notifyItemRemoved(position);
// notifyItemRangeChanged(position, mDataList.size());
// notifyDataSetChanged();
}
public void addItem(int position, Landscape landscape) {
mDataList.add(position, landscape);
notifyItemInserted(position);
// notifyItemRangeChanged(position, mDataList.size());
// notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
ImageView imgThumb, imgDelete, imgAdd;
int position;
Landscape current;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.tvTitle);
imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
imgDelete = (ImageView) itemView.findViewById(R.id.img_row_delete);
imgAdd = (ImageView) itemView.findViewById(R.id.img_row_add);
}
public void setData(Landscape current, int position) {
this.title.setText(current.getTitle());
imgThumb.setImageResource(current.getImageID());
this.position = position;
this.current = current;
}
public void setListeners() {
imgDelete.setOnClickListener(MyViewHolder.this);
imgAdd.setOnClickListener(MyViewHolder.this);
imgThumb.setOnClickListener(MyViewHolder.this);
}
#Override
public void onClick(View v) {
// Log.i("onClick before operation", position + " " + mDataList.size());
switch (v.getId()) {
case R.id.img_row_delete:
removeItem(position);
break;
case R.id.img_row_add:
addItem(position, current);
break;
}
// Log.i("onClick after operation", mDataList.size() + " \n\n" + mDataList.toString());
}
}
}
You're passing in "this" for the first argument to RecyclerAdapter. "this" being a Fragment class. The Fragment class does not extend from Context, so it cannot be used as such.
Try "this.getActivity()". This may result in null, so ideally put this within the Fragment subclass's "onActivityCreated" method (instead of the "onCreateView" method)
You need to override OnActivitycreated in order to call getActivity. Call setUpRecyclerView in OnActivitycreated and add getActivity. before findViewById and it will work

Dividers in RecyclerView

I need dividers in RecyclerView like an Image,I cant understand how I can do this.Dividers must look like textview or image,I need to make different ViewHolder or what?Im google it and don't find any example look like I need.
Its must look like this
http://www.picshare.ru/view/7210663/
My Adapter:
package com.example.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Objects;
import butterknife.Bind;
/**
* Created by Рост on 20.03.2016.
*/
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
private List<User> UserList;
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View viewUser = inflater.inflate(R.layout.row_item_layout,parent,false);
viewHolder = new MyViewHolder(viewUser);
return (MyViewHolder) viewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.UserTitle.setText(UserList.get(position).getTitle());
holder.UserDescription.setText(UserList.get(position).getDescription());
holder.UserIcon.setImageResource(R.drawable.emilypic);
}
public UserAdapter(List<User> UserList){
this.UserList = UserList;
}
#Override
public int getItemCount() {
return UserList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView UserIcon;
public TextView UserTitle;
public TextView UserDescription;
public Button UserFollowButton;
public MyViewHolder(View itemView) {
super(itemView);
UserIcon = (ImageView) itemView.findViewById(R.id.item_icon);
UserTitle = (TextView) itemView.findViewById(R.id.item_title);
UserDescription = (TextView) itemView.findViewById(R.id.item_description);
UserFollowButton = (Button) itemView.findViewById(R.id.item_follow_button);
}
}
}
Fragment with data:
package com.example.myapplication;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
public class Fragment_Username_Search extends Fragment {
private List<User> userList = new ArrayList<>();
private RecyclerView recyclerView;
private UserAdapter userAdapter;
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void prepareUserData() {
User user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Sammy Lee","sambam",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Lilly Martinson","lilmartini",R.id.item_follow_button);
userList.add(user);
userAdapter.notifyDataSetChanged();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment__username__search, container, false);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
userAdapter = new UserAdapter(userList);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(userAdapter);
prepareUserData();
return view;
}
}

Categories

Resources