1

I am using a custom adapter for my ListView which I already don't understand as much as I would like to. I can find information online on how to add a row to ListView but it doesn't integrate nicely into my code, I'm guessing because I'm using a custom adapter. I have my setOnClickListener() method for my button in my Main Activity and have been experimenting there but just cant figure it out I'm also not sure if my method is in the right place?

this is the 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

            }

        });

    }
}

here is my CustomAdapter class

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>

and 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
  • Please make sure you don't publish names, passwords etc.! Commented Dec 16, 2016 at 18:23
  • sorry first post didn't know. Commented Dec 16, 2016 at 23:51

2 Answers 2

1

First make your list into an ArrayList instead of String array.

private ArrayList<String> arrayList;

In the buttons onClick

arrayList.add("New Item");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

Comments

1
        public void onClick(View v) {

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

You will need to add final to the adapter declaration for this to work:

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

EDIT

To add more than one row at a time:

        public void onClick(View v) {

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

5 Comments

Don't forget to call notifyDataSetChanged() after you modify the adapter's backing data.
@AlexandruDascălu The adapter is extending ArrayAdapter; the notifyDataSetChanged() call is baked into the add() method so we don't have to do it. Although you can modify that behavior with setNotifyOnChange(), the default is to call it. See line 192, github.com/android/platform_frameworks_base/blob/master/core/…
That moment when I get smashed by the source code. Good point :)
I am getting "Cannot resolve method 'add(java.lang.String[])" for the add() method. I have added "String[] stringToAdd = {""};" and set the adapter to final like you said.
Ahh. Your question asked "how to add a row" so I assumed a single item. For more than one item, see my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.