In my App I developed a News feature with RecaylerView widget. Now I want to pass CoverImage and Date from this recyclerView to detail page of news. I can pass date of the news successfully. But the cover image is not shown in the detail page. I have found 0 image in my logcat in android monitor window. I cannot not identify what would be the problem here.
My Model class of news is-
public class NewsModel extends RealmObject {
#PrimaryKey
private int image;
private String title;
private String date;
private String detail ;
public NewsModel() {
}
public NewsModel(int image, String title, String date) {
this.image = image;
this.title = title;
this.date = date;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
......
}
My adaper class for news page is
public class NewsAdapter extends
RecyclerView.Adapter<NewsAdapter.NewsHolder> {
private List<NewsModel> newsObject;
private Context context;
public NewsAdapter(Context context, List<NewsModel> newsObject) {
this.context = context;
this.newsObject=newsObject;
}
#Override
public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_row_layout,parent,false);
return new NewsHolder(view);
}
#Override
public void onBindViewHolder(NewsAdapter.NewsHolder holder, int position) {
final NewsModel newsModel=newsObject.get(position);
holder.newsImage.setImageResource(newsModel.getImage());
holder.newsHeadline.setText(newsModel.getTitle());
holder.newsDate.setText(newsModel.getDate());
holder.cardView.setTag(position);
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int pos = (int)view.getTag();
openDetailNews(Integer.toString(newsModel.getImage()),newsModel.getDate());
}
});
}
//open Detail News Page
private void openDetailNews(String...details) {
Intent i=new Intent(context,DetailNews.class);
i.putExtra("image",details[0]);
i.putExtra("date",details[1]);
context.startActivity(i);
}
#Override
public int getItemCount() {
return newsObject.size();
}
public class NewsHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public ImageView newsImage;
public TextView newsHeadline;
public TextView newsDate;
public NewsHolder(View itemView) {
super(itemView);
newsImage=(ImageView)itemView.findViewById(R.id.news_picture);
newsHeadline=(TextView)itemView.findViewById(R.id.news_headline);
newsDate=(TextView) itemView.findViewById(R.id.news_date);
cardView=(CardView)itemView.findViewById(R.id.cardview_news);
}
}
}
The detail news page code where I want to get the cover image and date from news page is-
public class DetailNews extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<NewsImageModel> newsObject;
ImageView _coverImage;
TextView _description;
TextView _newsDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
// Showing and Enabling clicks on the Home/Up button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
newsObject=getAllImageList();
// setting up views and stuff
setUpUIViews();
Intent intent = getIntent();
//RECEIVE DATA
Log.e("_coverImage",""+_coverImage);
Integer coverImage=intent.getExtras().getInt("image");
Log.e("coverImage",""+coverImage);
String newsDate=intent.getExtras().getString("date");
//BIND DATA
_coverImage.setImageResource(coverImage);
Log.e("coverImage",""+coverImage);
_newsDate.setText(newsDate);
}
private void setUpUIViews() {
_coverImage=(ImageView)findViewById(R.id.news_cover);
_description=(TextView)findViewById(R.id.news_description);
_newsDate=(TextView)findViewById(R.id.news_date);
recyclerView = (RecyclerView)findViewById(R.id.image_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(DetailNews.this);
recyclerView.setLayoutManager(layoutManager);
adapter = new NewsDetailAdapter(this,newsObject );
recyclerView.setAdapter(adapter);
}
private List<NewsImageModel> getAllImageList() {
List<NewsImageModel> images = new ArrayList<NewsImageModel>();
......
return images;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
}
Use this :
public void onClick(View view) {
int pos = (int)view.getTag();
Intent i=new Intent(context,DetailNews.class);
i.putExtra("image",newsModel.getImage());
i.putExtra("date",newsModel.getDate());
context.startActivity(i);
}
and then in your activity:
Intent intent = getIntent();
int imageId = intent.getIntExtra("image",0);
then use this resource id to set your image
_coverImage.setImageResource(imageId);
Try it!
String coverImage=intent.getExtras().getString("image");
int convertCoverImage = Integer.valueOf("coverImage");
_coverImage.setImageResource(convertCoverImage);
You can't directly show a string url or any integer type into a imageview . You need to add Image lib's like Picaso or glide.
follow below steps:
1.Add dependencies:
compile 'com.squareup.picasso:picasso:2.5.2'
2.In your adapter insted of your old code replace with
Picasso.with(context).load(newsModel.getImage()).into(holder.newsImage);
Your problem will be solved.
private int image;
What integer data you are setting for the image? Is it a resource image?
Related
I have a ListView developed in Android Studio, I have a few items that will go to a specific activity however the majority of the items will go to a Detail_Activity. The problem that I am having is that I don't know how to get the image and title text from the ListView to display on the Detail_Activity. Can someone explain how I can make this happen? Thanks
I have tried to use the code that I developed as shown but it will not display the image or name on the Detail_Activity when the row is clicked.
Here is the code minus all the list items in MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);
final ArrayList<Object> list = new ArrayList<>();
list.add(new String("Government Codes"));
list.add(new LTCItem("Reciprocity", "Agreements With Other States", R.drawable.handshake));
listView.setAdapter(new LTCAdapter(this, list));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
if (position == 2) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
} else {
Intent intent = new Intent(MainActivity.this, Detail.class);
startActivity(intent);
}
}
});
}
}
Here is the code from the Detail_Activity.java file:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Detail extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
}
}
Here is the code from the Item.java file:
public class LTCItem {
private String name;
private String subtitle;
private int image;
public LTCItem(String name, String subtitle, int image) {
this.name = name;
this.subtitle = subtitle;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
Here is the code from the Adapter.java file:
public class LTCAdapter extends BaseAdapter {
ArrayList<Object> list;
private static final int LTC_Item = 0;
private static final int HEADER = 1;
private LayoutInflater inflater;
public LTCAdapter(Context context, ArrayList<Object> list) {
this.list = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getItemViewType(int position) {
if (list.get(position) instanceof LTCItem) {
return LTC_Item;
} else {
return HEADER;
}
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return 1;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
switch (getItemViewType(i)) {
case LTC_Item:
view = inflater.inflate(R.layout.item_list_view, null);
break;
case HEADER:
view = inflater.inflate(R.layout.item_listview_header, null);
break;
}
}
switch (getItemViewType(i)) {
case LTC_Item:
ImageView image = (ImageView) view.findViewById(R.id.itemListViewImgIcon);
TextView name = (TextView) view.findViewById(R.id.itemListViewTxtTopicName);
TextView subtitle = (TextView) view.findViewById(R.id.itemListViewTxtTopicSubtitle);
image.setImageResource(((LTCItem) list.get(i)).getImage());
name.setText(((LTCItem) list.get(i)).getName());
subtitle.setText(((LTCItem) list.get(i)).getSubtitle());
break;
case HEADER:
TextView title = (TextView) view.findViewById(R.id.itemListViewHeader);
title.setText(((String) list.get(i)));
break;
}
return view;
}
}
I expect the Detail_Activity to display name, image from the ListView when clicked, and I will add a description text to the Detail_Activity as well.
for this, you just have to pass your data using intent put extra methods like this in the first activity
Intent = intent = new Intent(this , Detail.class)
intent.putString("name",list.get(position).getName());
intent.putString("image",list.get(position).getImage());
startctivity(intent)
Now just get your data in the Detail activity's onCreatae method just like this
if(getIntent().getExtras()!=null){
String namae = getIntent().getStringExtra("name");
String image= getIntent().getStringExtra("image");
}
Now you can use your image or name where you want to use them
You should add it in intent extra
From main activity:
Intent i = new Intent(MainActivity.this, DetailActivity.class);
i.putExtra("title",list.get(position).getTitle)
startActivity(i);
In detail activity:
String title = getIntent().getStringExtra("title");
friends...
In my project, I am creating a list of images from the server and showing in Recyclerview.
now I want to remove a particular item from the list when I click on the verify button.
I tried to hide holder.itemview but its see again when I scroll.
I just want to remove or hide that item which is verified once.
here is my code :
1) Main activity
public class MainActivity extends AppCompatActivity {
private static final String url = "www.mysite.com/api/api-id-list.php?action=imgs";
private ProgressDialog mDetailProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
LoadRecyclerView();
}
private void LoadRecyclerView() {
mDetailProgress = new ProgressDialog(this);
mDetailProgress.setTitle("Loading images");
mDetailProgress.setMessage("Please Wait...");
mDetailProgress.setCanceledOnTouchOutside(false);
mDetailProgress.show();
final RecyclerView imgList = (RecyclerView) findViewById(R.id.imgList);
imgList.setLayoutManager(new LinearLayoutManager(this));
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Image[] images = gson.fromJson(response, Image[].class);
imgList.setAdapter(new ImageAdapter(MainActivity.this, images));
mDetailProgress.dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), "Something went wrong..!", Toast.LENGTH_LONG);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}}
2) ImageAdapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private ProgressDialog mDetailProgress;
private Context context;
private Image[] data;
Button btn_ban, btn_verify;
private View view;
public ImageAdapter (Context context, Image[] data) {
this.context = context;
this.data = data;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_item, parent, false);
return new ImageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
final Image imagelist = data[position];
holder.userCode.setText(imagelist.getCode());
Glide.with(holder.userImage.getContext()).load(imagelist.getIdPhoto()).into(holder.userImage);
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.itemView.setVisibility(View.GONE);
}
});
}
#Override
public int getItemCount() { return data.length; }
public class ImageViewHolder extends RecyclerView.ViewHolder {
TextView userCode;
ImageView userImage;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
userImage = (ImageView) itemView.findViewById(R.id.card_iv_img);
userCode = (TextView) itemView.findViewById(R.id.card_tv_code);
btn_ban = (Button) itemView.findViewById(R.id.btn_ban);
btn_verify = (Button) itemView.findViewById(R.id.btn_verify);
}
}}
3) Image.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
#SerializedName("per_ID")
#Expose
private String perID;
#SerializedName("code")
#Expose
private String code;
#SerializedName("first_nm")
#Expose
private String firstNm;
#SerializedName("last_nm")
#Expose
private String lastNm;
#SerializedName("photo_ID")
#Expose
private String photoID;
#SerializedName("id_photo")
#Expose
private String idPhoto;
public String getPerID() {
return perID;
}
public void setPerID(String perID) {
this.perID = perID;
}
public Image withPerID(String perID) {
this.perID = perID;
return this;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Image withCode(String code) {
this.code = code;
return this;
}
public String getFirstNm() {
return firstNm;
}
public void setFirstNm(String firstNm) {
this.firstNm = firstNm;
}
public Image withFirstNm(String firstNm) {
this.firstNm = firstNm;
return this;
}
public String getLastNm() {
return lastNm;
}
public void setLastNm(String lastNm) {
this.lastNm = lastNm;
}
public Image withLastNm(String lastNm) {
this.lastNm = lastNm;
return this;
}
public String getPhotoID() {
return photoID;
}
public void setPhotoID(String photoID) {
this.photoID = photoID;
}
public Image withPhotoID(String photoID) {
this.photoID = photoID;
return this;
}
public String getIdPhoto() {
return idPhoto;
}
public void setIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
}
public Image withIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
return this;
}}
First make data not array, but List, so it's easy to remove item;
Secondly set listener for verify button inside ViewHolder and inside OnClick remove item and notify adapter.
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
data.remove(position);
notifyItemRemoved(position);
}
});
Note: Setting OnClickListener inside ViewHolder makes sure that only correct item is removed by using getAdapterPosition() to get correct position.
Position provided by onBindViewHolder might be invalid after new items are inserted.
I have an online quiz app in Firebase.
If my phone is in English, the app works fine, but when it change into Turkish, Piccaso does not load image.Please help me I can not find a solution for over a week
In this case my phone is in English
In this cas my phone is in Turkish
private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories) {
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category model, int position) {
viewHolder.category_name.setText(model.getName());
Picasso.get().load(model.getImage()).into(viewHolder.category_image);
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, final int position, boolean isLongClick) {
{
Intent play = new Intent(getActivity(), StartActivity.class);
Common.categoryId = adapter.getRef(position).getKey();
Common.categoryName = model.getName();
startActivity(play);
}
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}
View Holder
public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
View mV;
public TextView category_name;
public ImageView category_image,gradient2;
public Button btnPlay;
private ItemClickListener itemClickListener;
public CategoryViewHolder(View itemView) {
super(itemView);
mV = itemView;
category_image = (ImageView)itemView.findViewById(R.id.category_image);
category_name = (TextView)itemView.findViewById(R.id.category_name);
gradient2 = (ImageView)itemView.findViewById(R.id.gradient2);
btnPlay = (Button)itemView.findViewById(R.id.btn_play);
btnPlay.setTag(R.id.btn_play,itemView);
btnPlay.setOnClickListener(this);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
}
Catgeory Model.java
public class Category {
private String Name;
private String Image;
private String Button;
public Category() {
}
public Category(String name, String image, String button) {
this.Name = name;
this.Image = image;
this.Button = button;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getButton() {
return Button;
}
public void setButton(String button) {
Button = button;
}
}
I find solution.I changed catgegory model and firabse json file.Maybe in category model Name And İmage translated into Turkish when changed phone language.
you model should be the same firebase json file. I changed model and jason file then app works fine.
Json File
"Category" : {
"01" : {
"ad" : "Azərbaycan",
"sekil" : "https://avatanplus.com/files/resources/mid/5969ff2ae80a415d460cbfc6.jpg"
},
"02" : {
"ad" : "Türkiyə",
"sekil" : "https://img00.deviantart.net/6f00/i/2012/238/9/0/turkey_flag_grunge_hd_2_0_by_syndikata_np-d5che5q.jpg"
},
"03" : {
"ad" : "Viner",
"sekil" : "https://img.milli.az/2017/12/08/606172.jpg"
}
Category Model
public class Category {
private String ad;
private String sekil;
private String Button;
public Category() {
}
public Category(String ad, String sekil, String button) {
this.ad = ad;
this.sekil = sekil;
Button = button;
}
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
public String getSekil() {
return sekil;
}
public void setSekil(String sekil) {
this.sekil = sekil;
}
public String getButton() {
return Button;
}
public void setButton(String button) {
Button = button;
}
}
I find solution.I changed catgegory model and firabse json file.Maybe in category model Name And İmage translated into Turkish when changed phone language.
I have received a json data from rest api. This data contains id, title, body, an array of images "appImages" and a teaserImage. Now I deserialize the json in controller class. I have creted two adapters. First adpter is used for recyclerview. This recycler view is showing the title and and teasetImage. this portion working. If user click on item it redirect to detail activity, where he can see teaserImage as cover image, and the body as description. Now this layout I oroganised this way, at fisrt the ImageView for cover Image, TextView for Description. And below description I have created a recyclerView to show the array of images. The CoverImage and description of detail cativity is working well. For recyclerview I have created another Adpater, this adapter is used to show all the images based on the title of news. But I stuch to show those images in recyclerview. I have explained in deatil in my code.
My controller class
public class NewsController {
private static final String TAG = NewsController.class.getSimpleName();
private UserCallbackListener mListener;
private NewsRestApiManager mApiManager;
private AppImage appImages;
public NewsController(UserCallbackListener listener) {
mListener = listener;
mApiManager = new NewsRestApiManager();
}
public void startFetching(){
mApiManager.getNewsApi().getNews(new Callback<String>() {
#Override
public void success(String s, Response response) {
Log.d(TAG, "JSON :: " + s);
try {
JSONArray array = new JSONArray(s);
for(int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
NewsModel news = new NewsModel();
news.setTitle( jsonObject.optString( "title") );
news.setBody( jsonObject.optString( "body" ) );
ArrayList<AppImage> list = new ArrayList();
JSONArray imageArray =jsonObject.getJSONArray("appImages");
if (imageArray.length() > 1) {
for(int j=0; j<imageArray.length();j++){
appImages = new AppImage();
appImages.setSrc(new JSONArray( s ).getJSONObject( i ).getJSONArray( "appImages" ).getJSONObject( j ).getString( "src" ));
list.add(appImages);
}
}
news.setAppImages( list );
TeaserImageSmall coverImage=new TeaserImageSmall();
coverImage.setSrc( new JSONArray( s ).getJSONObject( i ).getJSONObject( "teaserImageSmall" ).getString( "src" ));
news.setTeaserImageSmall(coverImage);
mListener.onFetchProgressNews(news);
}
} catch (JSONException e) {
mListener.onFetchFailed();
}
mListener.onFetchComplete();
}
#Override
public void failure(RetrofitError error) {
Log.d(TAG, "Error :: " + error.getMessage());
mListener.onFetchComplete();
}
});
}
public interface UserCallbackListener{
void onFetchStart();
void onFetchProgressNews(NewsModel news);
void onFetchProgressNews(List<NewsModel> userList);
void onFetchComplete();
void onFetchFailed();
}
My adapter class for News Recyclerview. This is perfect now.
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsHolder>
........
public void addNews(NewsModel news) {
Log.d(TAG,news.getTeaserImageSmall().getSrc());
mNews.add(news);
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(NewsHolder holder, int position) {
final NewsModel currentNews = mNews.get(position);
Picasso.with(holder.itemView.getContext());
Picasso.with(holder.itemView.getContext()).load(currentNews.getTeaserImageSmall().getSrc()).into( holder.newsImage );
holder.newsHeadline.setText(currentNews.getTitle());
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(context,DetailNews.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("src",currentNews.getTeaserImageSmall().getSrc());
i.putExtra("title",currentNews.getTitle());
i.putExtra("body",currentNews.getBody());
context.startActivity(i);
}
});
Now Another Adapter for Array of images which will show in detail activity of news page.
Edited Adapter Class
public class NewsImageAdapter extends RecyclerView.Adapter<NewsImageAdapter.ImageHolder> {
public static String TAG = NewsImageAdapter.class.getSimpleName();
private Context context;
private List<AppImage> appImageList;
DetailNews detailNews = new DetailNews ();
public NewsImageAdapter(List<AppImage> imageObject,Context context) {
this.context = context;
this.appImageList = imageObject;
}
public void addImage(AppImage appImage) {
Log.d(TAG,appImage.getSrc());
appImageList.add(appImage);
notifyDataSetChanged();
}
#Override
public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.newsdetail_image_row,parent,false);
return new ImageHolder(view);
}
#Override
public void onBindViewHolder(ImageHolder holder, int position) {
final AppImage currentImage=appImageList.get(position);
//getting error for current news,
detailNews .navigate(context, appImageList, currentNews.getTeaserImageSmall().getSrc(), currentNews.getTitle(),currentNews.getBody());
Picasso.with(holder.itemView.getContext()).load(currentImage.getSrc()).into( holder.images);
}
#Override
public int getItemCount() {
return imageObject.size();
}
public class ImageHolder extends RecyclerView.ViewHolder {
public ImageView images;
public ImageHolder(View itemView) {
super(itemView);
images= itemView.findViewById(R.id.news_image);
}
}
}
Detail activity of news where I am shoing coverImage and description at this moment. But I also want to show the list of images below the description. I would like to how can I implement that.
Edited Detail Activity
public class DetailNews extends AppCompatActivity{
public class DetailNews extends AppCompatActivity{
private RecyclerView recyclerView;
private NewsImageAdapter adapter;
private List<AppImage> imageList= new ArrayList<>();
private NewsController mController;
private CardView cardview;
private ImageView _coverImage;
private TextView _newsHeading;
private TextView _description;
private TextView _newsDate;
private static List<AppImage> appImageList,mAppImageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
//newsObject=getAllImageList();
// setting up views and stuff
setUpUIViews();
Intent intent = getIntent();
//RECEIVE DATA
Log.e("_coverImage",""+_coverImage);
String coverImage = intent.getStringExtra ("src");
String heading=intent.getExtras().getString("title");
//String newsDate=intent.getExtras().getString("date");
String description=intent.getExtras().getString("body");
//BIND DATA
Picasso.with(this).load(coverImage ).into(_coverImage);
_newsHeading.setText(heading);
// _newsDate.setText(newsDate);
_description.setText(description);
Linkify.addLinks( _description,Linkify.WEB_URLS );
}
private void setUpUIViews() {
_coverImage=(ImageView)findViewById(R.id.news_cover);
_newsHeading=(TextView)findViewById(R.id.heading);
_description=(TextView)findViewById(R.id.news_description);
_newsDate=(TextView)findViewById(R.id.date);
cardview=(CardView) findViewById(R.id.cardView);
recyclerView = (RecyclerView)findViewById(R.id.image_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(DetailNews.this);
recyclerView.setLayoutManager(layoutManager);
adapter = new NewsImageAdapter(imageList,getApplicationContext() );
recyclerView.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
public void navigate(Context activity, List<AppImage> appImageList, String src, String title, String body) {
mAppImageList = appImageList;
Intent intent = new Intent(activity, DetailNews .class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("src",src);
intent .putExtra("title",title);
intent .putExtra("body",body);
activity.startActivity(intent);
try {
if (activity instanceof NewasPage) { //Error for news
((NewsPage) activity).finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
My Mdel Class is
public class NewsModel {
#Expose
private String _id;
#Expose
private String body;
#Expose
private String title;
#Expose
private List<AppImage> appImages;
public List<AppImage> getAppImages() {
return appImages;
}
public void setAppImages(List<AppImage> appImages) {
this.appImages = appImages;
}
AppImage Model Class
public class AppImage {
#Expose
private String _id;
#Expose
private String alt;
#Expose
private String src;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
In DetailNews Activity, add a method
private static List<AppImage> appImageList mAppImageList;
public void navigate(Context activity, List<AppImage> appImageList,String src,String title,String body) {
mAppImageList = appImageList;
Intent intent = new Intent(activity, DetailNews .class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("src",src);
intent .putExtra("title",title);
intent .putExtra("body",body);
activity.startActivity(intent);
try {
if (activity instanceof FirstActivity) {
((FirstActivity) activity).finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
And in the adapter do as
DetailNews detailNews = new DetailNews ();
detailNews .navigate(context, appImageList, currentNews.getTeaserImageSmall().getSrc(), currentNews.getTitle(),currentNews.getBody());
I am trying to show my integers saved in the firebase database in my Recyclerview. I have two Strings I can easily show in my App, but somehow it doesn't work with integers. Even more strange is that when I save a String where before there was an integer I got an error.
Here my database structure
Here I populate my Viewholder
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ListItem, ListViewHolder>(
ListItem.class,
R.layout.card_item,
ListViewHolder.class,
mDatabase.orderByChild("minusAccandShare").endAt(0)
) {
#Override
protected void populateViewHolder(ListViewHolder viewHolder, ListItem model, final int position) {
viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
viewHolder.setHeading(model.getHeading());
viewHolder.setStoreName(model.getStoreName());
viewHolder.setAcceptCount(model.getAcceptCount());
viewHolder.mView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(EXTRA_QUOTE, firebaseRecyclerAdapter.getRef(position).getKey());
i.putExtra(BUNDLE_EXTRAS, extras);
startActivity(i);
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
My ViewHolder
public static class ListViewHolder extends RecyclerView.ViewHolder {
View mView;
public ListViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setImage(Context ctx, String image){
ImageView postImage = (ImageView) mView.findViewById(R.id.im_item_icon);
Picasso.with(ctx).load(image).resize(200,200).into(postImage);
}
public void setHeading(String heading){
TextView card_heading = (TextView) mView.findViewById(R.id.lbl_item_sub_title);
card_heading.setText(heading);
}
public void setStoreName(String storeName){
TextView card_storeName = (TextView) mView.findViewById(R.id.lbl_item_text);
card_storeName.setText(storeName);
}
public void setAcceptCount(String accepts){
TextView accepts_count = (TextView) mView.findViewById(R.id.lbl_accepts_count);
accepts_count.setText(accepts);
}
}
And finally my model
public class ListItem {
private String heading, storeName, image;
private Long accepts;
public ListItem() {
}
public ListItem(String heading,String storeName, Long accepts, String image){
this.heading = heading;
this.storeName = storeName;
this.image = image;
this.accepts = accepts;
}
public String getHeading() {
return heading;
}
public String getStoreName() {
return storeName;
}
public String getImage() {return image;}
public String getAcceptCount() {
return String.valueOf(accepts);
}
In this way I see null where there should stand 200 instead. I tried everything. I even saved an String instead of an integer and tried to show it the same way as the heading String ( which works perfectly) but then I see a blank place... Please help. Thanks
you should create a getter and setter for accepts in ListItem.
public Long getAccepts() {
return accepts;
}
public void setAccepts(Long accepts) {
this.accepts = accepts;
}