I'm having trouble getting my image to output. I want to output the data from a API in a recycler view using retrofit and Gson. I've read through the documentation and some stackoverflow pages. I am not using a third party library (such as picasso) so I am using AsyncTask. The photo class is the typical POJO class, and I put the internet permission. Here is the code:
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
List<Photo> photos;
ClickListener ClickListener;
public RecyclerViewAdapter(List<Photo> photos, ClickListener ClickListener) {
this.photos = photos;
this.ClickListener = ClickListener;
}
interface ClickListener {
void getItem(int position);
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_view, parent, false);
return new RecyclerViewHolder(view, ClickListener);
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, #SuppressLint("RecyclerView") int position) {
holder.title.setText(photos.get(position).title);
holder.albumid.setText(photos.get(position).albumId);
holder.regid.setText(photos.get(position).id);
new Thread(new Runnable(){
#Override
public void run() {
holder.circleImageView.setImageBitmap(getBitmapFromURL(photos.get(position).thumbnailUrl));
holder.imageView.setImageBitmap(getBitmapFromURL(photos.get(position).url));
}
}).start();
}
#Override
public int getItemCount() {
return photos.size();
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, albumid, regid;
ImageView imageView;
ClickListener ClickListener;
CircleImageView circleImageView;
public RecyclerViewHolder(View itemView, ClickListener ClickListener) {
super(itemView);
title = itemView.findViewById(R.id.title);
albumid = itemView.findViewById(R.id.albumid);
regid = itemView.findViewById(R.id.id);
imageView = itemView.findViewById(R.id.url);
circleImageView = itemView.findViewById(R.id.thumb);
this.ClickListener = ClickListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
ClickListener.getItem(getAdapterPosition());
}
}
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
}
Main Activity
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.ClickListener {
ApiInterface apiInterface;
RecyclerView mRecyclerView;
List<Photo> photos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
apiInterface = ApiClient.getClient().create(ApiInterface.class);
apiInterface.getPhotos().enqueue(new Callback<List<Photo>>() {
#Override
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
photos = response.body();
mRecyclerView.setAdapter(new RecyclerViewAdapter(photos, MainActivity.this));
}
#Override
public void onFailure(Call<List<Photo>> call, Throwable t) {
}
});
}
#Override
public void getItem(int position) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this)
.setTitle(photos.get(position).title)
.setMessage(photos.get(position).url)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
}
APIClient
public class ApiClient {
private static final String BASE_URL = "https://jsonplaceholder.typicode.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient(){
if (retrofit == null){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}
}
APIInterface
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface ApiInterface {
#GET("/photos")
Call<List<Photo>> getPhotos();
#GET("/photos/{id}")
Call<Photo> getPhoto(#Path("id") int id);
#GET("/photos")
Call<Photo> getPhotoUsingQuery(#Query("id") int id);
}
Child View
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/thumb"
android:layout_width="138dp"
android:layout_height="match_parent"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp" />
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/albumid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="#+id/url"
android:layout_width="50dp"
android:layout_height="120dp"
android:layout_weight="1" />
</LinearLayout>
Related
I have made a meme app, which fetches image url from an API and i m parsing them and showing in recyler view using Glide , in my item layout i have two buttons one for sharing and one for download Image , i want when user clicks the download button the images get download to the user phone.
Help me how i can implement this.
MyAdapter class
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
ArrayList<Model> modelArrayList;
public Adapter(Context context, ArrayList<Model> modelArrayList) {
this.context = context;
this.modelArrayList = modelArrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String url = modelArrayList.get(position).getUrl();
holder.setImage(url);
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sharing = new Intent (Intent.ACTION_SEND);
sharing.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharing.setType("text/plain");
String subject = "Hey Man just look at this coll meme click the link " +url;
sharing.putExtra(Intent.EXTRA_TEXT,subject);
context.startActivity(Intent.createChooser(sharing,"Shring using"));
}
});
holder.buttonDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
#Override
public int getItemCount() {
return modelArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
Button button,buttonDownload;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
button = itemView.findViewById(R.id.button);
buttonDownload = itemView.findViewById(R.id.btn_download);
}
void setImage(String link){
Glide.with(context).load(link).into(imageView);
}
}
}
My model class
public class Model {
String url;
public Model(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
My item_layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background"
tools:ignore="VectorDrawableCompat" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="104dp"
android:layout_marginLeft="104dp"
android:layout_marginBottom="28dp"
android:text="Share"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/btn_download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="71dp"
android:layout_marginRight="71dp"
android:layout_marginBottom="28dp"
android:text="Download"
android:textColor="#0B0B0B"
app:backgroundTint="#12E71A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
My mainactivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
ArrayList<Model> arrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recylerview_id);
arrayList = new ArrayList<>();
String url = "https://meme-api.herokuapp.com/gimme/30";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("memes");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String url = jsonObject.getString("url");
Model m = new Model(url);
arrayList.add(m);
}
adapter = new Adapter(MainActivity.this,arrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
}
Glide allows to load a Bitmap.
you can save Bitmap to file, and then just pass file path to another screen.
Here you can find detailed tutorial, how to store image to file with Glide library:
https://medium.com/#akshayranagujjar/how-to-save-image-to-storage-using-glide-in-android-fa26c842f212
And don't forget about storage permissions.
I want to ask you, how to change programmatically the background color of LinearLayout in CardView when the text in TextView equals specify word or is empty. For example field1 getting string "red", the background of LinearLayout gets red. I'm fetching the data from JSON php file.
MainActivity.class
public class MainActivity extends AppCompatActivity {
private String jsonURL = "http://example.com/test.php";
private final int jsoncode = 1;
private RecyclerView recyclerView;
ArrayList<Model> ModelArrayList;
private Adapter adapter;
private static ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler);
fetchJSON();
}
#SuppressLint("StaticFieldLeak")
private void fetchJSON(){
new AsyncTask<Void, Void, String>(){
protected String doInBackground(Void[] params) {
String response="";
HashMap<String, String> map=new HashMap<>();
try {
HttpRequest req = new HttpRequest(jsonURL);
response = req.prepare(HttpRequest.Method.POST).withData(map).sendAndReadString();
} catch (Exception e) {
response=e.getMessage();
}
return response;
}
protected void onPostExecute(String result) {
//do something with response
onTaskCompleted(result,jsoncode);
}
}.execute();
}
public void onTaskCompleted(String response, int serviceCode) {
Log.d("responsejson", response.toString());
switch (serviceCode) {
case jsoncode:
if (isSuccess(response)) {
removeSimpleProgressDialog(); //will remove progress dialog
ModelArrayList = getInfo(response);
adapter = new Adapter(this,ModelArrayList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}else {
Toast.makeText(MainActivity.this, getErrorCode(response), Toast.LENGTH_SHORT).show();
}
}
}
public ArrayList<Model> getInfo(String response) {
ArrayList<Model> ModelArrayList = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("status").equals("true")) {
JSONArray dataArray = jsonObject.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
Model fieldsModel = new Model();
JSONObject dataobj = dataArray.getJSONObject(i);
fieldsModel.setField1(dataobj.getString("field1"));
fieldsModel.setField2(dataobj.getString("field2"));
ModelArrayList.add(fieldsModel);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return ModelArrayList;
}
public boolean isSuccess(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("status").equals("true")) {
return true;
} else {
return false;
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
public String getErrorCode(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
return jsonObject.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
return "No data";
}
}
Adapter.class
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private LayoutInflater inflater;
private ArrayList<Model> ModelArrayList;
public Adapter(Context ctx, ArrayList<Model> rogerModelArrayList){
inflater = LayoutInflater.from(ctx);
this.ModelArrayList = rogerModelArrayList;
}
#Override
public Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(Adapter.MyViewHolder holder, int position) {
holder.field1.setText(ModelArrayList.get(position).getField1());
holder.field2.setText(ModelArrayList.get(position).getField2());
}
#Override
public int getItemCount() {
return ModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView field1,field2;
public MyViewHolder(View itemView) {
super(itemView);
field1 = (TextView) itemView.findViewById(R.id.field1);
field2 = (TextView) itemView.findViewById(R.id.field2);
}
}
}
Model.class
public class Model {
private String field1, field2;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#cd15e6"
tools:context=".MainActivity">
** <android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="15dp"/> **
</LinearLayout>
rv_item.xml
Here is the LinearLayout which i want to change the color LL_background
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:id="#+id/LL_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/field1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="ddd"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="#+id/field2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="ddd"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Please check my project and share your ideas. Thanks a lot 😊
First of all add layout reference to ViewHolder
class MyViewHolder extends RecyclerView.ViewHolder{
TextView field1,field2;
LinearLayout layout;
public MyViewHolder(View itemView) {
super(itemView);
field1 = (TextView) itemView.findViewById(R.id.field1);
field2 = (TextView) itemView.findViewById(R.id.field2);
layout = itemView.findViewById(R.id.LL_background);
}
}
Then check Field1 and set color
#Override
public void onBindViewHolder(Adapter.MyViewHolder holder, int position) {
holder.field1.setText(ModelArrayList.get(position).getField1());
holder.field2.setText(ModelArrayList.get(position).getField2());
if(ModelArrayList.get(position).getField1().equalIgnoreCase("red")
holder.layout.setBackgroundColor(Color.RED)
}
red is not recognizable in android. Either you have to handle it manually like above or you can send the corresponding #code of color from server and handle it like below:
holder.layout.setBackgroundColor(Color.parseColor("#ff0000"));
You can change layout instead color. Create different layouts that has different background color or different design. Then do this.
#Override
public Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
if(ModelArrayList.get(position).getField1().equalIgnoreCase("red")
view = inflater.inflate(R.layout.anotherLayout, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
I have a cloud firestore database with a collection named Bus and three documents inside it as it is shown in the image
database picture
I have followed what is written in this link
How to display data from Firestore in a RecyclerView with Android?
three recycler views are being created and displayed but with empty data
also when I add a fourth document to Bus four recycler views are being created and displayed but with empty data
can you tell me how to fix my code?
model:
public class Bus {
public Bus(String driverName, int busNo, String contactNo) {
this.driverName = driverName;
this.busNo = busNo;
this.contactNo = contactNo;
}
public Bus(){ }
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public int getBusNo() {
return busNo;
}
public void setBusNo(int busNo) {
this.busNo = busNo;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
String driverName, contactNo;
int busNo ;
}
activity code :
public class ViewBusDetailes extends AppCompatActivity {
LinearLayoutManager linearLayoutManager;
FirestoreRecyclerAdapter<Bus, BusViewHolder> adapter;
class BusViewHolder extends RecyclerView.ViewHolder {
private View view;
BusViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setDetiles(String contactNoText , String driverNameText, int busNoText) {
TextView driverName;
TextView busNo;
TextView contactNo;
driverName = itemView.findViewById(R.id.driverNameDriverListView);
contactNo = itemView.findViewById(R.id.driverContactDriverListView);
busNo = itemView.findViewById(R.id.busNumber);
driverName.setText(driverNameText);
busNo.setText(String.valueOf(busNoText));
contactNo.setText(contactNoText);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_bus_details__layout);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Bus");
FirestoreRecyclerOptions<Bus> options = new FirestoreRecyclerOptions.Builder<Bus>()
.setQuery(query, Bus.class)
.build();
adapter = new FirestoreRecyclerAdapter<Bus, BusViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull BusViewHolder holder, int position, #NonNull Bus model) {
holder.setDetiles(model.getContactNo() , model.getDriverName() ,model.getBusNo());
}
#NonNull
#Override
public BusViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bus_list_view, parent, false);
return new BusViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
}
bus_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="360dp"
android:layout_height="72dp"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="#+id/busNumber"
android:layout_width="40dp"
android:layout_height="40dp"
android:textSize="20sp"
android:gravity="center"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:textColor="#ffffff"
android:background="#drawable/circle"
/>
<LinearLayout
android:layout_width="287dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/driverNameDriverListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textSize="17sp"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:textColor="#de000000"
android:lineSpacingExtra="8sp"
android:text="Anwar"
/>
<TextView
android:id="#+id/driverContactDriverListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginLeft="17dp"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:textColor="#de000000"
android:lineSpacingExtra="6sp"
android:text="Dude can you solve this problem plea..."
/>
</LinearLayout>
</LinearLayout>
activity_view_bus_detiels.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ViewBusDetailes"
android:id="#+id/container">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Do it like this
public class Bus {
private String driverName;
private int busNo;
private String contactNo;
public String getDriverName() {
return driverName;
}
public int getBusNo() {
return busNo;
}
public String getContactNo() {
return contactNo;
}
}
activity code :
public class ViewBusDetailes extends AppCompatActivity {
LinearLayoutManager linearLayoutManager;
FirestoreRecyclerAdapter adapter;
class BusViewHolder extends RecyclerView.ViewHolder {
TextView driverName;
TextView busNo;
TextView contactNo;
BusViewHolder(View itemView) {
super(itemView);
driverName = itemView.findViewById(R.id.driverNameDriverListView);
contactNo = itemView.findViewById(R.id.driverContactDriverListView);
busNo = itemView.findViewById(R.id.busNumber);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_bus_details__layout);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Bus");
FirestoreRecyclerOptions<Bus> options = new FirestoreRecyclerOptions.Builder<Bus>()
.setQuery(query, Bus.class)
.build();
adapter = new FirestoreRecyclerAdapter<Bus, BusViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull BusViewHolder holder, int position, #NonNull Bus model) {
holder.driverName.setText(model.getDriverName());
holder.busNo.setText(String.valueOf(model.getBusNo()));
holder.contactNo.setText(model.getContactNo());
}
#NonNull
#Override
public BusViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ViewBusDetailes.this).inflate(R.layout.bus_list_view, parent, false);
return new BusViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
}
Also, add this dependency if you haven't.
implementation 'com.firebaseui:firebase-ui-firestore:4.3.2'
Also, edit your Firebase Firestore rules from Firestore settings in the firebase console.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Check if it works.
I am working on an android application that is showing data in Recycle List View Holder. When I Click on List Item in Recycler View Holder the application crashes.
public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {
private List<User> listUsers;
Context mContext;
ItemClickListenerLongPressed itemClickListenerLongPressed;
public UserRecyclerAdapterSavedUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_second, parent, false);
return new UserViewHolder(itemView);
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.textViewID.setText(listUsers.get(position).getUserid());
holder.textViewName.setText(listUsers.get(position).getName());
holder.textViewPassword.setText(listUsers.get(position).getPassword());
holder.textViewRole.setText(listUsers.get(position).getRole());
}
public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed){
this.itemClickListenerLongPressed=itemClickListenerLongPressed;
}
#Override
public int getItemCount() {
Log.v(UserRecyclerAdapterSavedUsers.class.getSimpleName(),""+listUsers.size());
return listUsers.size();
}
/**
* ViewHolder class
*/
public class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//public AppCompatTextView ID;
public AppCompatTextView textViewID;
public AppCompatTextView textViewName;
public AppCompatTextView textViewPassword;
public AppCompatTextView textViewRole;
public UserViewHolder(View view) {
super(view);
textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);
textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);
textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);
textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);
}
#Override
public void onClick(View v) {
if (itemClickListenerLongPressed != null) itemClickListenerLongPressed.onClick(v, getAdapterPosition());
Toast.makeText(mContext, "USMAN", Toast.LENGTH_SHORT).show();
}
}
}
Here is the users List activity
public class UsersListActivity extends AppCompatActivity implements ItemClickListenerLongPressed{
AppCompatActivity activity = UsersListActivity.this;
AppCompatTextView textViewName;
RecyclerView mRecyclerView;
AppCompatButton textViewButtonNewUser;
UserRecyclerAdapterSavedUsers userRecyclerAdapterSavedUsers;
List<User> listUsers;
DatabaseHelper databaseHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_record_updated_list);
mRecyclerView= (RecyclerView) findViewById(R.id.recyclerViewUsers);
mRecyclerView.setAdapter(userRecyclerAdapterSavedUsers);
userRecyclerAdapterSavedUsers.setItemClickListenerLongPressed(this);
initViews();
initObjects();
}
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(UsersListActivity.this,AdminMain.class));
finish();
}
#Override
protected void onRestart() {
super.onRestart();
}
/**
* This method is to initialize views
*/
private void initViews() {
textViewName = (AppCompatTextView) findViewById(R.id.textViewName);
textViewButtonNewUser = (AppCompatButton) findViewById(R.id.btnaddnew);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewUsers);
textViewButtonNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(UsersListActivity.this,UserRecordSaveActivity.class));
}
});
}
/**
* This method is to initialize objects to be used
*/
private void initObjects() {
listUsers = new ArrayList<>();
userRecyclerAdapterSavedUsers = new UserRecyclerAdapterSavedUsers(listUsers);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(userRecyclerAdapterSavedUsers);
databaseHelper = new DatabaseHelper(activity);
String emailFromIntent = getIntent().getStringExtra("USERS");
textViewName.setText(emailFromIntent);
getDataFromSQLite();
}
/**
* This method is to fetch all user records from SQLite
*/
private void getDataFromSQLite() {
// AsyncTask is used that SQLite operation not blocks the UI Thread.
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
listUsers.clear();
listUsers.addAll(databaseHelper.getAllUser());
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
userRecyclerAdapterSavedUsers.notifyDataSetChanged();
}
}.execute();
}
#Override
public void onClick(View view, int position) {
}
}
When I clicked on the List Item it crashed and the error was caused by Toast. As I remove the toast the Error goes because of using a try catch item not clicked.
Here is the image of Error.
After Removing try catch It again shows error but this time the error is shown on AlertDialog. Builder. Here is the image of error without try catch.
Image after removing try and catch
ERROR BEFORE REMOVING TOAST OVER ON CLICK
Image after adding toast logcat Error
Image After Adding Toast
The error is now on users list activity
Image after eidting of code
The actual data in list when removing the click listener
Actual data in list by removing the click listener
Here is my recycler layout file
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/Indigo"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="User ID"
android:textColor="#color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textViewID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User ID"
android:textColor="#android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Name"
android:textColor="#color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textColor="#android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/hint_password"
android:textColor="#color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textViewPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hint_password"
android:textColor="#android:color/darker_gray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Role"
android:textColor="#color/colorTextHint" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textViewRole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Role"
android:textColor="#android:color/darker_gray" />
</LinearLayout>
</LinearLayout>
Here is the Image of Logcat
Logcat Image at Updating Data
Here is the IMEIRecord Save activity Adapter like user record..
public class IMEIRecyclerAdapter extends RecyclerView.Adapter<IMEIRecyclerAdapter.ImeiViewHolder> {
private List<User> ListImei;
Context mContext;
ItemClickListenerLongPressed itemClickListenerLongPressed;
View itemView;
public IMEIRecyclerAdapter(List<User> ListImei) {
this.ListImei = ListImei;
}
#Override
public ImeiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_imei, parent, false);
return new ImeiViewHolder(itemView);
}
#Override
public void onBindViewHolder(ImeiViewHolder holder, int position) {
final User user= ListImei.get(position);
holder.textViewImeiId.setText(ListImei.get(position).getImeiid());
holder.textViewImeiNo.setText(ListImei.get(position).getImei());
}
public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed) {
this.itemClickListenerLongPressed = itemClickListenerLongPressed;
}
#Override
public int getItemCount() {
Log.v(UsersRecyclerAdapter.class.getSimpleName(),""+ListImei.size());
return ListImei.size();
}
public void displayingAlertDialogimei() {
final User user= new User();
//displaying alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
builder.setTitle("Choose Option");
builder.setMessage("Update or Delete?");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
goToUpdateActivity(user.getUserid());
dialog.cancel();
}
});
builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//go to update activity
dialog.cancel();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
}
private void goToUpdateActivity(String userid) {
Intent goToUpdate = new Intent(mContext, RoughUser.class);
goToUpdate.putExtra("USER_ID", userid);
mContext.startActivity(goToUpdate);
}
public class ImeiViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public AppCompatTextView textViewImeiId;
public AppCompatTextView textViewImeiNo;
LinearLayout layout;
public ImeiViewHolder(View view) {
super(view);
textViewImeiId = (AppCompatTextView) view.findViewById(R.id.textViewImeiId);
textViewImeiNo = (AppCompatTextView) view.findViewById(R.id.textViewImeiNo);
layout = (LinearLayout) view.findViewById(R.id.list_view_imei);
layout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
displayingAlertDialogimei();
}
}
}
first add id to your parent LinearLayout as, android:id="#+id/list_view"
and then update adapter class
public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {
private List<User> listUsers;
Context mContext;
ItemClickListenerLongPressed itemClickListenerLongPressed;
View itemView;
public UserRecyclerAdapterSavedUsers(List<User> listUsers) {
this.listUsers = listUsers;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_recycler_second, parent, false);
return new UserViewHolder(itemView);
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.textViewID.setText(listUsers.get(position).getUserid());
holder.textViewName.setText(listUsers.get(position).getName());
holder.textViewPassword.setText(listUsers.get(position).getPassword());
holder.textViewRole.setText(listUsers.get(position).getRole());
}
public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed){
this.itemClickListenerLongPressed=itemClickListenerLongPressed;
}
#Override
public int getItemCount() {
return listUsers.size();
}
private void displayingAlertDialog() {
//displaying alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
builder.setMessage("your toast message here...");
builder.setCancelable(true);
builder.setPositiveButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
}
/**
* ViewHolder class
*/
public class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//public AppCompatTextView ID;
public AppCompatTextView textViewID;
public AppCompatTextView textViewName;
public AppCompatTextView textViewPassword;
public AppCompatTextView textViewRole;
LinearLayout layout;
public UserViewHolder(View view) {
super(view);
textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);
textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);
textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);
textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);
layout = view.findViewById(R.id.list_view);
layout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
displayingAlertDialog();
}
}
You have not declare mContext in your adapter class.
In Adapter class constructor may change like this.
public UserRecyclerAdapterSavedUsers(List<User> listUsers,Context context) {
this.mContext= context;
this.listUsers1 = listUsers;
user= new User();
}
and Recycle view Activity class you have to change
UserRecyclerAdapterSavedUsers myAdapter = new RecyclerViewAdapter(yourList,this);
Use the Recyclerview Item click like this click here
and then you can access the interface in your activity or fragment and then you can add whatever you need.
giving toast and populating AlertDialog inside the Adapteris not the proper way of coding
I have a recycle view which populates data from a server, the components inside are a textView and a Switch. The server can return n number of data. How can i set a unique id to the Switch2 when I am populating the data, because later I will need to set a listener to the Switches, My server actually returns a unique id but I'm not so sure on how to set it to the Switch2, or is there any alternate parameters that can be used to identify the Switch?
layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="81dp">
<android:android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="1dp"
card_view:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Switch
android:id="#+id/switch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="254dp" />
<TextView
android:id="#+id/user_set_light_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:text="TextView"
android:textSize="20dp"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="23dp" />
</RelativeLayout>
</android:android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
adapter
public class populateLights_adapter extends RecyclerView.Adapter<populateLights_adapter.ViewHolder> {
private List<populate_lights> listItems;
private Context context;
public populateLights_adapter(List<populate_lights> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.addlight_items, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
populate_lights listItem = listItems.get(position);
holder.lightText.setText(listItem.getLightName());
holder.status.setChecked(listItem.getState());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView lightText;
public Switch status;
public ViewHolder(View itemView) {
super(itemView);
lightText = (TextView) itemView.findViewById(R.id.user_set_light_id);
status = (Switch) itemView.findViewById(R.id.switch2);
}
}
}
java class
public class populate_lights {
private String lightName;
private boolean state;
public populate_lights(String lightName, boolean state){
this.lightName = lightName;
this.state = state;
}
public String getLightName(){
return lightName;
}
public boolean getState(){
return state;
}
}
main
public class lightsControl extends Fragment {
View myView;
public static final String URL = "serverurl.com";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<populate_lights> listItems;
private Switch mySwitch;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.lightscontrol, container, false);
recyclerView = (RecyclerView) myView.findViewById(R.id.lightsView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
listItems = new ArrayList<>();
loadData();
adapter = new populateLights_adapter(listItems, myView.getContext());
recyclerView.setAdapter(adapter);
return myView;
}
private void loadData(){
final ProgressDialog progressDialog = new ProgressDialog(myView.getContext());
progressDialog.setMessage("Loading");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
Snackbar mySnackbar = Snackbar.make(myView, "Data Fetched!", Snackbar.LENGTH_SHORT);
mySnackbar.show();
Log.v("DATA_RESPONSE", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("lightData");
for(int i=0;i<array.length();i++){
JSONObject obj = array.getJSONObject(i);
Log.v("LIGHT ID ", "index=" + obj.getString("LightID"));
Log.v("Value ", "index=" + obj.getBoolean("Value"));
populate_lights popLights = new populate_lights(
obj.getString("LightID"), //unique id
obj.getBoolean("Value") //value returns true, or false
);
listItems.add(popLights);
}
adapter = new populateLights_adapter(listItems, myView.getContext());
recyclerView.setAdapter(adapter);
}
catch(Exception e){
e.printStackTrace();
}
}},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Snackbar mySnackbar = Snackbar.make(myView, "Oops, there was an error communicating with our server, try again", Snackbar.LENGTH_SHORT);
mySnackbar.show();
Log.v("LoginFormERROR", "index=" + error);
progressDialog.dismiss();
}
}
)
};
RequestQueue requestQueue = Volley.newRequestQueue(myView.getContext());
requestQueue.add(stringRequest);
}
public void showErrorAlert(){
AlertDialog.Builder builder1 = new AlertDialog.Builder(myView.getContext());
builder1.setMessage("Opps, something went wring");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Main Menu",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getActivity().onBackPressed();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
}
screenshot
See if this code prints the right position in the logs. Put this inside the constructor of the view holder in the adapter class:
status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d("Position: ", String.valueOf(getLayoutPosition()));
}
});