Android的 - 自定义列表视图添加项目上按一下按钮 [英] Android - Add item to custom listview on button click

查看:182
本文介绍了Android的 - 自定义列表视图添加项目上按一下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个自定义的列表视图,其中列表中的每个项目包含文本的两行。我希望做的是每个用户点击该按钮时,它与用户输入的文本列表中创建一个新的项目。虽然我知道如何获取文本,我无法添加新项列表视图,因为我根本不知道从哪里开始。

下面是我的code:

 公共类MainFeedActivity扩展ListActivity实现OnClickListener {    查看sendButton;
    的EditText postTextField;
    TextView的currentTimeTextView,mainPostTextView;
    ListView的feedListView;
    的String []测试;
    ArrayList的<&HashMap的LT;字符串,字符串>>清单;     @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main_feed);            //创建按钮和点击监听器
            sendButton = this.findViewById(R.id.sendPostButton);
            sendButton.setOnClickListener(本);
            名单=新的ArrayList<&HashMap的LT;字符串,字符串>>();            //创建列表视图适配器
            SimpleAdapter适配器=新SimpleAdapter(
                    这个,
                    列表中,
                    R.layout.post_layout,
                    新的String [] {时间,后},
                    新的INT [] {R.id.postTimeTextView,R.id.postTextView});            //填充一些列表视图 - 测试
            fillData();            //设置列表适配器
            setListAdapter(适配器);     }     公共无效fillData(){
         //长timeTest = System.currentTimeMillis的();         HashMap的<字符串,字符串>临时=新的HashMap<字符串,字符串>();
         temp.put(时间,当前时间);
         temp.put(后,用户文本GOES HERE);
         list.add(临时);     }    @覆盖
    公共无效的onClick(视图按钮){
        开关(button.getId()){
        案例R.id.sendPostButton:            打破;
        }    }}


解决方案

这是因为加入了新的元素,在的ArrayList (就像你在做<$ C $一样简单C> fillData ),然后调用 adapter.notifyDataSetChanged()

I currently have a custom listview where each item on the list contains two rows of text. What I would like to do is each time a user clicks on the button, it creates a new item on the list with the text that the user inputted. While I know how to get the text, I am having trouble adding a new item to the list view as I simply don't know where to begin.

Here is my code:

public class MainFeedActivity extends ListActivity implements OnClickListener{

    View sendButton;
    EditText postTextField;
    TextView currentTimeTextView, mainPostTextView;
    ListView feedListView;
    String [] test;
    ArrayList<HashMap<String,String>> list;



     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_feed);

            //create button and implement on click listener
            sendButton = this.findViewById(R.id.sendPostButton);
            sendButton.setOnClickListener(this);


            list = new ArrayList<HashMap<String,String>>();

            //create the adapter for the list view
            SimpleAdapter adapter = new SimpleAdapter(
                    this,
                    list,
                    R.layout.post_layout,
                    new String[]{"time", "post"},
                    new int[]{R.id.postTimeTextView, R.id.postTextView});

            //fill the list view with something - TEST
            fillData();

            //set list adapter 
            setListAdapter(adapter);



     }

     public void fillData(){
         //long timeTest = System.currentTimeMillis();

         HashMap<String,String> temp = new HashMap<String,String>();
         temp.put("time", "current time");
         temp.put("post", "USER TEXT GOES HERE");
         list.add(temp);

     }

    @Override
    public void onClick(View button) {
        switch(button.getId()){
        case R.id.sendPostButton:



            break;
        }

    }

}

解决方案

It's as simple as adding a new element to your ArrayList (like you do in fillData), then calling adapter.notifyDataSetChanged().

这篇关于Android的 - 自定义列表视图添加项目上按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆