Hello !
I'm developing a "ToDoList" and I'm reaching some problems. I want to add some "features" like : delete, edit and add a description to each task. So, my code is like this :
And here are my XML files :
activity_task
activity_main
activity_form
I tried for 2 days to add the "deleteItems" method and the "editItems" method, but I can't do it. Someone has a solution ?
I'm developing a "ToDoList" and I'm reaching some problems. I want to add some "features" like : delete, edit and add a description to each task. So, my code is like this :
Java:
public class MainActivity extends AppCompatActivity {
private ArrayAdapter<String> itemsAdapter;
private ArrayList<String> items;
private ImageButton formButton;
private ListView lvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
commonFunction();
}
public void commonFunction() {
lvMain = (ListView) findViewById(R.id.lvMain);
items = new ArrayList<String>();
readItems();
itemsAdapter = new ArrayAdapter<String>(this,
R.layout.activity_task,
R.id.taskTitle,
items);
lvMain.setAdapter(itemsAdapter);
formButton = (ImageButton) findViewById(R.id.btnPlus);
formButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setLayoutActivity();
}
});
}
public void setLayoutActivity() {
setContentView(R.layout.activity_form);
ImageButton buttonChecked;
ImageButton buttonUnchecked;
buttonChecked = (ImageButton) findViewById(R.id.btnChecked);
buttonUnchecked = (ImageButton)findViewById(R.id.btnUnchecked);
buttonChecked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View w) {
addItems();
}
});
buttonUnchecked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View w) {
setContentView(R.layout.activity_main);
commonFunction();
}
});
}
public void addItems() {
EditText textTop = (EditText) findViewById(R.id.textTop);
String itemText = textTop.getText().toString();
itemsAdapter.add(itemText);
textTop.setText("");
writeItems();
setContentView(R.layout.activity_main);
readItems();
commonFunction();
}
private void readItems() {
File actDir = getFilesDir();
File save = new File(actDir, "ToDoList.txt");
try {
items = new ArrayList<>(FileUtils.readLines(save));
} catch (IOException e) {
items = new ArrayList<>();
}
}
private void writeItems() {
File actDir = getFilesDir();
File save = new File(actDir, "ToDoList.txt");
try {
FileUtils.writeLines(save, items);
} catch (IOException e) {
e.printStackTrace();
}
}
}
And here are my XML files :
activity_task
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/taskTitle"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="4dp"
android:layout_marginLeft="10dp"
android:text="Task"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/taskNews"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_below="@+id/taskTitle"
android:text="News"
android:textSize="20sp" />
<ImageButton
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/btnMinus"
android:background="#00000000"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:onClick="deleteItems"
app:srcCompat="@mipmap/ic_minus_foreground"/>
<ImageButton
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/btnEdit"
android:background="#00000000"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp"
android:onClick="editItems"
app:srcCompat="@mipmap/ic_edit_foreground"/>
</RelativeLayout>
activity_main
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.personal.todolist.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lvMain"
android:layout_above="@+id/btnPlus" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="@+id/btnPlus"
android:background="#00000000"
android:layout_alignParentBottom="true"
app:srcCompat="@mipmap/ic_plus_foreground" />
</RelativeLayout>
activity_form
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textTop"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="Task's title"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textBottom"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Task's news"
android:textAlignment="center"
android:inputType="textMultiLine" />
<ImageButton
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/btnUnchecked"
android:background="#00000000"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
app:srcCompat="@mipmap/ic_unchecked_foreground" />
<ImageButton
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/btnChecked"
android:background="#00000000"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
app:srcCompat="@mipmap/ic_checked_foreground" />
</RelativeLayout>
I tried for 2 days to add the "deleteItems" method and the "editItems" method, but I can't do it. Someone has a solution ?