Android getview move ROW to top with if conditions - java

Android getview with list view i need move row to top position with conditions. Now list view shows users and groups. I need move groups in top positions in list view
if (Dialog.Type.GROUP.equals(dialog.getType()))
I need group list moved to top. This my full code. Or if there is any way if string contain specific value can row move top?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
Dialog dialog = getItem(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.item_dialog, null);
viewHolder = new ViewHolder();
viewHolder.avatarImageView = (RoundedImageView) convertView.findViewById(R.id.avatar_imageview);
viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.name_textview);
viewHolder.lastMessageTextView = (TextView) convertView.findViewById(R.id.last_message_textview);
viewHolder.unreadMessagesTextView = (TextView) convertView.findViewById(
R.id.unread_messages_textview);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
List<DialogOccupant> dialogOccupantsList = dataManager.getDialogOccupantDataManager().getDialogOccupantsListByDialogId(
dialog.getDialogId());
if (Dialog.Type.PRIVATE.equals(dialog.getType())) {
User opponentUser = ChatUtils.getOpponentFromPrivateDialog(
UserFriendUtils.createLocalUser(currentUser), dialogOccupantsList);
if (opponentUser.getFullName() != null) {
viewHolder.nameTextView.setText(opponentUser.getFullName());
displayAvatarImage(opponentUser.getAvatar(), viewHolder.avatarImageView);
} else {
viewHolder.nameTextView.setText(resources.getString(R.string.deleted_user));
dataManager.getDialogDataManager().deleteById(dialog.getDialogId());
}
} else {
viewHolder.nameTextView.setText(dialog.getTitle());
viewHolder.avatarImageView.setImageResource(R.drawable.placeholder_group);
displayGroupPhotoImage(dialog.getPhoto(), viewHolder.avatarImageView);
}
return convertView;
}
private static class ViewHolder {
public RoundedImageView avatarImageView;
public TextView nameTextView;
public TextView lastMessageTextView;
public TextView unreadMessagesTextView;
}

Related

How to setTextColor based on String value in ListView?

I want to set text color of textview based on string STATUS.
If Status = RED, text should be red; if Status = GREEN, text should be green. I've tried many solutions but I cant still set color...
Here is my code:
Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewItem viewItem = null;
if (convertView == null) {
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater) this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInfiater.inflate(R.layout.listview_items, null);
viewItem.tvDescription = (TextView) convertView.findViewById(R.id.tv_description);
viewItem.tvDate = (TextView) convertView.findViewById(R.id.tv_date);
viewItem.tvPoints = (TextView) convertView.findViewById(R.id.tv_points);
viewItem.tvStatus = (TextView) convertView.findViewById(R.id.tv_status);
TextView tvpkt = (TextView) convertView.findViewById(R.id.tv_points);
TextView tvstatus = (TextView) convertView.findViewById(R.id.tv_status);
String status = tvstatus.getText().toString();
if (status.equals("Red")) {
tvpkt.setTextColor(Color.RED);
} else if (status.equals("Green")) {
tvpkt.setTextColor(Color.GREEN);
}
convertView.setTag(viewItem);
} else {
viewItem = (ViewItem) convertView.getTag();
}
viewItem.tvDescription.setText(valueList.get(position).Description);
viewItem.tvDate.setText(valueList.get(position).Date);
viewItem.tvPoints.setText(valueList.get(position).Points);
viewItem.tvStatus.setText(valueList.get(position).Status);
return convertView;
}
I think the String status doesn't match the conditions in the if-statement later.
One way to skip the case sensitivity of the String is to use equalsIgnoreCase which basically compares two Strings, ignoring case considerations.
Something like this:
if(status.equalsIgnoreCase("Red")){
tvpkt.setTextColor(Color.RED);
}
else if(status.equalsIgnoreCase("Green")){
tvpkt.setTextColor(Color.GREEN);
}
Another way is to capitalize the status before comparison by using toUpperCase() method, something like this:
String status = tvstatus.getText().toString().toUpperCase;
Then compare it with an uppercased version of strings:
if(status.equals("RED")){
tvpkt.setTextColor(Color.RED);
}
else if(status.equals("GREEN")){
tvpkt.setTextColor(Color.GREEN);
}
Your code is okay just the sequence is wrong. Also only view inflation and viewholder creation should be done in case convertview is null. Modify your code like this
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInfiater.inflate(R.layout.listview_items, null);
viewItem.tvDescription= (TextView)convertView.findViewById(R.id.tv_description);
viewItem.tvDate= (TextView)convertView.findViewById(R.id.tv_date);
viewItem.tvPoints= (TextView)convertView.findViewById(R.id.tv_points);
viewItem.tvStatus= (TextView)convertView.findViewById(R.id.tv_status);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.tvDescription.setText(valueList.get(position).Description);
viewItem.tvDate.setText(valueList.get(position).Date);
viewItem.tvPoints.setText(valueList.get(position).Points);
viewItem.tvStatus.setText(valueList.get(position).Status);
TextView tvpkt = (TextView)convertView.findViewById(R.id.tv_points);
String status= viewItem.tvstatus.getText().toString();
if(status.equalsIgnoreCase("Red"))
{
tvpkt.setTextColor(Color.RED);
}
else if(status.equalsIgnoreCase("Green"))
{
tvpkt.setTextColor(Color.GREEN);
}
return convertView;
}

Do I use ViewHolder correctly? Everithing is worked, but i still have doubts with the ListView optimization with holder recycler

I am just adapting my custom adapter code with ViewHolder so that i can optimize my list view with a recycler, but i am not sure if i do it right.
My view holder class:
public class ViewHolderTask {
int positionHolder;
TextView nameHolder;
TextView timeHolder;
TextView sessionHolder;
TextView dateHolder;
FloatingActionButton mFabTaskHolder;
public ViewHolderTask(View v, int position) {
this.positionHolder = position;
this.nameHolder = v.findViewById(R.id.taskNameText);
this.timeHolder = v.findViewById(R.id.timeTextView);
this.sessionHolder = v.findViewById(R.id.textViewSession);
this.dateHolder = v.findViewById(R.id.dateTextView);
this.mFabTaskHolder = v.findViewById(R.id.myFabTask);
}
My custom adapter class:
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolderTask holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE );
convertView = inflater.inflate(R.layout.task_row, parent, false);
holder = new ViewHolderTask(convertView, position);
convertView.setTag(holder);
}else{
holder = (ViewHolderTask) convertView.getTag();
}
Task task = taskArrayList.get(position);
//set the configurations
holder.getTimeHolder().setText(getTimeString(task.getTime()));
holder.getNameHolder().setText(task.getName());
holder.getDateHolder().setText(getDateString(task.getDate()));
holder.getSessionHolder().setText(getSessionString(task.getSession()));
//Set the FAB listener
addFabListener(holder.getmFabTaskHolder(), position);
//set the clicked background
if(TaskActivity.getIsClicked() && TaskActivity.getPositionClicked()-1 == position){
convertView.setBackgroundResource(R.color.backgroundSelectedItem);
}
return convertView;
}
Do I use it right?
Seems to be fine for me other than this portion of the code
//set the clicked background
if(TaskActivity.getIsClicked() && TaskActivity.getPositionClicked()-1 == position){
convertView.setBackgroundResource(R.color.backgroundSelectedItem);
}
You might need to reset the background resource back to default for the item which is not clicked. maybe you have to add "else" part to the "if"

Different Button in List view

I have two ArrayList namely "one" and "two" ["two" is always a sub-set of "one"], And i have a list view which is populating by Arraylist "one". Now am checking condition that if element of "two" is present in "one", then set ImageButton in list view and if not the set Button in list view.
Below is my getView method. Any help or keywords will be appreciated.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
//HERE HOW TO SWITCH TWO LAYOUTS..
convertView = inflater.inflate(R.layout.invite_row, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.textView6);
viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
viewHolder.button = (Button) convertView
.findViewById(R.id.childButton);
Typeface typeFace= Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
viewHolder.name.setTypeface(typeFace);
// viewHolder.button.setBackgroundResource(R.drawable.invitebuttonbackground);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
final String tempname = getItem(position);
viewHolder.name.setText(tempname);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position, temp, tempname);
}
}
});
return convertView;
}
one thing you can do is
View convertView;
if (whatever condition) {
convertView = inflater.inflate(R.layout.invite_row, null);
} else {
convertView = inflater.inflate(R.layout.invite_row_two, null);
}
if you can post your layout , i will have clear undestanding of what you want to doing.

Android ListView Get row on button click

I have a custom adapter class for a listview and I want to be able to access the content of a specific row by clicking a button on it. I tried to create a ViewHolder, but I get a NPE error when I try to click it.
static class ViewHolder {
TextView camera;
TextView players;
TextView max_players;
ImageView privata;
Button Buton;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String variabile[] = getItem(position).split("\\s+");
LayoutInflater linflater = LayoutInflater.from(getContext());
View customView = linflater.inflate(R.layout.custom_row, parent, false);
final ViewHolder holder = new ViewHolder();
holder.camera = (TextView) customView.findViewById(R.id.Nume);
holder.players = (TextView) customView.findViewById(R.id.players);
holder.max_players = (TextView) customView.findViewById(R.id.max_players);
holder.privata = (ImageView) customView.findViewById(R.id.privata);
holder.Buton = (Button) customView.findViewById(R.id.Buton);
holder.camera.setText(variabile[0]);
if (!variabile[1].equals("true")) {
parola = false;
holder.privata.setVisibility(View.INVISIBLE);
}
holder.players.setText(variabile[2]);
holder.max_players.setText(variabile[3]);
room_id = variabile[4];
nume = variabile[5];
holder.Buton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hash = new HashMap<String, String>();
hash.put("name", nume);
hash.put("room", room_id);
if (intra) {
holder.Buton.setText("Iesi");
site = siteul + "/join";
intra = false;
} else {
holder.Buton.setText("Intra");
site = siteul + "/leave";
intra = true;
}
new ATask().execute(site);
}
});
return customView;
}
When using the ViewHolder pattern, you should check if the convertView in null or has been created before, in the getView method, and after that use setTag and getTag methods. like this :
if (convertView == null)
{
LayoutInflater linflater = LayoutInflater.from(getContext());
convertView = linflater.inflate(R.layout.your_list_item_view, parent, false);
viewHolder.textView = (TextView)convertView.findViewById([the id]);
.
.
.
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
You need to check if the convertView is null so it is already has been visited or not then store the holder in tag Like
ViewHolder holder;
if (convertView == null) {
LayoutInflater linflater = LayoutInflater.from(getContext());
holder = linflater.inflate(R.layout.custom_row, parent, false);....
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}//Common code

Should I use viewHolders if I use different ViewItemTypes?

My initial question was posted here:
ListView for messaging app shows wrong listItem layout after scrolling
But I am a bit confused since there are two answers that were upvoted and I'd like to use both of them. First, I think it is a safe assumption that I should use the getItemViewType method to help me with performance. After that though, should I still use the viewHolder pattern as described in Googles documentation on Making ListView Scrolling Smooth?
If I do use the ViewHolder code, do I incorporate it into getView?
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}
static public enum LAYOUT_TYPE {
INBOUND,
OUTBOUND
}
#Override
public int getViewTypeCount () {
return LAYOUT_TYPE.values().length;
}
#Override
public int getItemViewType (int position) {
if ( messages.get(position).isOutbound())
return LAYOUT_TYPE.OUTBOUND.ordinal();
else
return LAYOUT_TYPE.INBOUND.ordinal();
}
#Override
public View getView (int position, View convertView, ViewGroup parent) {
LAYOUT_TYPE itemType = LAYOUT_TYPE.values()[getItemViewType(position)];
... (code until inflater )
switch (itemType){
case INBOUND:
convertview = /inflate & configure inbound layout
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
break;
case OUTBOUND:
convertview = /inflate & configure outbound layout
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
break;
}
//Typical getView
public View getView(int position, View containerRow, ViewGroup parent) {
if (containerRow == null) {
//One time inflate
containerRow = inflater.inflate(R.layout.yourLayout, parent, false);
//instantiate all view from layout in levent relevent object.
//One time instantiation of viewholder
viewHolder = new ViewHolder();
viewHolder.someview = viewFromLayou;
//finally
containerRow.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) containerRow.getTag();
}
}
//In your case it should be
public View getView(int position, View containerRow, ViewGroup parent) {
if (containerRow == null) {
//One time inflate
containerRow = inflater.inflate(R.layout.yourLayout, parent, false);
//instantiate all view from layout in levent relevent object.
//One time instantiation of viewholder
if(inbound)
inBoundViewHolder = new ViewHolder();
else
outBoundViewHolder = new ViewHolder();
viewHolder.someview = viewFromLayou;
//finally
containerRow.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) containerRow.getTag();
}
}

Categories

Resources