Android list view show it button visible and invisible? - android

I am Android developer .I am used in json parser . i have to stored all data in Array List .What are the issue means i got custom id .They given 0 and 1 value passed.When i will get 0 means add button visible otherwise 1 will get in means custom button visible.How to check it in get View method.
I have added Array List .

just use the condition something like below
if (yourvalue.equals("0")
{
button.setVisibility(View.VISIBLE);
}
else
{
button.setVisibility(View.GONE);
}

Use the following code inside your get view method:
button.setVisibility((value==0)?View.VISIBLE:View.GONE);

Related

Android RecyclerView Custom Image Displaying

I have a recyclerview which displays data from an SQLite database.
I have a list of icons that I save in an ArrayList and apply color to them inside the ArrayList too.
Example: mMoodIcons.add(new MoodIcons(R.drawable.excited_icon, R.mipmap.background_clouds_excited, ContextCompat.getColor(mContext, R.color.excited), mContext.getString(R.string.excited)));
In the recycerview I have this code:
if(currentLog.getMoodBefore() == 3){
holder.moodIcon.setImageResource(R.drawable.confident_icon);
holder.moodIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.confident));
}
I need to make it dynamic so, for example, each mood has a number 1 = happy, 2 = fine, etc
So when the recycler view is displaying the information from the log it can display the correct mood icon with the color applied.
Could someone help me with the logic to write this code? I'm hitting a wall.
EDIT: The way I have it written above works in the if statement. But that would require 18 if statements. Is there a slicker way of writing this, maybe using an arraylist and a for loop?
I solved this myself. I created an arraylist of the mood icons and colors. Then I used this code:
for (MoodIconsWorkoutLog moodBefore : moodIcons) {
if(moodBefore.getMoodBefore() == currentLog.getMoodBefore() ){
holder.moodIcon.setImageResource(moodBefore.getMoodImage());
holder.moodIcon.setColorFilter(moodBefore.getColour());
}
}
Worked perfectly

Getting zero element in listview without using the onsetclicklistener

I want to get the zero element postion in a listView without using the onclick listeners is there a way to do that.
Expected results
Get zero element position in a listview automatically without clicking the element
if(yourList.size!=0)
{
yourObject=yourList.get(0);
}
it should be like
if(yourList.size > 0)
{
yourObject=yourList.get(0);
}

FirebaseListAdapter displaying wrong data on scrolling

I am using FirebaseListAdapter to populate ListView. It is working fine for the data populated directly from Firebase. Here, I am trying to display/hide a tick image based on the status field. On initial load it is working fine, but when I scroll up and down the tick mark is getting set for some of the other items incorrectly. Need help! Here is my code inside populateView.
nameTextView.setText(appointment.getName());
timeTextView.setText(appointment.getTime());
if (appointment.getAppointmentStatus() == APPOINTMENT_COMPLETED) {
appStatusImage.setImageResource(R.drawable.ic_done_black_24dp);
} else {
appStatusImage.setVisibility(View.INVISIBLE);
}
This happens probably because you are using a RecyclerView, that meaning that the Views are reused instead of each object having it's own View. In other words, when you write:
if (appointment.getAppointmentStatus() == APPOINTMENT_COMPLETED) {
appStatusImage.setImageResource(R.drawable.ic_done_black_24dp);
}
Initially, you will set your View with the given image if it meets your if statement. But if the View itself is reused for another object that does not meet the if statement, the image resource will remain from the previous object property. What you should do is:
if (appointment.getAppointmentStatus() == APPOINTMENT_COMPLETED) {
appStatusImage.setImageResource(R.drawable.ic_done_black_24dp);
appStatusImage.setVisibility(View.VISIBLE);
} else {
appStatusImage.setVisibility(View.INVISIBLE);
}
And everything should work as you expect

how to get the Default Listview Value in android?

In my application i have customListView ,i need to get the first value of list view(i.e. Zero position value) when very first time activity is loaded without using OnItemClickLiseners and it is focusable.How Can i do?,please can any one help me.
Thanking in Advance.
You can either use
listView.getChildAt(0)
or
listView.getChildAt(listView.getFirstVisiblePosition());
This is based on the assumption that you need to pull out values of the item that is at the first (0th) position in the ListView.
Since you are using a Custom ListView, you should also have a custom Adapter (possibly an ArrayAdapter or a BaseAdapter) that you use to show the data etc.
In any case, just check for this in the getView():
if (position == 0) {
// GET WHAT EVER DATA YOU NEED OUT OF THE ITEM.
}
If you can add more info to the OP about how you deal with the data, perhaps this answer can be made a little bit more useful. For example, do you use a POJO class to bind the data to the ListView?

how to add firstvalue by default null in spinner.?

I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.

Categories

Resources