Cannot resolve method setText() in imageView - java

I'm trying to do a simple project with Recyclerview and Cardview in Android Studio following this tutorial.
But I'm getting an error in onBindViewHolder function line 32 saying it can't resolve the method setText() in imageView and I don't know why since mTitle is not an ImageView and it is a TextView:
package com.example.demoproject;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.content.Context;
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
ArrayList<Model> models; // this array list creates a list of arrays which parameters define in my model class
public MyAdapter(Context c, ArrayList<Model> models) {
this.c = c;
this.models = models;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,null); // this line inflate my row
return new MyHolder(view); // this will return my view to holder class
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); // here i is position
}
#Override
public int getItemCount() {
return 0;
}
}
MyHolder.java :
package com.example.demoproject.
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyHolder extends RecyclerView.ViewHolder {
ImageView mImaeView, mTitle;
public MyHolder(#NonNull View itemView) {
super(itemView);
this.mImaeView = itemView.findViewById(R.id.imageIv);
this.mTitle = itemView.findViewById(R.id.titleTv);
}
}

Your mTile is not a TextView it is a ImageView. Change it to this:
ImageView mImaeView;
TextView mTitle;
And make sure that itemView.findViewById(R.id.titleTv); returns TextView. Check in Your xml file (recycler view item) that You made it eveything well and titleTv is a TextView

because setText() method is set on textview not in imageView

Related

Is it possible change the fragment when listview click

I would like to change the fragment when listview item clicked under Bottom navigation activity
But I have not idea how to write the OnClickListener
Anyone can provide some hints or tell me what wrong in this program?
Here is the program
And thank you for spend the time to view my program
Thank you very much
package com.example.campus.ui.campus;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.c.MainActivity;
import com.example.c.R;
import com.example.c.database.campus.CampusData;
import com.example.c.ui.campusInformation.CampusInformationActivity;
import com.example.c.ui.campusInformation.CampusInformationFragment;
import java.util.ArrayList;
import java.util.List;
public class CampusListLayoutAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<CampusData> campusList = new ArrayList<CampusData>();
private int resourceViewID;
private Context context;
private Context mContext;
static class ViewHolder{
LinearLayout llCampusCard;
TextView tvCampusName;
TextView tvCampusAddress;
ImageView ivCampusImage;
}
public CampusListLayoutAdapter(Context c, List<CampusData> campusList){
context = c;
layoutInflater = LayoutInflater.from(c);
this.campusList = campusList;
}
#Override
public int getCount() {
return campusList.size();
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
mContext = viewGroup.getContext();
ViewHolder holder = new ViewHolder();
view = layoutInflater.inflate(R.layout.listview_campus, null);
if(view != null){
holder.tvCampusName = view.findViewById(R.id.tvCampusName);
holder.tvCampusAddress = view.findViewById(R.id.tvCampusAddress);
holder.ivCampusImage = view.findViewById(R.id.ivCampusImage);
holder.tvCampusName.setText(campusList.get(i).name);
holder.tvCampusAddress.setText(campusList.get(i).address);
String image = campusList.get(i).image;
resourceViewID = context.getResources().getIdentifier(image, "drawable", context.getPackageName());
holder.ivCampusImage.setImageResource(resourceViewID);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}else {
return null;
}
}
}
First of all, create an interface in your adapter class.
public interface Callbacks {
void onItemClick(YourObject object, int position); // arguments as per the requirement
}
change your constructor of adapter as
Callbacks callback;
public CampusListLayoutAdapter(Context c, List<CampusData> campusList, Callbacks callback){
.....
this.callback = callback;
}
Now in your onClick() use..
callback.onItemClick(yourClickedItem, position)
then pass an anonymous or simply implement interface in your activity.
adapter = new CampusListLayoutAdapter(context, list, new CampusListLayoutAdapter.Callbacks() {
#Override
public void onItemClick(Alert_bean alert, int position) {
// do what you want here in activity like changing fragment or view updates
}
});

error: cannot find symbol variable itemsAdapter

I'm following a tutorial and I can't get my project to successfully run. I keep getting the error: cannot find symbol variable itemsAdapter, even though my class is in the same folder. Any ideas? Thank you
MainActivity.java
itemsAdapter.java
MainActivity.java:
package com.example.simpletodo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<String> items;
Button btnAdd;
EditText etItem;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItem = findViewById(R.id.etItem);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
items.add("Buy milk");
items.add("Go to the gym");
items.add("Email Autumn");
itemsAdapter ItemsAdapter;
ItemsAdapter = new itemsAdapter(items);
rvItems.setAdapter(itemsAdapter);
rvItems.setLayoutManager(new LinearLayoutManager(this));
}
}
ItemsAdapter.java:
package com.example.simpletodo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class itemsAdapter extends
RecyclerView.Adapter<itemsAdapter.ViewHolder>{
List<String> items;
public itemsAdapter(List<String> items) {
this.items = items;
}
// #NonNull
#Override
// creates each view
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// Use layout inflator to inflate a view
View todoView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
// wrap it inside a View Holder and return it
return new ViewHolder(todoView);
}
// responsible for binding data to a particular view holder
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// Grab the item at the position
String item = items.get(position);
// Bind the item into specified view holder
holder.bind(item);
}
// the # of items available in the data
#Override
public int getItemCount() {
return 0;
}
// Container to provide easy access to views that represent each row of the list
class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(#NonNull View itemView) {
super(itemView);
}
// Update the view inside of the view holder with this data
public void bind(String item) {
}
}
In line 41 of MainActivity.java itemsAdapter needs to be ItemsAdapter
PS: Your naming standard for itemsAdapter is reversed from the convention. The class should be named ItemsAdapter and the variable/instance should be itemsAdapter

RecyclerView that's supposed to diplay cardViews doesn't show anything

I have a recyclerView that must show a list of cards, but it doesn't!
There is an arrayList of recipes get passed to the adapter to display them as cardviews, everything in the debugging seems to be alright, but it doesnt display anything on the screen.
ViewHolder:
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.nd.ameer.bake.R;
public class RecipeViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public ImageView coverImageView;
public RecipeViewHolder(View v) {
super(v);
titleTextView = (TextView) v.findViewById(R.id.titleTextView);
coverImageView = (ImageView) v.findViewById(R.id.recipeImageView);
}
}
Adapter:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.example.nd.ameer.bake.R;
import com.example.nd.ameer.bake.models.Recipe;
import com.example.nd.ameer.bake.views.holders.RecipeViewHolder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class RecipeAdapter extends
RecyclerView.Adapter<RecipeViewHolder> {
ArrayList<Recipe> recipes = new ArrayList<>();
Context context;
public RecipeAdapter(ArrayList<Recipe> recipes, Context context) {
this.recipes = recipes;
this.context = context;
}
#Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recipe_item, parent, false);
return new RecipeViewHolder(view);
}
#Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
holder.coverImageView.setImageResource(R.color.colorPrimaryLight);
holder.titleTextView.setText(recipes.get(position).getName());
}
#Override
public int getItemCount() {
return recipes.size();
}
}
Fragment onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_recipe, container, false);
createRecipes();
recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recipe);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (recipes.size() > 0 & recyclerView != null) {
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new RecipeAdapter(recipes, getContext()));
}
return rootView;
}
The method createRecipes(); creates a list of recipes and then it get passed to the adapter.
as you can see, the recipes size is 4, so it's not the problem
I didn't know the cause of the problem till now, but I moved both the adapter and the view holder to the Recipes Fragment as inner classes, and this has fixed the problem.

java.lang.IllegalArgumentException: Context must not be null

I am using adapter to show images. In Adapter, How to get context when using Picasso?
I tried different solutions but I couldn't solve. What should I do when using picasso in Adapter? Context is a big problem for developers I see on the internet.
package gc.x;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.TextView;
import com.squareup.picasso.Picasso;
import gc.ovidos_challenge.R;
import gc.ovidos_challenge.models.Image;
import java.net.URL;
import java.util.List;
public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.MyViewHolder> {
private List<Image> imagesList;
private Context context;
public ImagesAdapter(Context context) {
this.context = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView imageview;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.textview);
imageview = (ImageView) view.findViewById(R.id.imageurl);
}
}
public ImagesAdapter(List<Image> imagesList) {
this.imagesList = imagesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_image, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Image image = imagesList.get(position);
holder.title.setText(image.title);
Picasso.with(this.context).load(image.url).into(holder.imageview);
// holder.imageview.setImageBitmap(image.getBitmapFromURL());
}
#Override
public int getItemCount() {
return imagesList.size();
}
}
you have created two constructors one for context and one for your data. use one constructor to initialize both of them
like this
public ImagesAdapter(Context context,List<Image> imagesList) {
this.context = context;
this.imagesList=imagesList;
}

proper way of creating method in your custom listview adapter

I created a simple android application with a ListView in it. I have my custom list adapter below:
package com.example.android.efaas;
import java.util.List;
import com.example.android.R;
import com.example.android.efaas.bean.ListItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater = null;
Context ctx;
List<ListItem> data;
public CustomListAdapter(Activity activity, List<ListItem> data){
ctx = activity;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int pos) {
return pos;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.list_menu, null);
ImageView img = (ImageView) rowView.findViewById(R.id.list_menu_image);
TextView title = (TextView) rowView.findViewById(R.id.list_menu_title);
ListItem item = data.get(pos);
img.setImageResource(item.getId());
title.setText(item.getTitle());
return rowView;
}
public ListItem getListItem(int pos){
return data.get(pos);
}
}
I have a public method getListItem, I tried to call it during ListView onClick but somehow I cant seem to make it work. Here's the image of the problem:
How do I resolve this?
You should cast adapter.getAdapter() to CustomListAdapter class then
the compile time error is due of the fact that adapter is not your CustomListAdapter. What you can do is cast the return value of adapter.getAdapter() to CustomListAdapter. Conversely you should use what's already available to retrieve the object at position. Like getItemAtPosition(int)

Categories