Use of embedded checkbox in MVP mode ListView
This article is written a small demo, how to embed a checkbox in the ListView with the use of this article and the previous embedded button similar to the same, using the MVP mode of writing code, this case will have a few small details, I will be introduced in the case.
The basic framework of the procedure is as follows:
View Layer:
Mainactivity.java
Control name
for public class Mainactivity extends appcompatactivity implements viewinter<mybean>{//listview Private ListView mlist;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Mlist = (ListView) Findviewbyid (r.id.mlist);
Start requesting data new presenter (this) to the presenter Layer
. Load ()
;
/**
* Presenter layer passes data to the method via an interface callback
* This method enables you to pass data items to the adapter/
@Override public
Void ShowData (list<mybean> mybeen) {
Myadapter adapter = new Myadapter (mybeen);
Mlist.setadapter (adapter);
}
Viewinter.java
/**
* view layer All the interface updates, request data and other functions are written in this interface, easy to extend/public
interface viewinter<t> {
void ShowData (list<t> T);
}
Presenter Layer:
Presenter.java
public class Presenter {
//view Layer object, mainly calls its subclass of its own method, and then return the data
viewinter<mybean> VI;
Model layer of objects, mainly call its subclass function, realize data acquisition
modelinter mi;
/**
* Only need to pass the view layer of reference, the model layer of data from its own to deal with *
* public
presenter (Viewinter<mybean> vi) {
THIS.VI = VI;
mi = new Modelimp ();
}
/**
* The main function of this method is to be responsible for data transfer and notification processing data/public
void Load () {
//Call Mi.getdata method, can obtain the required data, and then callback to the view layer
Mi.getdata (New Modelinter.dealdata () {
@Override public
void SetData () {
list<mybean> data = new arraylist<> ();
for (int i = 0; i < i++) {
Mybean bean = new Mybean ();
Here is the random generation of the data in the entity class, that is, whether the Set check box defaults to the selected state
bean.setchecked (Math.random () > 0.5? true:false);
Data.add (bean);
Callback to pass data
Vi.showdata;}}
Model layer:
Modelinter.java
/**
* All model layer functions can be written in this interface, this interface facilitates the extension of new
functions
/public interface Modelinter {
void GetData (Dealdata dealdata);
public interface dealdata{
void SetData ();
}
Modelimp.java
The public class Modelimp implements modelinter{
/**
* implements data processing in which the parameters in the method are an interface type, and all passed values must implement the method of their definition.
@Override public
void GetData (Dealdata dealdata) {
dealdata.setdata ();
}
}
Adapter.java
The public class Myadapter extends Baseadapter implements Compoundbutton.oncheckedchangelistener {//Defines an object similar to the map collection and is more efficient than
Map is higher, used to save check box is currently clicked by the user after the state private sparsearray<boolean> array;
private context;
Private list<mybean> data;
Public Myadapter (list<mybean> data) {this.data = data;
Array = new Sparsearray ();
@Override public int GetCount () {return data = null 0:data.size ();
@Override public Object getitem (int i) {return data.get (i);
@Override public long Getitemid (int i) {return i; @Override public View getview (int position, View Convertview, ViewGroup parent) {Mybean bean = Data.get (positi
ON);
if (context = = null) context = Parent.getcontext ();
Viewholder holder = null;
if (Convertview = = null) {Convertview = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.list_item, NULL);
Holder = new Viewholder (); HOLDER.MTV = (TextView) convertview.fIndviewbyid (R.ID.MTV);
Holder.mcheck = (CheckBox) Convertview.findviewbyid (R.id.mcheck);
Convertview.settag (holder);
} holder = (Viewholder) convertview.gettag ();
Holder.mTv.setText ("check box" + position);
Set the check box's Listener event Holder.mCheck.setOnCheckedChangeListener (this);
Holder.mCheck.setText ("Programming" + position);
Set the corresponding position to Holder.mCheck.setTag (R.id.check, position) in tag; First, determine if the data in the data item is true, and set its default value//if (Data.get (position). ischecked ()//holder.mCheck.setChecked (data) if true. Get (position). ischecked ())//commented////(save user selected status to the corresponding item check box//holder.mCheck.setChecked (Array.get (position, false))
;//commented return Convertview;
@Override public void OnCheckedChanged (Compoundbutton Compoundbutton, Boolean B) {//Every time the event is triggered, get the value of the item position int i = (int) compoundbutton.gettag (r.id.check);//comments//Then save the state to the collection//Array.put (i, B);//comments//and modify the value of the data item
。 Data.get (i). setchecked (b);//commented} static class ViewholdeR {TextView mTv;
CheckBox Mcheck;
}
}
Mybean.java
The data item object public
class Mybean {
Boolean ischecked;
public Boolean ischecked () {return
ischecked;
}
public void setchecked (Boolean checked) {
ischecked = checked;
}
}
The basic code has been implemented, so let's take a look at the run effect chart that is generated if the code in the annotation in the GetView method in Myadapter.java is missing:
From this running effect, we can clearly see the small bug, at the beginning of the check box is not selected, when we select the first check box, when we drag down, you will see that the check box 7 is obviously followed by the election, when we select the check box 2, check box 8 is also selected, so what is the reason for this, because in the ListView of a reuse control mechanism, on this issue, the previous basis of this blog has explained the principle, this case does not do detailed explanation.
Finally, let's look at the results of the run of the code that cancels those annotations:
From the results of this operation you can see that because the default data selection is 1, 2, 5 when we cancel 1, 2 Select 0 o'clock, the following multiplexing components will not want to run the above results are reused, this is the normal choice, want to choose the choice, want to cancel on the cancellation.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.