I want to remove item that has a copy in the ArrayList list.
for example:
List = { 54,55,55 } NewList = {54}
OR
List = { 54,55,55,55 } NewList = {54,55}
MY CODE:
public void submit(){
checked.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int j = 0;
int k = 1;
int l =0;
int size = list.size();
String temp = "";
//removing common
while (j < size) {
while(k < size){
if(list.get(j) == list.get(k)){
list.remove(k);
}
k++;
}
j++;
}
int newsize = list.size();
//printing
while (l < newsize){
temp = temp + " " + list.get(l);
l++;
}
MessageTo.message(ViewTasksActivity.this, temp);
}
});
}
My code only works on this condition for example:
list = {54,56,57,54}
and then clicked submit to clear off the list
list = {56,57} CORRECT
but with this conditions
list = {54,55,57,58,55}
and then clicked submit to clear off the list
list = {54,55,57,58,55} INCORRECT should remove 55
OR
list = {54,55,55,58,55}
and then clicked submit to clear off the list
list = {54,55,55,58,55} INCORRECT should remove 55 but remains only one 55 , should be {54,55,58}
-------Updates-------
FULL CODE
public class ViewTasksActivity extends AppCompatActivity {
private GridLayout gridLayout;
private Button b,checked;
private CheckBox cb;
ArrayList<Integer> list;
public int goal_id,actid;
int rowIndex = 1;
int colIndex = 0;
int rowIndex2 = 1;
int colIndex2 = 1;
int rowIndex3 = 1;
int colIndex3 = 2;
int i=0;
MyDBAdapter dbhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_tasks);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
//layout variables
gridLayout = (GridLayout) findViewById(R.id.taskListLayout);
checked = (Button) findViewById(R.id.checktasks);
goal_id = Integer.parseInt(extras.getString("goalid"));
actid = Integer.parseInt(extras.getString("activityId"));
list = new ArrayList<Integer>();
dbhandler = new MyDBAdapter(this);
set = new HashSet<>();
displayAllTasks();
submit();
}
public void displayAllTasks(){
//get a list of all tasks
List<Tasks> allTasks = dbhandler.getAllTasksbyActivity(goal_id,actid);
for (final Tasks task : allTasks){
//task name
TextView textView2 = new TextView(ViewTasksActivity.this);
GridLayout.LayoutParams param3 = new GridLayout.LayoutParams();
param3.rowSpec = GridLayout.spec(rowIndex);
param3.columnSpec = GridLayout.spec(colIndex);
textView2.setTextColor(Color.BLACK);
textView2.setLayoutParams(param3);
textView2.setText(task.getTaskName());
//status
TextView textView = new TextView(ViewTasksActivity.this);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.rowSpec = GridLayout.spec(rowIndex2);
param.columnSpec = GridLayout.spec(colIndex2);
textView.setTextColor(Color.BLACK);
textView.setLayoutParams(param);
if(task.getComplete().equalsIgnoreCase("False")){
cb = new CheckBox(ViewTasksActivity.this);
GridLayout.LayoutParams param4 = new GridLayout.LayoutParams();
param4.rowSpec = GridLayout.spec(rowIndex3);
param4.columnSpec = GridLayout.spec(colIndex3);
param4.setMargins(30, 5, 5, 5);
cb.setLayoutParams(param4);
cb.setId(task.getTaskId());
cb.setClickable(Boolean.TRUE);
textView.setText(" Incomplete");
cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
list.add(task.getTaskId());
i++;
}
});
}
else
{
cb = new CheckBox(ViewTasksActivity.this);
GridLayout.LayoutParams param5 = new GridLayout.LayoutParams();
param5.rowSpec = GridLayout.spec(rowIndex3);
param5.columnSpec = GridLayout.spec(colIndex3);
param5.setMargins(30, 5, 5, 5);
cb.setLayoutParams(param5);
textView.setText(" Complete");
cb.setId(task.getTaskId());
cb.isChecked();
cb.setClickable(Boolean.FALSE);
}
gridLayout.addView(textView);
gridLayout.addView(textView2);
gridLayout.addView(cb);
rowIndex3++;
rowIndex2++;
rowIndex++;
}
}
public void submit(){
checked.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int j = 0;
int k = 1;
int l =0;
int size = list.size();
String temp = "";
//removing common
while (j < size) {
while(k < size){
if(list.get(j) == list.get(k)){
list.remove(k);
}
k++;
}
j++;
}
int newsize = list.size();
//printing
while (l < newsize){
temp = temp + " " + list.get(l);
l++;
}
MessageTo.message(ViewTasksActivity.this, temp);
}
});
}
public void goBack(){
Intent myIntent = new Intent(ViewTasksActivity.this, ViewActActivity.class);
myIntent.putExtra("goalid", Integer.toString(goal_id));
startActivity(myIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_view_tasks, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_back) {
goBack();
return true;
}
return super.onOptionsItemSelected(item);
}
}