Android列表视图打开另一个列表视图

时间:2015-05-22 19:18:52

标签: android listview android-listview

我目前已经实现了一个列表视图,当您单击某个项目时会打开第二个活动。在第二个活动中,我有另一个列表视图,我可以添加项目,但当我回到第一个列表并单击另一个项目时,第二个列表中的所有项目也会出现在这个项目中。

任何想法如何解决这个问题?可以根据需要发布代码

以下是第一项活动的代码:

public class MainActivity extends Activity {

private ArrayList<String> entries;
private ArrayAdapter<String> entriesAdapter;
private ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    createListView();
    createListViewListener();
}

private void createListView() {
    list = (ListView) findViewById(R.id.diaryListView);
    entries = new ArrayList<>();
    readEntries();
    entriesAdapter = new CustomAdapter(this, entries);
    list.setAdapter(entriesAdapter);
}

private void createListViewListener() {
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            entries.remove(position);
            entriesAdapter.notifyDataSetChanged();
            writeEntries();
            return true;
        }
    });
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("entry", entries.get(position));
            startActivity(intent);
        }
    });
}

public void addEntry(View v) {
    EditText entryEditText = (EditText) findViewById(R.id.entryEditText);
    String diaryText = entryEditText.getText().toString();
    entriesAdapter.add(diaryText);
    entryEditText.setText("");
    writeEntries();
}

private void readEntries() {
    File filesDir = getFilesDir();
    File journalEntriesFile = new File(filesDir, "journalEntries.txt");
    try {
        entries = new ArrayList<>(FileUtils.readLines(journalEntriesFile));
    } catch (IOException e) {
        entries = new ArrayList<>();
    }
}

/**
 * Method to save a list of tasks
 */
private void writeEntries() {
    File filesDir = getFilesDir();
    File journalEntriesFile = new File(filesDir, "journalEntries.txt");
    try {
        FileUtils.writeLines(journalEntriesFile, entries);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

和第二个:

public class SecondActivity extends Activity {

private String entryName;
private TextView entryTitle;
private ArrayList<String> entryTask;
private ArrayAdapter<String> entryTaskAdapter;
private ListView entryTaskList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    displayEntryTitle();
    createEntryTaskListView();
    createEntryTaskListViewListener();
}

private void displayEntryTitle() {
    entryTitle = (TextView) findViewById(R.id.entryTitle);
    Intent intent = getIntent();
    entryName = intent.getStringExtra("entry");
    entryTitle.setText("" + entryName);
}

private void createEntryTaskListView() {
    entryTaskList = (ListView) findViewById(R.id.entryTaskListView);
    entryTask = new ArrayList<>();
    readEntryTasks();
    entryTaskAdapter = new CustomAdapter2(this, entryTask);
    entryTaskList.setAdapter(entryTaskAdapter);
}

private void createEntryTaskListViewListener() {
    entryTaskList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            entryTask.remove(position);
            entryTaskAdapter.notifyDataSetChanged();
            writeEntryTasks();
            return true;
        }
    });
    entryTaskList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            View view2 = (LayoutInflater.from(SecondActivity.this)).inflate(R.layout.alert_dialog, null);

            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SecondActivity.this);
            alertBuilder.setTitle("Edit Journal Task Entry");
            alertBuilder.setView(view2);
            final EditText editEntryTaskText = (EditText) view2.findViewById(R.id.editEntryTask);

            alertBuilder.setCancelable(true).setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String editEntry = editEntryTaskText.getText().toString();
                    editEntryTaskText.setText("" + editEntry);
                }
            });
            Dialog dialog = alertBuilder.create();
            dialog.show();
        }
    });
}

public void onAddEntryTask(View v) {
    EditText editText = (EditText) findViewById(R.id.entryTaskEditText);
    String entryTaskText = editText.getText().toString();
    entryTaskAdapter.add(entryTaskText);
    editText.setText("");
    writeEntryTasks();
}

private void readEntryTasks() {
    File filesDir = getFilesDir();
    File taskEntriesFile = new File(filesDir, "taskEntries.txt");
    try {
        entryTask = new ArrayList<>(FileUtils.readLines(taskEntriesFile));
    } catch (IOException e) {
        entryTask = new ArrayList<>();
    }
}

private void writeEntryTasks() {
    File filesDir = getFilesDir();
    File taskEntriesFile = new File(filesDir, "taskEntries.txt");
    try {
        FileUtils.writeLines(taskEntriesFile, entryTask);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

如果没有看到你如何从第二个活动回到第一个活动,我只能假设当你返回时,ArrayList的入口任务被插入到ArrayList条目中,所以基于有限的信息,我得到的最好的答案是

private void createListView() {
list = (ListView) findViewById(R.id.diaryListView);

entries = new ArrayList<>();
list.setAdapter(null);
//ArrayList<String> entries = null;
readEntries();
entriesAdapter = new CustomAdapter(this, entries);
list.setAdapter(entriesAdapter);
}