Mir-wise98 Mir-wise98 - 7 days ago 7
Java Question

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 if I have long list of students than screen. The items that are down or up the current view loses color. Is my approach correct? How can I save the color of each individual list item. Of course if there is another best approch other than using
ListView
, I'm open to suggestions.

Here is the ListActivity containing the list

public class ListActivity extends Activity implements Serializable{
private String userName;
private TextView nameTextView;
private ListView nameList;
private CustomAdapter adapter;
private boolean colorRed;
private Class myClass;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

myClass = new Class();
colorRed = false;
// Open the students list from file if exists
openFromFile();

nameList = (ListView) findViewById(R.id.nameListView);
nameTextView = (TextView) findViewById(R.id.nameTextView);
Bundle extras = getIntent().getExtras();
if (extras != null) {
userName = extras.getString("NAME");
nameTextView.setText("Hi! " + userName);
}

adapter = new CustomAdapter(this, myClass.getStudentNames());
nameList.setAdapter(adapter);
TextView tv = (TextView) findViewById(R.id.sNametv);
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

if (colorRed == true) {
view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorPresent));
colorRed = false;
} else {
view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorAbsent));
colorRed = true;
}

// ALso could use this
// adapterView.getChildAt(position).setBackgroundColor(Color.RED);

}
});
}


I'm using a
boolean colorRed
for checking color as I'm not able to compare view color with color from
colors xml
.

nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (colorRed == true) {
view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorPresent));
colorRed = false;
} else {
view.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.colorAbsent));
colorRed = true;
}
}
});


CustomAdapter Class for ListView

public class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(Context context, ArrayList<String> names) {
super(context, R.layout.custom_layout, names);

}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_layout, parent, false);
String singleName = getItem(position);
TextView tv = (TextView) customView.findViewById(R.id.sNametv);
tv.setText(singleName);

return customView;
}
}


I've double checked it's a unique question! Rest Assured...

Thank You in Advance

Answer

Create Listener Interface:

public interface ListListener {
     void clickListItem(int position);
}

Here is Model class:

public class Route {
    String studentName;
    boolean colorRed;

    public Route(String studentName, boolean colorRed) {
        this.studentName=studentName;
        this.colorRed=colorRed;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public boolean isColorRed() {
        return colorRed;
    }

    public void setColorRed(boolean colorRed) {
        this.colorRed = colorRed;
    }
}

Create Adapter class:

public class AAdapter  extends BaseAdapter implements View.OnClickListener {

    Context context;
    private List<Route> routes;
    Holder holder;
    private static LayoutInflater inflater=null;
    ListListener listListener;

    public AAdapter(Context context, List<Route> names,ListListener listListener) {
       this.routes=names;
        this.context=context;
        this.listListener=listListener;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void onClick(View view) {
        listListener.clickListItem((Integer)view.getTag());
    }

    private class Holder
    {
        TextView tv;
    }

    @Override
    public Route getItem(int position) {
        return routes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return routes.size();
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView==null){
            holder=new Holder();

            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:

public class MainActivity extends AppCompatActivity implements ListListener{

    AAdapter adapter;
    ListView lv;
    List<Route> myNames;
    ListListener listListener=MainActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.listnames);


        myNames=new ArrayList<>();

        /* DEMO DATA U NEED TO FETCH YOUR DATA */
        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));

        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));

        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));

        myNames.add(new Route("FIRST",false));
        myNames.add(new Route("Second",false));
        myNames.add(new Route("Third",false));
        myNames.add(new Route("Fourth",false));
        myNames.add(new Route("Fifth",false));


        adapter = new AAdapter(this, myNames,listListener);
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void clickListItem(int position) {
        if(myNames.get(position).colorRed){
           myNames.get(position).colorRed=false;
        }else {
            myNames.get(position).colorRed=true;
        }
        adapter.notifyDataSetChanged();
    }
}
Comments