CustomAdapter上的notifyDataSetChanged无法正常工作

时间:2015-10-14 11:49:07

标签: android listview

我有一个用于添加项目的Dialog。 Dialog(Addproject.java)是在另一个活动(Projects_list.java)上调用的活动。 在Projects_list.java中。我有一个使用customadapter(Custom_projectList_adapter.java)的listview。

我在对话框中添加任何项目后,我想刷新listview的内容。 我调用了notifyDataSetChanged但它没有工作。

这是我的代码:

Projects_list.java

public class Projects_list extends ActionBarActivity {
    public String[] itemname ={"Safari","Camera","Global","FireFox","UC Browser","Android Folder", "VLC Player","Cold War"};
    Custom_projectList_adapter adapter;
    ListView list;
    final ArrayList<String> projectlist = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.projects_list);

        for (int i = 0; i < itemname.length; ++i) {
            projectlist.add(itemname[i]);
        }

        CircleButton addproject = (CircleButton) findViewById(R.id.addproject);
        adapter = new Custom_projectList_adapter(this,projectlist);
        list = (ListView) findViewById(R.id.projectlistView);
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        list.setAdapter(adapter);

        addproject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent addproject = new Intent(Projects_list.this, AddProject.class);
                startActivity(addproject);
            }
        });
    }

    public void additem(String item) {
        projectlist.add(item);
        adapter.notifyDataSetChanged();
    }
}

这是我的AddProject.java

public class AddProject extends Activity {
    EditText newProjectname;
    String project_name;
    Button add_project;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_project);

        newProjectname = (EditText) findViewById(R.id.add_project_name);
        add_project = (Button) findViewById(R.id.btn_add_project);

        add_project.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String add = newProjectname.getText().toString().trim();
                Projects_list pl = new Projects_list();
                pl.additem(add);
            }
        });
    }

}

这是我的Custom_projectList_adapter.java

public class Custom_projectList_adapter extends ArrayAdapter<String>{

    private final Activity context;
//    private final String[] projectName;
    ArrayList<String> projectName;
    int selectedoption = 0;
    public Custom_projectList_adapter(Activity context, ArrayList<String> projectName) {
        super(context, R.layout.custom_project_list,projectName);
        this.context = context;
        this.projectName = projectName;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.custom_project_list, null, true);

        RadioButton radio_project = (RadioButton) rowView.findViewById(R.id.radio_project);
        radio_project.setText(projectName.get(position));
        radio_project.setChecked(position == selectedoption);
        radio_project.setTag(position);
        radio_project.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedoption = (Integer) view.getTag();
                notifyDataSetChanged();
                Toast.makeText(context, projectName.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        return rowView;
    }

}

因为我是初学者。我很困惑。请帮帮我。

1 个答案:

答案 0 :(得分:1)

add_project.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String add = newProjectname.getText().toString().trim();
            Projects_list pl = new Projects_list();
            pl.additem(add);
        }
    });

在这里,您实例化一个新的Projects_list,因此pl.additem(add);将不会更新适配器的数据。并且不建议手动实例化Activity

我建议您使用Dialog而不是启动另一个Activity来添加项目。