0

I wanna add items to my list but it only shows the first one:

public class MainActivity extends Activity {

    Server server;
    TextView infoip, msg;
    TextView usersTitle;
    String[] array = {"a"};
    ArrayList<String> lst;
    ArrayAdapter<String> adapter;
    ListView userList;

    @Override
    public void onCreate(Bundle savedInstanceState) {        
        lst = new ArrayList<String>(Arrays.asList(array));
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst);
        userList = (ListView) findViewById(R.id.userList);
        userList.setAdapter(adapter);

From this other class method, everytime it is called I want the text to go below the first one. The method certainly runs but it does not put the text below the previous one. It just shows "a"! Anyone knows why?

activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                        activity.lst.add(message);
                        activity.adapter.notifyDataSetChanged();                           

                    }
                });

I have also tried:

adapter.insert(String, int);

lst.add(int, String);

And even added in the onCreate method this:

lst.add(1, "2");
adapter.notifyDataSetChanged();

And still doesnt add the "2"!!

Lambros
  • 151
  • 2
  • 14

5 Answers5

1

If you are adding items to Arraylist from another class ,you have to declare your Arraylist Static.So that it can hold items in memory.

Replace ArrayList lst with public static ArrayList

Nishat Jahan
  • 399
  • 1
  • 2
  • 12
1

Here is the solution to your Problem.I have created an Activity class and Tests java class.

public class MainActivity extends Activity {
String[] array = {"a"};
public static ArrayList<String> lst;
ArrayAdapter<String> adapter;
ListView userList;
Tests tests = new Tests();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userList = (ListView) findViewById(R.id.userList);
    lst = new ArrayList<String>(Arrays.asList(array));
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst);

    userList.setAdapter(adapter);
    tests.callThread();
}

}

Here is the Tests.java Class public class Tests {

int i = 0;
String message = "";
Thread runOnUiThread;

public void callThread()

{


    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                while (i < 10) {
                    i = i + 1;
                    message = String.valueOf(i);
                    //Create a server socket object and bind it to a port
                    MainActivity.lst.add(message);
                }


                }catch(Exception e){
                    e.printStackTrace();
                }
        }
    }).start();
}

}

Just call your service inside this thread where I have incremented variable i and by this way you can populate the list in right order.

Nishat Jahan
  • 399
  • 1
  • 2
  • 12
0

Can you tell whether the other class is Activity or Fragment ? And while adding the data into Arraylist, you don't need the Thread to be run in order to insert new data to Arraylist

gautam90
  • 58
  • 7
  • It's a java class and i need the thread cause it's a server connection method. I just want the users connected to be in a list. – Lambros Oct 08 '16 at 12:32
  • So when you are calling the other class, you must pass the reference i.e. context of MainActivity class into your calling class. – gautam90 Oct 08 '16 at 12:37
  • Because then only you can call or use the members of that MainActivity class. Because in Android accessing other class members is totally different as comparison to core java programming. – gautam90 Oct 08 '16 at 12:41
0

Try to make "lst" and "adapter" both static.

Harv
  • 446
  • 5
  • 12
0

I'm suspicious about the runOnUiThread. Can you provide more information why did you use this function? Also i highly recommend using RecyclerView

Also you can refer to this post for adding items to RecyclerView

Community
  • 1
  • 1