I have a listview that contains a checkbox next to each item. I want to move items to the top or bottom of the list by checking or unchecking them. If I check the item, move to the top and I uncheck, move to the bottom.
I have tried to implement different sets of code, including setSelection(int position)
. I have been focusing on the if and else statements on the Adapter since they pertain to what happens when you check and uncheck the checkbox. When I do try to implement the setSelection(int position)
method, I get error messages. Is there a way to fix the code so that setSelection(int position)
is compatable with my code?
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> selectedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedItems = new ArrayList<String>();
ListView chl = (ListView) findViewById(R.id.cityselector);
chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] items = {"Berlin", "New York", "Paris", "Mexico City", "Asana", "Tangier"};
Adapter adapter=new Adapter(this,items);
chl.setAdapter(adapter);
chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int item_position=i;
}
});
}
Adapter.java
public class Adapter extends BaseAdapter {
String[] items;
LayoutInflater inflater;
ArrayList selectedItems;
Activity activity;
Adapter(Activity activity, String[] items){
this.items=items;
inflater=activity.getLayoutInflater();
selectedItems = new ArrayList<String>();
this.activity=activity;
}
@Override
public int getCount() {
return items.length;
}
@Override
public String getItem(int i) {
return items[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View v = view;
if(v == null){
v = inflater.inflate(R.layout.checkboxlayout,null);
}
TextView textview_Cities=(TextView)v.findViewById(R.id.Textview_Cities);
textview_Cities.setText(items[i]);
final CheckedTextView checkedTextView=(CheckedTextView)v.findViewById(R.id.checkbox);
checkedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!checkedTextView.isChecked()){
checkedTextView.setChecked(true);
selectedItems.add(items[i]);
//This is where I put the setSelection(int position) method////
}
else {
checkedTextView.setChecked(false);
selectedItems.remove(items[i]);
}
}
});
return v;
}
}
You can try out this
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
int finalPosition = isChecked ? 0 : list.size() - 1;
int currentPosition = getAdapterPosition();
moveItem(currentPosition, finalPosition);
}
});
void moveItem(int currentPosition, int finalPosition) {
if (currentPosition < finalPosition) {
for (int i = currentPosition; i < finalPosition; i++) {
Collections.swap(list, i, i + 1);
}
} else {
for (int i = currentPosition; i > finalPosition; i--) {
Collections.swap(list, i, i - 1);
}
}
notifyItemMoved(currentPosition, finalPosition);
}
here the OnCheckedChangeListener
is inside the ViewHolder
of the RecyclerView
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.