0

In my application,there is a listview which contains some data.There is an edittext and a button in the same activity.When i type something in the edittext,i want to add that text as new item into ListView on a button click.How to do that?Please help...

Here is a sample of my code...

This is my customList adapter

public class MainActivity extends Activity 
{
    ListView list;

    String[] web = {
    "Google Plus",
        "Twitter",
        "Windows",
        "Bing",
        "Itunes",
        "Wordpress",
        "Drupal"
} ;

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

       EditText et = (EditText)findViewById(R.id.editText1);
    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View arg0) {
                  Toast.makeText(MainActivity.this, "Button clicked...", Toast.LENGTH_SHORT).show();
         }
    });

    CustomList adapter = new CustomList(MainActivity.this, web);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener()  {
       @Override
       public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
          Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

         }
     });

This is my adapter class

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final String[] web;

public CustomList(Activity context,  String[] web) {
    super(context, R.layout.list_single, web);
    this.context = context;
    this.web = web;

}

@Override
public View getView(int position, View view, ViewGroup parent) {
   LayoutInflater inflater = context.getLayoutInflater();
   View rowView= inflater.inflate(R.layout.list_single, null, true);
   TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


   txtTitle.setText(web[position]);


  return rowView;
}
}
Nevaeh
  • 1,519
  • 7
  • 24
  • 45
  • 2
    get text from editText on Button click. add it to the array and call notifyDataSetChanged on the adapter. You could use a ArrayList also – Raghunandan Mar 26 '14 at 11:13

4 Answers4

4

Declare

final ArrayList<String> dataset = new ArrayList<String>();
Button b=(Button)findViewById(R.id.button1);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
final CustomList adapter = new CustomList(MainActivity.this, dataset, imageId);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) 
{
   dataset.add(et.getText().toString());
   adapter.notifyDataSetChanged();
}
});

Then

private  ArrayList<String>  web;
public CustomList(Activity context, ArrayList<String> web, Integer[] imageId) {
super(context, R.layout.list_single, web);

And in getView

txtTitle.setText(web.get(position));

Edit:

public class MainActivity extends Activity 
{
EditText ed;
CustomList adapter;
ArrayList<String> list = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) this.findViewById(R.id.button1);
    ListView lv = (ListView) this.findViewById(R.id.listView1);
    ed= (EditText) this.findViewById(R.id.editText1);
    adapter = new CustomList(this,list);
    lv.setAdapter(adapter);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            list.add(ed.getText().toString());
            adapter.notifyDataSetChanged();
            }

    });

}
}

activity_main.xml;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button1"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1" >
    </ListView>

</RelativeLayout>

CustomList

public class CustomList extends ArrayAdapter<String>{

private  ArrayList<String >web;
LayoutInflater mInflater;
public CustomList(Context context, ArrayList<String> web) {
    super(context, 0, web);
    mInflater= LayoutInflater.from(context);
    this.web = web;

}
@Override
public View getView(int position, View view, ViewGroup parent) {

   View rowView= mInflater.inflate(R.layout.list_item,parent,false);
   TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
   txtTitle.setText(web.get(position));
  return rowView;
}
}

list_item

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="TextView" />

</RelativeLayout>

Snap

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Add item in your arraylist/list/Array, and then notify your list by using:

adapterObject.notifyDataSetChanged()
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
0

You can try this

ArrayList<String> web=new ArrayList<String>();
   b.setOnClickListener(new View.onClickListener(){
       @Override
       public void onClick(View arg0) 
       {
             web.add(et.getText().toString());
             adapter.notifyDataSetChanged();
       }
    });
suresh kumar
  • 193
  • 1
  • 10
0

After updating the web and ImageId. You have to call the notifyDataSetChanged on your adapter.

Try this code with changes:

EditText et = (EditText)findViewById(R.id.editText1);


            Button b=(Button)findViewById(R.id.button1);
     CustomList adapter = new CustomList(MainActivity.this, web, imageId);
            b.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View arg0) 
                 {
    //update the data on the web and imageID
                  Toast.makeText(MainActivity.this, "Button clicked...", 
    Toast.LENGTH_SHORT).show();
    //call the notifydatasetChanged on the adapter
    adapter.notifyDataSetChanged();
                  }
               });


        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        {

             @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

                    }
                });