更改listview的ArrayList是否适当使用静态?

时间:2016-03-12 21:59:20

标签: java android listview

我正在制作两个活动,一个主页面包含任务的自定义列表视图,在该主页面上,您可以单击并单击“+”并添加新任务。它会将您重定向到addTask活动,该活动是一个带有调用submit_task_to_list的按钮的表单。

MainActivity

static KittyAdapter mAdapter; // Could be listview adapter
static ArrayList<Tasks> tasks = new ArrayList<Tasks>();

单击按钮时的AddTaskActivity

public void submit_task_to_list(View v) {
    EditText title = (EditText) findViewById(R.id.Title);
    EditText desc = (EditText) findViewById(R.id.Description);
    Tasks newTask = new Tasks(title.toString(), desc.toString());
    TaskCentral.tasks.add(newTask);
    TaskCentral.mAdapter.notifyDataSetChanged();
}

有没有比使用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);
}

}

我的适配器

公共类KittyAdapter扩展了ArrayAdapter {

public KittyAdapter(Context context, Tasks[] items) {
    super(context, R.layout.custom_task_layout, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater myinf = LayoutInflater.from(getContext());
    View customView = myinf.inflate(R.layout.custom_task_layout, parent, false);

    Tasks singleTask = getItem(position);
    CheckBox done = (CheckBox) customView.findViewById(R.id.Done);
    TextView myItemText = (TextView) customView.findViewById(R.id.ItemText);

    myItemText.setText(singleTask.getTitle());

    return customView;
}

如何从第二个活动中添加item和notifydatasetchanged?

2 个答案:

答案 0 :(得分:3)

这就是我要做的事情:

在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?):

Intent intent=new Intent();
intent.putExtra("title", title.toString());
intent.putExtra("desc", desc.toString());
setResult(RESULT_OK, intent);
finish();

答案 1 :(得分:0)

根据您的项目,静态方法或变量的使用可能是好的或坏的解决方案。您必须知道它们是什么以及如何使用它们,然后您将知道何时使用它们。静态方法和变量属于类并且首先创建,实例方法和变量只能通过使用类的实例来引用 - 换句话说:它们属于对象。