我正在尝试为学校作业安排撰写应用程序。主页面是用户在本学期学习的课程列表。用户可以根据自己的意愿添加任意数量的主题。当点击某个项目(课程)时,弹出菜单会显示与此课程相关的选项,如“截止日期”,“教师”,“网站链接”,“联系人”等。
我创建了一个Mainactivity类和另一个名为“Deadlines”的类,它处理“deadlines”选项。只要选择此选项,Mainactivity就会配置为指向此“deadlines”类。
然而,问题是我在截止日期列表中添加新项目后,单击返回主页面并返回,项目将消失。但这不会发生在主页上。
我想也许我可以创建Deadlines类的实例数组,并在主页上添加新项时添加新成员。但是,我不知道如何实现这一点。
我将在下面提供我的源代码: 这是我的MainActivity类:
package p.listviewexercise3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import static p.listviewexercise3.R.id.deadlines;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
ArrayList list;
ArrayAdapter<String> arrayAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText.getText().toString();
if (name.isEmpty()){
Toast.makeText(getBaseContext(),"lol",Toast.LENGTH_SHORT).show();
}
else{
list.add(name);
arrayAdapter.notifyDataSetChanged();
}
}
});
list = new ArrayList();
list.add("x");
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long id) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, listView);
popupMenu.getMenuInflater().inflate(R.menu.menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item){
switch(item.getItemId()){
case deadlines:
Intent intent = new Intent(getApplicationContext(), Deadlines.class);
intent.putExtra("ITEM_INDEX", position);
startActivity(intent);
}
return true;
}
});
popupMenu.show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
list.remove(position);
arrayAdapter.notifyDataSetChanged();
return true;
}
});
}
}
这是我的截止日期课程:
package p.listviewexercise3;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by root on 9/28/17.
*/
public class Deadlines extends AppCompatActivity {
Button button;
EditText editText;
ListView deadlines;
ArrayList list;
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deadlines);
Intent in = getIntent();
int index = in.getIntExtra("p.ITEM_INDEX", -1);
button = (Button) findViewById(R.id.button2);
editText = (EditText) findViewById(R.id.editText2);
deadlines = (ListView) findViewById(R.id.deadlines);
list = new ArrayList();
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
deadlines.setAdapter(arrayAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = editText.getText().toString();
list.add(text);
arrayAdapter.notifyDataSetChanged();
}
});
}
}