This is my first time with recyclerview and I need some help. I am using a firebase and I want to hide an item(imageview) from my recyclerview adapter.
public RecyclerViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
image = itemView.findViewById(R.id.listrow_img1);
title = itemView.findViewById(R.id.listrow_tv1);
lock = itemView.findViewById(R.id.lock_accountImg);
}
I want to hide the lock image if my firebase realtime database don't have a child named("password"). if it has that child() the lock image will show up. Thank you in advance for those who help.
To hide or display a view according to a condition, create a method inside the ViewHolder class:
public void hideDisplayImageView(boolean passwordExist) {
if (passwordExist) {
lock.setVisibility(View.VISIBLE);
} else {
lock.setVisibility(View.GONE);
}
}
And call from within the onBindViewHolder method:
String password = list.get(position).getPassword();
if(password == null) {
holder.hideDisplayImageView(true);
} else {
holder.hideDisplayImageView(false);
}
You can try this like
if("your condition for the child is true"){
image.setVsisibility(View.VISIBLE)}
else{
image.setVsisibility(View.GONE)}
Kolin
val firebaseData = list.[position].getChild()
if(!firebaseData == "password")
holder.image.isVisible = false
else holder.image.isVisible = true
Java
String firebaseData = list.getPosition().getChild();
if(!firebaseData.contains("password")){
holder.image.setVisibility(View.GONE)
}else{
holder.image.setVisibility(View.VISIBLE)
}
Related
I have a recyclerview I want to display post in recycler view ..what my problem is I want to display only posts that as popularlist="true" in the database...there are photo that I don't want to display
please take a look at database structure ..over here popularlist="false" by default by all photo I want the recycler view to check if the popularlist="true" it needs to display in recycler view if its falseit should not display the photo ....can any one please tell me how can I do this ...
#Override
public int getItemViewType ( int position ) {
int viewType;
if (moviesList.get(position).getType_post().contains("Photo") ) {
viewType = IMAGE_TYPE;
} else if(moviesList.get(position).getType_post().contains("Video")){
viewType = VIDEO_TYPE;
}
return viewType;
}
I used this code for checking photo or video ...I don't know about filtering
what I have tried
I created an interface as listviewmodel below
interface ListViewModel {
int VIEW_TYPE_PHOTO = 1;
int VIEW_TYPE_VIDEO = 2;
int getViewType();
}
and created 2 model class called photoviewmodel and videoviewmodel which extends listviewmodel
class PhotoViewModel implements ListViewModel{
String image_path;
public PhotoViewModel(String image_path) {
this.image_path = image_path;
}
#Override
public int getViewType() {
return VIEW_TYPE_VIDEO;
}
}
in fragment where I get photo I add this code where recycler view is in
List<ListViewModel> listViewModels = new ArrayList<>();
for (Photo photo : photos) {
if(photo.getPopularlist().contains("true")){
if(photo.getType_post().equals("Photo")){
List<PhotoViewModel> pho = new ArrayList<>();
PhotoViewModel p = new PhotoViewModel();
p.setImagePath(photo.getImage_path());
pho.add(p);
listViewModels.add(new PhotoViewModel(photo.getImage_path(),photo.getDescription(),photo.getDate_created()));
// Log.d(TAG, "onDataChange: alphs"+photoViewModel1.getImagePath());
}
if(photo.getType_post().equals("Video")){
listViewModels.add(new VideoViewModel(photo.getImage_path()));
}
}
}
it display the body but I am not getting image path and other thing which I have add in fragment over here
You need to add a query:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Query query=ref.orderByChild("popularlist").equalTo(true);
Then you will be able to retrieve the data that only has popularlist=true
https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query
i am starting with creating android apps, i already have some basic experience with C# and Java. Now i'm stuck at a strange problem, see below:
public void pictureSwitch (View view){
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
boolean switched = false;
if (switched){
imageView.setImageResource(R.drawable.catstart);
Log.i("Status-Start", "Wert: " + switched);
switched = false;
} else {
imageView.setImageResource(R.drawable.catswitch);
Log.i("Status-switched", "Wert: " + switched);
switched = true;
}
}
}
The Problem is, on the first click on the Button it changes to the catswitch drawable. But it never switches back and i dont know why.
Thanks for your help in advance.
Lets have a boolean as instance variable.
private boolean switched = false;
public void pictureSwitch() {
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
if (switched) {
imageView.setImageResource(R.drawable.catstart);
} else {
imageView.setImageResource(R.drawable.catswitch);
}
switched = !switched;
}
And you do not need a parameter View view if you are not using it .
I don't know what's wrong with my code, that it always returns null when I use getParseObject().
I'm using parse.com to save my data, and in one table I used one file as a pointer. I have a Game class that has ImgName as a Pointer<Gallery> to a gallery class.
Now I want to retrieve the ImgName value, so this is what I did:
public Adapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Game");
query.include("ImgName");
return query;
}
});
}
// Customize the layout by overriding getItemView
#Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_item_landing_cards, null);
}
ParseObject gallery = object.getParseObject("ImgName");
String name=gallery.getString("name");
TextView nameTextView = (TextView) v.findViewById(R.id.text);
nameTextView.setText(name);
But I'm getting null all the time.
Any suggestions?
Use this for the re-use issue:
ParseObject gallery = object.getParseObject("ImgName");
if (gallery != null) {
String name=gallery.getString("name");
TextView nameTextView = (TextView) v.findViewById(R.id.text);
nameTextView.setText(name);
} else {
nameTextView.setText(""); // or any other default value you want to set
}
NOTE:
The cell re-use issue is not on Parse. Cell re-use is a general concept used by the ListView. The cells are recycled for performance by Android. We just have to protect it from re-using old values.
In my main activity I display a ListView which uses a custom BaseAdapter (ThoughtListAdapter).
listView = (ListView) findViewById(R.id.list);
adapter = new ThoughtListAdapter(this, resultingThoughts);
listView.setAdapter(adapter);
Every item in the ListView has a custom layout containing a TextView and two Button.
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}
thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
When a Button is clicked an AsyncTask (AsyncPost) is called which connects to my database and makes some changes.
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("LIKE CLICKED");
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 1;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
What I need is making both Button-s of a list item disappear after the AsyncTask is done with a successful outcome. I have a method onComplete(JSONObject json) which elaborates the JSONObject returned by the AsyncTask. I try to make the buttons non visible inside the onComplete method, but this doesn't work because onComplete() doesn't know which exact button has been clicked.
How can I pass an instance of the exact clicked button inside onComplete() and make disappear only the Like and Dislike buttons of the concerned list item?
AsyncPost is a global AsyncTask used by all my other activities. I would strongly prefer to leave it alone. The onComplete() method functions as the onPostExecute() method of the AsyncTask.
Here are the getView() and onComplete() methods inside my BaseAdapter, which contain all the code shown above.
Thank you.
public View getView(final int position, View convertView, ViewGroup parent) {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item_thought, null);
}
thoughtText = (TextView) convertView.findViewById(R.id.thought_text_view);
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
//thoughtItems is a list of custom ojbects (Thought)
Thought t = thoughtItems.get(position);
//Here i set the content of the current TextView
thoughtText.setText(t.getText());
//the two buttons do basically the same thing when get clicked
likeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 1;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
dislikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thought t = thoughtItems.get(position);
thoughtId = t.getId();
opinion = 0;
AsyncPost asyncPost = new AsyncPost(activity,ThoughtListAdapter.this);
asyncPost.execute(SHARE_THOUGHT_URL,
TAG_PERSON_EMAIL, "m#b.it",
TAG_THOUGHT_ID, thoughtId.toString(),
TAG_OPINION, opinion.toString());
}
});
return convertView;
}
#Override
public void onComplete(JSONObject json) {
if (json != null) {
try {
if (json.getInt(TAG_SUCCESS) == 0) {
Toast.makeText(activity, "Operazione non riuscita.", Toast.LENGTH_LONG).show();
} else {
//if everything is good i try to make the buttons of that particular list item disappear
likeButton.setVisibility(View.GONE);
dislikeButton.setVisibility(View.GONE);
}
}
catch (JSONException e) {
Log.e(TAG_LOG, "JSONException", e);
}
}
else Toast.makeText(activity, "Errore connessione!", Toast.LENGTH_LONG).show();
}
One solution to this would be to have something on your Thought object to indicate whether or not to show the buttons.
So in your getView() method you check this
likeButton = (Button) convertView.findViewById(R.id.thought_like_button);
dislikeButton = (Button) convertView.findViewById(R.id.thought_dislike_button);
Thought t = thoughtItems.get(position);
if (t.hideButtons()) {
likeButton.setVisibility(View.GONE);
dislikeButton.setVisibility(View.GONE);
}
else {
likeButton.setVisibility(View.VISIBLE);
dislikeButton.setVisibility(View.VISIBLE);
}
Then you would need to have your onComplete method return the id of the Thought object that it related to. Then inside your onComplete you could do
int id = //get your id from your JSON response
for(Thought t : thoughtItems) {
if (t.getId() == id) {
t.setHideButtons(true);
notifyDataSetChanged();
break;
}
}
By calling notifyDataSetChanged() it will redraw your list and when it does the check for whether it should show the buttons or not it will not show them because it was set on that thought item
How can i do this?
something:
final View view=FLall.getChildAt(i);
if (view.getType()==ImageView) {
...
}
If, for some strange reason, you can't use Asahi's suggestion (using tags), my proposition would be the following:
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
// do what you want with imageView
}
else if (view instanceof TextView) {
TextView textView = (TextView) view;
// do what you want with textView
}
else if ...
I try the following and it worked:
View view=FLall.getChildAt(i);
Log.i("ViewName", view.getClass().getName());
For Others who check this Question,in some cases instanceof does not work(I do not know why!),for example if your want to check if view type is ImageView or ImageButton(i tested this situation) , it get them the same, so you scan use this way :
//v is your View
if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageView")) {
Log.e("imgview", v.toString());
imgview = (ImageView) v;
} else if (v.getClass().getName().equalsIgnoreCase("android.widget.ImageButton")) {
Log.e("imgbtn", v.toString());
imgbtn = (ImageButton) v;
}
You can use tag for that purpose:see set/getTag methods at http://developer.android.com/reference/android/view/View.html
I am using this solution for KOTLIN code, so going off of Arash's solution:
if(v.javaClass.name.equals("android.widget.ImageView", ignoreCase = true)) ...
using this didn't work for me, but tweaking it to:
if(v.javaClass.name.contains("ImageView", ignoreCase = true)) ...
worked for me!
In Kotlin you can check this by using "is":
val view = findViewById<View>(R.id.a_view)
if (view is ImageView) {
print("This is a ImageView")
} else {
print("This is not a ImageView")
}
In Android Xamarin, This is how Android get type of a view and compare with controller type.
var view = FindViewById<TextView>(Resource.Id.emailText);
if (typeof(TextView).IsInstanceOfType(view))
{
var textView = (TextView)view;
}