Android - 单击按钮时将项目添加到自定义列表视图

发布于 2024-12-10 12:26:08 字数 1710 浏览 0 评论 0原文

我目前有一个自定义列表视图,其中列表中的每个项目都包含两行文本。我想做的是每次用户单击该按钮时,它都会使用用户输入的文本在列表上创建一个新项目。虽然我知道如何获取文本,但我在向列表视图添加新项目时遇到了麻烦,因为我根本不知道从哪里开始。

这是我的代码:

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;
        }
    }
}

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法少女 2024-12-17 12:26:08

就像在 ArrayList 中添加新元素一样简单(就像在 fillData 中所做的那样),然后调用 adapter.notifyDataSetChanged()

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

塔塔猫 2024-12-17 12:26:08

调用 fillData() 后,只需调用 adapter.notifyDataSetChanged() 即可。

NotifyDataSetChanged() - 通知附加视图基础数据已更改,它应该自行刷新。

After calling fillData(), just make a call on adapter.notifyDataSetChanged().

NotifyDataSetChanged() - Notifies the attached View that the underlying data has been changed and it should refresh itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文