如何通过单击每个按钮将项目添加到ListView [英] How to add an item to a ListView with each Button click

查看:98
本文介绍了如何通过单击每个按钮将项目添加到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在处理的代码,每次单击按钮时,都应该在ListView(带有数组)中简单地添加一个项目.每次单击按钮时,应将用户在EditText上插入的文本添加到列表中.

This is the code I'm working on, it should simply add an item in a ListView (w/ array) each time the Button is clicked. The text inserted by the user on a EditText should be added to the list each time the Button is clicked.

我发现这行代码:

new Button.OnClickListener()

被视为匿名"事物,并且没有运行,请帮帮我;(

is seen as an 'anonymous' thing, and it's not run, please help me out ;(

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.lang.*;
import java.util.List;

public class MainActivity extends ActionBarActivity {
//Setup Lists
String[] arrayNames;
ListView listNames;
TextView namesText;

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

    //Creating and Printing Lists
    listNames = (ListView) findViewById(R.id.listNamesId);
    namesText = (TextView) findViewById(R.id.namesTexter);
    final ArrayAdapter<String> adapterNames = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, (List<String>) namesText);

    Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
    buttonPlus.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    listNames.setAdapter(adapterNames);
                }
            }
    );
}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<ListView
    android:id="@+id/listNamesId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#000"
    android:dividerHeight="1dp"
    android:headerDividersEnabled="true"
    android:layout_marginTop="50dp"></ListView>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/textnametextname"
    android:id="@+id/buttonPlus"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:nestedScrollingEnabled="false" />

<EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/namesTexter"
    android:layout_alignBottom="@+id/buttonPlus"
    android:layout_centerHorizontal="true"
    android:inputType="text" />

推荐答案

问题不在于您的按钮单击侦听器,原因在于您没有使用列表作为适配器的数据源.

The problem is not with your button click listener, it's that you're not using a List as the data source of your Adapter.

我刚刚使用ArrayList作为数据源并在按钮单击侦听器中对其进行了更新.每次单击该按钮时,EditText中的所有内容都会添加到ListView中.

I just got this working using an ArrayList as the data source, and updating it in the button click listener. Each time you click the button, whatever is in the EditText gets added to the ListView.

public class MainActivity extends ActionBarActivity {

    ArrayList<String> arrayNames = new ArrayList<String>();
    ListView listNames;
    TextView namesText;


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


        //Creating and Printing Lists
        listNames = (ListView) findViewById(R.id.listNamesId);
        namesText = (TextView) findViewById(R.id.namesTexter);
        final ArrayAdapter<String> adapterNames = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNames);
        listNames.setAdapter(adapterNames);

        Button buttonPlus = (Button)findViewById(R.id.buttonPlus);

        buttonPlus.setOnClickListener(
                new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        arrayNames.add(0, namesText.getText().toString());
                        adapterNames.notifyDataSetChanged();
                        namesText.setText("");

                    }
                }
        );


    }

这篇关于如何通过单击每个按钮将项目添加到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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