Я создаю простое приложение «сделать» для Android.
Что я хочу сделать, чтобы мое приложение сделало прокрутку listItem после того, как пользователь щелкнул по нему, и отменил прокрутку на еще один клик. Также этот же метод предполагает сохранение эффекта зачеркнутого (или нет) файла, чтобы при перезапуске приложения обновлялся статус каждого элемента.
Зачеркнутый метод написан здесь:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
TextView textView;
textView = view.findViewById(R.id.task);
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
saveInformations();
}
});
Здесь записан метод saveInformations:
private void saveInformations() {
try {
File file = new File(this.getFilesDir(), "filesToDo");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
for (int i = 0; i < toDoList.size(); i++) {
bufferedWriter.write(toDoList.get(i));
bufferedWriter.newLine();
}
bufferedWriter.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Связан ли зачеркнутый флаг рисования каким-либо образом с XML файл? Я ищу ответы, но не могу найти ничего подходящего. Ниже я добавляю свой код MainAcctivity:
public class MainActivity extends AppCompatActivity {
//list storing our data
final List<String> toDoList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//elements from our layout
final ListView listView = findViewById(R.id.lvList);
final Button button = findViewById(R.id.btnAdd);
final TextAdapter textAdapter = new TextAdapter();
//read informations saved from file
readInformations();
//
//setting data into textAdapter
textAdapter.setData(toDoList);
//
listView.setAdapter(textAdapter);
//method to delete the item from list
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Delete this task?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toDoList.remove(position);
textAdapter.setData(toDoList);
saveInformations();
}
})
.setNegativeButton("Nope", null)
.create();
dialog.show();
return true;
}
});
//method to strike thru already existing task
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
TextView textView;
textView = view.findViewById(R.id.task);
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
saveInformations();
}
});
//method adding new todoitem to the list
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText newTaskInput = new EditText(MainActivity.this);
newTaskInput.setSingleLine();
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("New Task")
.setView(newTaskInput)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toDoList.add(newTaskInput.getText().toString());
textAdapter.setData(toDoList);
saveInformations();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
}
//method to save informations from todolist
private void saveInformations() {
try {
File file = new File(this.getFilesDir(), "filesToDo");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
for (int i = 0; i < toDoList.size(); i++) {
bufferedWriter.write(toDoList.get(i));
bufferedWriter.newLine();
}
bufferedWriter.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//method to read informations from todolist
private void readInformations() {
File file = new File(this.getFilesDir(), "filesToDo");
if (!file.exists()) {
return;
}
try {
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = bufferedReader.readLine();
while (line != null) {
toDoList.add(line);
line = bufferedReader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//class responsible for putting elements in list
class TextAdapter extends BaseAdapter {
List<String> list = new ArrayList<>();
void setData(List<String> newList) {
list.clear();
list.addAll(newList);
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.todoitem, parent, false);
}
final TextView textView = convertView.findViewById(R.id.task);
textView.setText(list.get(position));
return convertView;
}
}
}