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;
}
}
1
  • 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 Commented Mar 26, 2014 at 11:13

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

17 Comments

@kalyanpvs actually a model class would be better. gonna edit it
and maybe you could also correct op's getView in order to avoid the double memory allocation.
@Raghunandan yes need to be change getView also to make it perfect.
no viewholder in this case. Remove the inflater and call the super View rowView = super.getView(positiom, .... )
@Nevaeh sorry my computer is giving problems had to restart
|
0

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

adapterObject.notifyDataSetChanged()

1 Comment

How to add the item into my list ?Can u show me a sample code ?
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();

                    }
                });

1 Comment

Thats too on onclick event of the button, but before calling the notifyDAtaSetChanged method. @Nevaeh
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();
       }
    });

Comments

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.