working with edittext and recyclerview in Android - 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

Related

Updates the same value to all items

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.

How to get data from one RecyclerView and show it in another Activity's RecyclerView

I have an Android nested recycler view in which I am able to show the data now I want to show that data in another activity recycler view. How can I do that?
This is my nested list view:-
This is where I want to show data in list:-
Here's my code: my Main Activity:-
package com.saikari.vid.activities;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
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 com.saikari.vid.R;
import com.saikari.vid.adapters.VerticalRVAdapter;
import com.saikari.vid.models.HorizontalModel;
import com.saikari.vid.models.VerticalModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private DatabaseReference dbCategories;
VerticalRVAdapter adapter;
RecyclerView verticalRecyclerView;
ArrayList<VerticalModel> mListData = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticalRecyclerView = findViewById(R.id.recycler_view);
verticalRecyclerView.setHasFixedSize(true);
setData();
}
private void setData() {
for (int i = 0; i<10; i++){
VerticalModel verticalModel = new VerticalModel();
verticalModel.setCat_title("Title: "+i);
ArrayList<HorizontalModel> arrayListHorizontal = new ArrayList<>();
for (int j = 0; j<10; j++){
HorizontalModel horizontalModel = new HorizontalModel();
horizontalModel.setVid_url("https://picsum.photos/seed/picsum/200/300");
horizontalModel.setVid_title("Name: "+j);
arrayListHorizontal.add(horizontalModel);
}
verticalModel.setVidArrayList(arrayListHorizontal);
mListData.add(verticalModel);
}
adapter.notifyDataSetChanged();
}
}
Vertical RecyclerView adapter class:-
public class VerticalRVAdapter extends RecyclerView.Adapter<VerticalRVAdapter.VerticalRVViewHolder> {
Context context;
ArrayList<VerticalModel> arrayList;
public VerticalRVAdapter(Context context, ArrayList<VerticalModel> arrayList){
this.arrayList = arrayList;
this.context = context;
}
#NonNull
#Override
public VerticalRVViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vertical,parent,false);
return new VerticalRVViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull VerticalRVViewHolder holder, int position) {
final VerticalModel verticalModel = arrayList.get(position);
String title = verticalModel.getCat_title();
final ArrayList<HorizontalModel> singleItem = verticalModel.getVidArrayList();
holder.titleText.setText(title);
HorizontalRVAdapter horizontalRecyclerViewAdapter= new HorizontalRVAdapter(context,singleItem);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
holder.recyclerView.setAdapter(horizontalRecyclerViewAdapter);
holder.buttonMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, verticalModel.getCat_title(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, VideoListActivity.class);
i.putExtra("catTitle", verticalModel.getCat_title());
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class VerticalRVViewHolder extends RecyclerView.ViewHolder{
RecyclerView recyclerView;
TextView titleText;
TextView buttonMore;
public VerticalRVViewHolder(#NonNull View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.recycler_view1);
titleText = itemView.findViewById(R.id.titleText);
buttonMore = itemView.findViewById(R.id.btn_more);
}
}
}
Horizontal Recycler View Adapter class:-
public class HorizontalRVAdapter extends RecyclerView.Adapter<HorizontalRVAdapter.HorizontalRVViewHolder> {
Context context;
ArrayList<HorizontalModel> arrayList;
public HorizontalRVAdapter(Context context, ArrayList<HorizontalModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public HorizontalRVViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal,parent,false);
return new HorizontalRVViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final HorizontalRVViewHolder holder, int position) {
final HorizontalModel horizontalModel = arrayList.get(position);
holder.textViewTitle.setText(horizontalModel.getVid_title());
com.bumptech.glide.Glide.with(context).load(horizontalModel.getVid_url()).into(holder.imageViewThumb);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, horizontalModel.getVid_url(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, YoutubePlayActivity.class);
i.putExtra("videoID", horizontalModel.getVid_url());
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class HorizontalRVViewHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
ImageView imageViewThumb;
public HorizontalRVViewHolder(#NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textTitleHorizontal);
imageViewThumb = itemView.findViewById(R.id.ivThumb);
}
}
}
Data in list activity class (this is the activity where I need to show data):-
public class VideoListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getIntent().getStringExtra("catTitle"));
}
}
Implement Parcelable on HorizontalModel class. If you need help with that check here.
In VerticalRVAdapter inside onBindViewHolder method
holder.buttonMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, verticalModel.getCat_title(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, VideoListActivity.class);
i.putExtra("catTitle", verticalModel.getCat_title());
// Put horizontal models in extra bundle
i.putParcelableArrayListExtra("horizontal_models", singleItem);
context.startActivity(i);
}
});
You can now get the bundled HorizontalModel list in VideoListActivity inside onCreate as
ArrayList<HorizontalModel> horizontalModels = getIntent().getParcelableArrayListExtra("horizontal_models");
You can use this list to populate the vertical list that you want to show.

How to take reference of itemClick on recyclerview

Suppose I have a scrolling RecyclerView (where images scrolling Horizontally) after clicking any of images it will appear below the recyclerView.
Main Activity:
package com.aariyan.testm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;`enter code here`
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;`enter code here`
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
//implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener
private SliderLayout mDemoSlider;
private ImageView imageView;
private String selectDrinkCategory;
private String drinks = "drinks";
//String i = "0";
int j;
RecyclerViewAdapter recyclerViewAdapter;
private int[] images = {R.drawable.a,
R.drawable.bb,
R.drawable.c,
R.drawable.d,
R.drawable.e,
R.drawable.d_2_1,
R.drawable.d_2_2,
R.drawable.d_2_3,
R.drawable.d_2_4
};
private ArrayList<Integer> inputImages = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get Intent
Intent intent = getIntent();
if (getIntent() != null) {
selectDrinkCategory = intent.getStringExtra("drinks");
}
initUI();
initBitmapImages();
initRecyclerView();
Toast.makeText(this, "J: "+j, Toast.LENGTH_SHORT).show();
}
private void initRecyclerView() {
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(manager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(inputImages, this);
Toast.makeText(this, " Main Value j : " + adapter.index, Toast.LENGTH_SHORT).show();
imageView.setImageResource(images[j]);
recyclerView.setAdapter(adapter);
}
private void initBitmapImages() {
for (int i = 0; i < images.length; i++) {
// inputImages.add(String.valueOf(images[i]));
inputImages.add(images[i]);
}
}
private void initUI() {
//mDemoSlider = (SliderLayout) findViewById(R.id.slider);
imageView = findViewById(R.id.viewSingleImages);
//imageView.setImageResource((images[0]));
}
#Override
protected void onStop() {
// To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
super.onStop();
}
}
RecyclerView:
I have a scrolling RecyclerView (where images scrolling Horizontally) after clicking any of images it will appear below the
package com.aariyan.testm;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<Integer> totalImages = new ArrayList<>();
private Context context;
//MainActivity mainActivity;
int index = 0;
public RecyclerViewAdapter(ArrayList<Integer> totalImages, Context context) {
this.totalImages = totalImages;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.images, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, final int i) {
//index = 0;
Glide.with(context).asBitmap().load(totalImages.get(i)).into(viewHolder.singleImages);
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isClicked) {
Toast.makeText(context, "TotalImages : "+totalImages.get(i), Toast.LENGTH_SHORT).show();
index = totalImages.get(i);
}
});
// viewHolder.singleImages.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// Toast.makeText(context, "" + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
// //viewHolder.imageView.setImageResource(totalImages.get(viewHolder.getAdapterPosition()));
// //index = viewHolder.getAdapterPosition();
// index = i;
// Toast.makeText(context, "Index : " + index, Toast.LENGTH_SHORT).show();
// }
// });
}
#Override
public int getItemCount() {
return totalImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView singleImages;
private ItemClickListener itemClickListener;
public ViewHolder(#NonNull View itemView) {
super(itemView);
singleImages = itemView.findViewById(R.id.imagesViewOnScroll);
itemView.setOnClickListener(this);
}
public void setItemClickListener (ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
}
}
activity_main.xml : just have a recyclerView and image View
and images.xml : only a image view to set on RecyclerView
for get clicked item of recyckerview you have to use interface.
here is code sample:
first of all make one interface like below :
public interface ImageClickListener {
void onImageClick(String path);
}
now in your activity, make adapter's object like this
RecyclerViewAdapter adapter = new RecyclerViewAdapter(inputImages, this, new ImageClickListener() {
#Override
public void onImageClick(String path) {
Glide.with(context).load(path).into(imageView);
}
});
now create your adapter and make a constructor like this
public RecyclerViewAdapter(ArrayList<Integer> totalImages, Context context, ImageClickListener imageClickListener) {
this.totalImages = totalImages;
this.context = context;
this.imageClickListener = imageClickListener;
}
now you have an instance of interface in which you have one method, named onImageClick() in which you will pass the path of clicked image.
now on click of image do code like this:
viewHolder.singleImages.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageClickListener.onImageClick(totalImages.get(viewHolder.getAdapterPosition()));
}
});
now your method of interface will call & it will execute activity's image view to set the given path.
Enjoy codding :)

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}

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