How To Toggle Listview Item Colors And Save It?
I'm working on an Attendance app. The app has ListActivity consisting of a list of students. I want to change color to RED and GREEN indicating absent and present. The problem is i
Solution 1:
Create Listener Interface:
publicinterfaceListListener {
voidclickListItem(int position);
}
Here is Model class:
publicclassRoute {
String studentName;
boolean colorRed;
publicRoute(String studentName, boolean colorRed) {
this.studentName=studentName;
this.colorRed=colorRed;
}
publicStringgetStudentName() {
return studentName;
}
publicvoidsetStudentName(String studentName) {
this.studentName = studentName;
}
publicbooleanisColorRed() {
return colorRed;
}
publicvoidsetColorRed(boolean colorRed) {
this.colorRed = colorRed;
}
}
Create Adapter class:
publicclassAAdapterextendsBaseAdapterimplementsView.OnClickListener {
Context context;
private List<Route> routes;
Holder holder;
privatestatic LayoutInflater inflater=null;
ListListener listListener;
publicAAdapter(Context context, List<Route> names,ListListener listListener) {
this.routes=names;
this.context=context;
this.listListener=listListener;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@OverridepublicvoidonClick(View view) {
listListener.clickListItem((Integer)view.getTag());
}
privateclassHolder
{
TextView tv;
}
@Overridepublic Route getItem(int position) {
return routes.get(position);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@OverridepublicintgetCount() {
return routes.size();
}
@NonNull@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
holder=newHolder();
convertView=inflater.inflate(R.layout.custom_layout,null);
holder.tv=(TextView)convertView.findViewById(R.id.textView);
holder.tv.setOnClickListener(this);
convertView.setTag(holder);
}else {
holder=(Holder)convertView.getTag();
}
holder.tv.setText(routes.get(position).getStudentName());
holder.tv.setTag(position);
if (!routes.get(position).colorRed){
holder.tv.setBackgroundColor(Color.GREEN);
}else {
holder.tv.setBackgroundColor(Color.RED);
}
return convertView;
}
}
Now MainActivity Class:
publicclassMainActivityextendsAppCompatActivityimplementsListListener{
AAdapter adapter;
ListView lv;
List<Route> myNames;
ListListener listListener=MainActivity.this;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listnames);
myNames=newArrayList<>();
/* DEMO DATA U NEED TO FETCH YOUR DATA */
myNames.add(newRoute("FIRST",false));
myNames.add(newRoute("Second",false));
myNames.add(newRoute("Third",false));
myNames.add(newRoute("Fourth",false));
myNames.add(newRoute("Fifth",false));
myNames.add(newRoute("FIRST",false));
myNames.add(newRoute("Second",false));
myNames.add(newRoute("Third",false));
myNames.add(newRoute("Fourth",false));
myNames.add(newRoute("Fifth",false));
myNames.add(newRoute("FIRST",false));
myNames.add(newRoute("Second",false));
myNames.add(newRoute("Third",false));
myNames.add(newRoute("Fourth",false));
myNames.add(newRoute("Fifth",false));
myNames.add(newRoute("FIRST",false));
myNames.add(newRoute("Second",false));
myNames.add(newRoute("Third",false));
myNames.add(newRoute("Fourth",false));
myNames.add(newRoute("Fifth",false));
adapter = newAAdapter(this, myNames,listListener);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@OverridepublicvoidclickListItem(int position) {
if(myNames.get(position).colorRed){
myNames.get(position).colorRed=false;
}else {
myNames.get(position).colorRed=true;
}
adapter.notifyDataSetChanged();
}
}
Post a Comment for "How To Toggle Listview Item Colors And Save It?"