如何将自定义警报对话框中的项目添加到列表视图?

no_profile

所以我已经使用 Android 的适配器和适配器完成了这项工作,并且它工作得很好。自定义警报对话框有一个编辑文本,当用户单击确定时,该名称将添加到列表视图中。

但是现在,我正在使用模型和自定义适配器。这该怎么做?

这是我的代码:

主活动.java

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    EditText textsearch;
    ListView lv;
    AlertDialog.Builder builder;
    ArrayList<Names> namesArrayList = new ArrayList<>(); //arraylist for names
    ArrayList<Names> findnames = new ArrayList<>(); //array list for search filter
    Adapter adapter;


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

        textsearch = (EditText) findViewById(R.id.textsearch);

        lv = (ListView) findViewById(R.id.listview);
        adapter = new Adapter(this, namesArrayList);
        lv.setAdapter(adapter);

        //
        builder = new AlertDialog.Builder(this);
        lv.setOnItemClickListener(this);

        this.textsearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //using regular expression

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                findnames.clear();
//
//                //using regular expression
//                String s1 = s.toString();
//                Pattern pattern = Pattern.compile(s1);
//
//                for (int i=0; i<list.size(); i++){
//                    Matcher matcher = pattern.matcher(list.get(i));
//                    if(matcher.find()){
//                        findnames.add(list.get(i));
//                    }//end if
//                }
//                //update the adapter
//                findadapter.notifyDataSetChanged();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.addmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        switch (id){
            case R.id.action_add:
                showDialog(); //calling the showDialog() method
                return true;

            default:
                 return super.onOptionsItemSelected(item);
        }
    }

    public void showDialog(){
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
        dialogBuilder.setView(dialogView);

        final EditText textName = (EditText) dialogView.findViewById(R.id.editName);

        dialogBuilder.setTitle("New Item");
        dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if(!textName.getText().toString().equals("")){
// code here to add item
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getApplicationContext(), "Item added!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Fields can not be empty!", Toast.LENGTH_SHORT).show();
                }

            }
        });

        dialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog build = dialogBuilder.create();
        build.show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        this.namesArrayList.remove(position);
        this.adapter.notifyDataSetChanged();
        Toast.makeText(getApplicationContext(), "Name deleted!", Toast.LENGTH_LONG).show();
    }
}

这是我的模型。名称.java

public class Names {
    String name;

    public Names(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

适配器.java

public class Adapter extends BaseAdapter {

    Context context;
    //data container
    ArrayList<Names> list;
    LayoutInflater inflater;

    //constructor


    public Adapter(Context context, ArrayList<Names> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.custom_layout, parent, false);
            holder.name = (TextView) convertView.findViewById(R.id.txtname);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //inflate
        holder.name.setText(list.get(position).getName());
        return convertView;
    }

    static class ViewHolder{
        TextView name;
    }
}
a_local_nobody

尝试这样的事情:

if(!textName.getText().toString().equals("")) {
Names name = new Names(textName.getText().toString());
adapter.list.add(name);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Item added!", Toast.LENGTH_SHORT).show();        
} else {
Toast.makeText(getApplicationContext(), "Fields can not be empty!", Toast.LENGTH_SHORT).show();
}

本质上,我所做的只是创建一个Names模型的新实例并将其添加到适配器中的项目列表中

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何关闭“自定义警报”对话框

Android中的“自定义警报”对话框

如何将自定义消息退出对话框添加到wix安装程序

jQuery-将自定义验证规则添加到对话框中动态生成的下拉列表中

如何在Android中使用网格视图创建自定义警报对话框?

如何自定义警报对话框,以使按钮适合警报对话框

Android-如何在自定义警报对话框中检查按钮单击?

如何在自定义警报对话框中刷新内容/ UI

将自定义视图添加到警报视图

如何将自定义textField添加到警报中?

具有多个视图的自定义警报对话框

自定义警报对话框搜索项目结果错误

使 Multiautocompletetextview 在警报对话框中添加按钮上方可滚动,我想要这种类型的自定义警报对话框

使用Spring DI将自定义对话框添加到Javafx

如何将自定义类添加到emmet.io列表包装中的项目?

如何制作自定义警报对话框全屏

在服务android中显示自定义警报对话框

在自定义警报对话框中获取单选按钮的值

在自定义警报对话框中从 Edittext 检索数据时显示空值?

从Android中的ListView适配器启动自定义警报对话框

Flutter:将警报对话框中的文本添加到列表视图

如何将自定义html块添加到页脚选项列表中?

如何将自定义Google搜索添加到我的自定义搜索框中?

将自定义名称,标题,图像,描述添加到新的Facebook共享对话框或不从og meta中获取的自定义故事

如何将自定义文件添加到自定义文章列表页面

如何将自定义类添加到 TinyMCE 中的自定义按钮?

如何将自定义xtype添加到另一个视图?

如何将自定义视图添加到Django管理界面?

如何将自定义视图组添加到Anko DSL?