Java 正在更改列表视图';s ArrayList是否正确使用了static?
我正在做两个活动,一个主页面带有任务的customlistview,在主页面上你可以点击“+”并添加一个新任务。它会将您重定向到addTask活动,这是一个带有按钮的表单,该按钮可调用submit_task_to_list 主要活动Java 正在更改列表视图';s ArrayList是否正确使用了static?,java,android,listview,Java,Android,Listview,我正在做两个活动,一个主页面带有任务的customlistview,在主页面上你可以点击“+”并添加一个新任务。它会将您重定向到addTask活动,这是一个带有按钮的表单,该按钮可调用submit_task_to_list 主要活动 static KittyAdapter mAdapter; // Could be listview adapter static ArrayList<Tasks> tasks = new ArrayList<Tasks>(); 有没有比使
static KittyAdapter mAdapter; // Could be listview adapter
static ArrayList<Tasks> tasks = new ArrayList<Tasks>();
有没有比使用static关键字更好的解决方案?我以前过度使用过静态,当我的另一个应用程序变得更加复杂时,处理它是痛苦的。我想尽量减少此应用程序上的坏习惯
我尝试将onClickListener移动到mainactivity,但android找不到它
完整代码:
主要活动
public class TaskCentral extends Activity {
static KittyAdapter mAdapter; // Could be listview adapter
static ArrayList<Tasks> tasks = new ArrayList<Tasks>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_central);
String[] tString = {"Hi", "My", "name", "is", "Kitty"};
Tasks T1 = new Tasks("m", "p");
Tasks T2 = new Tasks("m", "p");
Tasks T3 = new Tasks("m", "p");
Tasks T4 = new Tasks("m", "p");
tasks.add(T1);
tasks.add(T2);
tasks.add(T3);
Tasks[] TaskArr = new Tasks[tasks.size()];
TaskArr = tasks.toArray(TaskArr);
ListView taskCentral = (ListView)findViewById(R.id.Central);
mAdapter = new KittyAdapter(this, TaskArr);
taskCentral.setAdapter(mAdapter);
// mAdapter.notifyDataSetChanged();
}
@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_task_central, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addNew(View v) {
Intent addTaskIntent = new Intent(this, AddTaskActivity.class);
startActivity(addTaskIntent);
}
如何在第二个活动中添加item和notifydatasetchanged?根据您的项目,使用静态方法或变量可能是一个好的解决方案,也可能是一个坏的解决方案。你必须知道它们是什么以及如何使用它们,然后你才能知道何时使用它们。静态方法和变量属于类,并且是首先创建的,实例方法和变量只能通过使用类的实例来引用-换句话说:它们属于对象。这就是我要做的: 在TaskCentral:
final static int REQUEST_CODE_ADD_TASK = 324234;
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_ADD_TASK:
String title = data.getStringExtra("title");
String desc = data.getStringExtra("desc");
Tasks newTask = new Tasks(title, desc);
tasks.add(newTask);
mAdapter.notifyDataSetChanged();
}
}
}
并启动AddTaskActivity,如:
public void addNew(View v) {
Intent addTaskIntent = new Intent(this, AddTaskActivity.class);
startActivityForResult(addTaskIntent, REQUEST_CODE_ADD_TASK);
}
在AddTaskActivity中,您在何处完成(onClickListener?):
这是一个非常笼统的回答。它并没有解决这个问题,如果你明白这一点,那么你就会知道什么时候可以使用或不使用。这里的情况似乎与糟糕的编码有关,因为缺乏这方面的知识。我可能也错了,但为了得到更好的答案,我需要查看事件侦听器的代码,并知道每个方法的位置。很明显,他或多或少知道你在回答中解释的那些事情。他在寻找一个“最佳实践”@goncalopinto是的,这是我的错,我应该更清楚地说,我熟悉静态的概念,只是我经常跳到它上面。似乎应该有一种方法使arrayadapter只在我的类中可访问,但似乎有很多麻烦,因为这是一件非常基本的事情。我会更新更多信息谢谢,这很有帮助!有一件事,有没有一种方法也可以添加到ArrayList
任务中?它以前是TaskCentral.tasks.add(newTask)代码>(就在旧的notifyDataSetChangedsure上方,现在看我的答案哦,我真傻。非常感谢你的帮助!我一定会用这个
final static int REQUEST_CODE_ADD_TASK = 324234;
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_ADD_TASK:
String title = data.getStringExtra("title");
String desc = data.getStringExtra("desc");
Tasks newTask = new Tasks(title, desc);
tasks.add(newTask);
mAdapter.notifyDataSetChanged();
}
}
}
public void addNew(View v) {
Intent addTaskIntent = new Intent(this, AddTaskActivity.class);
startActivityForResult(addTaskIntent, REQUEST_CODE_ADD_TASK);
}
Intent intent=new Intent();
intent.putExtra("title", title.toString());
intent.putExtra("desc", desc.toString());
setResult(RESULT_OK, intent);
finish();