0

I want to make a dynamic list view which gets the user credentials when I login for the first time and displays it in a list the next time I start the app. I know how to send the username from one intent to another. i haven't focused on the SQLite part yet, will do that later. I'm facing problems in creating the dynamic list view. Found one very useful thread - Dynamically add elements to a listView Android

he used a button on the screen and called the method onClick to populate the list. Can i do it without the button? I want it to automatically happen once i am able to login. how can i use the statements in my code?

listItems.add(value);
adapter.notifyDataSetChanged();

here value is the username i am getting from some other intent.
please help. thanks!

Community
  • 1
  • 1
Umang
  • 583
  • 2
  • 12
  • 28
  • 1
    Does this answer your question? [How to add elements to a listView dynamically in Android](https://stackoverflow.com/questions/4540754/how-to-add-elements-to-a-listview-dynamically-in-android) – Josh Correia Aug 21 '20 at 21:47

3 Answers3

2

For this Just use the example given below: For Instance you are Adding Some Strings into your List

So Create a ListArray like this

ArrayList<String> listItems = new ArrayList<String>();

now whenever you want to add certain string into list just do this thing

  EditText editText = (EditText) findViewById(R.id.edit);
  listItems.add("my string");  OR
  listItems.add(editText.getText.toString()); //incase if you are getting string value from editText and adding it into the list

Use this Xml inside your linear layout in main.xml

  <EditText android:id="@+id/edit"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Now when you have added one item dynamically then call this

  adapter.notifyDataSetChanged();

The above will update your list and display the upadted list.

For more info about this see the following links:

http://www.androidpeople.com/android-custom-listview-tutorial-part-1
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
http://www.androidpeople.com/android-custom-dynamic-listview-%E2%80%93part3

In these tutorials you can replace String[] with ArrayList as given at the top of the answer ook and when you want to add any item just simply use the second code snippet.

Thanks
sHaH

Shah
  • 4,990
  • 10
  • 48
  • 70
  • it says getText cannot be resolved or is not a field. Why so? – Umang Aug 04 '11 at 09:22
  • well editText is you android EditText object which we can say is a TextBar to get input from user. I just gave that example if you want to get input text from edittext field in android and add it to the list. see my edited answer – Shah Aug 04 '11 at 09:25
  • I have stored the string which to be displayed in the variable 'value'. If I do listItems.add(value) then it gives an error. – Umang Aug 04 '11 at 09:31
  • Please can you follow the Tutorial MEntioned in the Answer in First Link.. This is the answer for you... Just replace String[] with ArrayList – Shah Aug 04 '11 at 09:36
  • but when i did listItems.add("hello"); then it displayed hello on the screen. I'll follow the tutorials. I just want to know if listItems.add() can have a string variable's name in it? – Umang Aug 04 '11 at 09:36
  • yes they can have Use String abc = "hello"; and listItems.add(abc.toString()); – Shah Aug 04 '11 at 09:42
  • M Glad i Helped... Can u mark this question as Correct and Also Vote UP :) – Shah Aug 04 '11 at 09:53
1

The best way to do this will be to use ArrayAdapter. When modifying the adapter it automatically refresh itself so you don't have to call notifyDataSetChanged.

Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • the code is using ArrayAdapter. adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems); setListAdapter(adapter); – Umang Aug 04 '11 at 09:03
  • So what is your problem then just add items to the adapter and keep in mind that you have to add the elements in the UI thread e.g all methods that modify the adapter should be in the UI thread. – Mojo Risin Aug 04 '11 at 09:42
0

You can try out this code to add elements dynamically to list view. You can do it with out button click also.

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    //step2 : create all the variables.
    EditText et;
    Button b;
    ListView lv;
    ArrayList<string> al;
    ArrayAdapter<string> aa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //step3 : intitalize all the variables.
        et = (EditText) findViewById(R.id.editText1);
        b = (Button) findViewById(R.id.button1);
        lv = (ListView) findViewById(R.id.listView1);
        al = new ArrayList<string>();//initialize array list
        aa = new ArrayAdapter<string>(this, 
                android.R.layout.simple_list_item_1, 
                al);//step4 : establish communication bw arraylist and adapter
        //step5 : establish communication bw adapter and dest (listview)
        lv.setAdapter(aa);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, 
                    View v, int arg2,
                    long arg3) {
                String item = al.get(arg2);
                Toast.makeText(getApplicationContext(), item, 0).show();
            }
        });
        //step6 : button click logic
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //step i: take text from et and add to arraylist
                String item = et.getText().toString();
                al.add(0, item);
                //step ii: notify to adapter
                aa.notifyDataSetChanged();
                //step iii: clr edit text
                et.setText("");
            }
        });
    }
}

For complete code check this list view example

user1923551
  • 4,684
  • 35
  • 29