Sid Sid - 1 month ago 16
Android Question

Recycler view with multiple view type dose not show multiple colors for an image view

I have created one list. In that I am showing expense amount, expense type, expense category and expense time.

I have used

RecyclerView
with multiple view type, for view type 1 I am showing a small circle and view type two it's a big circle.

Expense type is to show two different circles, one is small for time and another is big for date. Both I can see.

But now I want to give different colors on small circle i.e time depending on the time. The problem is I can see circle of only yellow color filled with yellow color which is for 12:30 and shows time and other circles are blank, no color no time only a border.

public class IAdapter extends RecyclerView.Adapter<IAdapter.ItemViewHolder> {

ArrayList<Expense> item;
public static final int TYPE1=1;
Context conext;

IAdapter(Context context,ArrayList<Expense> item) {
this.conext=context;
this.item=item;
}

@Override
public int getItemCount() {
return item.size();

}

@Override
public int getItemViewType(int position) {
return item.get(position).getExpenseType();// Assume that this return 1 0r 2
}

@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {

itemViewHolder.amount.setText(item.get(i).amount);
itemViewHolder.expense.setText(item.get(i).expense);

Expense expense = new Expense();
expense = item.get(i);

if(TYPE1==i) {

if(expense.getTime().equals("10:30")) {
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.GREEN);

itemViewHolder.small.setImageDrawable(drawable);
}else if(expense.getTime()
.equals("12:30")) {
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.YELLOW);
itemViewHolder.small.setImageDrawable(drawable);
}else if(expense.getTime().equals("4:30")) {
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.BLUE);
itemViewHolder.small.setImageDrawable(drawable);
}else if(item.get(i).time.equals("7:30")){
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.GRAY);
itemViewHolder.small.setImageDrawable(drawable);
}
}

}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType) {

if (viewType==TYPE1) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardlayout, viewGroup, false);
return new ItemViewHolder(itemView,viewType);
} else {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.bigcircle, viewGroup, false);
ItemViewHolder ivh = new ItemViewHolder(itemView, viewType);
return ivh;
}
}


public static class ItemViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView amount;
TextView expense;
ImageView small;
ImageView big;

ItemViewHolder(View itemView, int viewType) {
super(itemView);
amount = (TextView) itemView.findViewById(R.id.txtAmount);
expense = (TextView) itemView.findViewById(R.id.txtexpense);
cv = (CardView) itemView.findViewById(R.id.card_view);

if(viewType==TYPE1) {small = (ImageView) itemView.findViewById(R.id.small);}
else{ big=(ImageView)itemView.findViewById(R.id.big);
TextDrawable drawable5 = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("10Jan", Color.CYAN);
big.setImageDrawable(drawable5);}

}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}

}


Fragment :

public class ItemFragment extends Fragment {

RecyclerView recyclerView;

IAdapter adapter;
ArrayList<Expense> items;

public ItemFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
setHasOptionsMenu(true);
Expense e = new Expense();

recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imgbtn_fab);
LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());


items=new ArrayList<>();
recyclerView.setLayoutManager(llm);

recyclerView.setHasFixedSize(true);

initializeDataType1();

initRecyclerAdapter();

imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("list", items);
intent.putExtras(bundle);
startActivityForResult(intent, 101);
}
});
return view;
}

private void initRecyclerAdapter() {
adapter = new IAdapter(getActivity(),items);
recyclerView.setAdapter(adapter);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 101 && resultCode == 102) {
Bundle bundle = data.getExtras();
Expense ex = (Expense) bundle.getSerializable("ex");
items.add(ex);
initRecyclerAdapter();
}
super.onActivityResult(requestCode, resultCode, data);
}

private void initializeDataType1() {

items.add(new Expense("1000", "2000", 1,"10:30"));
items.add(new Expense("2000", "5000", 1,"12:30"));
items.add(new Expense("3000", "400", 2));
items.add(new Expense("1000", "4000", 1,"4:30"));
items.add(new Expense("3000", "3000", 2));
items.add(new Expense("2000", "100", 1,"4:30"));
items.add(new Expense("2000", "3333", 2));
items.add(new Expense("3000", "shopping", 1,"7:30"));
items.add(new Expense("1000", "food", 1,"7:30"));
items.add(new Expense("1000", "food", 2));
items.add(new Expense("2000", "drink", 1,"7:30"));
items.add(new Expense("3000", "shopping", 2,"10:30"));
items.add(new Expense("2000", "3333", 1,"7:30"));
items.add(new Expense("3000", "shopping", 1,"7:30"));
items.add(new Expense("1000", "food", 1,"12:30"));
items.add(new Expense("1000", "food", 1,"7:30"));
items.add(new Expense("2000", "drink", 1,"10:30"));
items.add(new Expense("3000", "shopping", 1,"10:30"));
}
}


It's comparing for only 12:30 time and others are skipped. Why is this happening?

Answer

Your condition inside

public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) : if(TYPE1==i)

works only when i==1. Probably this is not what you want. Condition should look like:

if (TYPE1==getItemViewType(i))