如何在android studio中点击按钮添加行到listView?

时间:2016-12-16 18:08:17

标签: java android listview

我正在为我的ListView使用自定义适配器,我已经不了解我想要的了。我可以在线查找有关如何向ListView添加行的信息,但它并没有很好地集成到我的代码中,我猜测因为我使用的是自定义适配器。我的主要活动中有我的setOnClickListener()方法,并且已经在那里进行了实验,但是无法弄清楚我是否也不确定我的方法是否在正确的位置?

这是mainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import static java.util.logging.Logger.global;

public class MainActivity extends Activity {

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

        final String[] Chores = {""};
        ListAdapter MyAdapter = new CustomAdapter(this, Chores);
        ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
        listViewObject.setAdapter(MyAdapter);

        listViewObject.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                    String ChoreString = String.valueOf(parent.getItemAtPosition(position));


                }
            }

        );

 // this is where I am trying to have button click add another row to my listView    
         final Button button = (Button) findViewById(R.id.button_ID);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 //this is where I am stuck

            }

        });

    }
}

这是我的CustomAdapter类

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


class CustomAdapter extends ArrayAdapter{

    public CustomAdapter(Context context, String[] choreText) {
        super(context, R.layout.custon_listview_row, choreText);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater myInflater = LayoutInflater.from(getContext());
        View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);

        String singleListItem = (String) getItem(position);
        EditText choreText = (EditText) customView.findViewById(R.id.editText_ID);
        ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);

        choreText.setText(singleListItem, TextView.BufferType.EDITABLE);



        imageButton.setImageResource(R.drawable.clock);

        return customView;
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="com.example.emilythacker.chorelist.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/customListView_ID"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp" />

    <Button
        android:text="Add Chore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button_ID" />
</RelativeLayout>

和custom_listview_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="60dp">

    <ImageButton
        android:layout_width="60dp"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/clock"
        android:id="@+id/imageButton_ID"
        android:layout_height="60dp"
        android:background="@null"
        android:layout_alignParentRight="true"
        android:padding="5dp"
        android:layout_weight="1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/editText_ID"
        android:layout_alignParentLeft="true"
        android:alpha = ".5"
        android:hint="Enter chore"
        android:maxLines="1"
        android:inputType="text"
         />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

        public void onClick(View v) {

            String stringToAdd = ... // 
            MyAdapter.add(stringToAdd);
        }

您需要在适配器声明中添加final才能使其正常工作:

        final ListAdapter MyAdapter = new CustomAdapter(this, Chores);

修改

一次添加多行:

        public void onClick(View v) {

            String[] rowsToAdd = ... // 
            MyAdapter.addAll(rowsToAdd);
        }

答案 1 :(得分:1)

首先将列表放入ArrayList而不是String数组。

private ArrayList<String> arrayList;

点击按钮

arrayList.add("New Item");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();