0

I am having a problem trying to pass a StringBuilder into an ArrayAdapter so that it will display in a ListView. I'm getting a cannot resolve constructor error when compiling and I'm sure it has something to do with the StringBuilder. Java is not my first language so any explanation with the answers is a greatly appreciated.

Here is my code

public class MainActivity extends Activity {

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

        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = activityManager.getRunningAppProcesses();


        StringBuilder info = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            info.append(list.get(i).processName + "\n");
        }
        ArrayAdapter<StringBuilder> adapter = new ArrayAdapter<StringBuilder>(this,android.R.layout.simple_list_item_1, info);
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);

   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_main, menu);

       return true;
   }
}
3
  • In which line do you get the error? Commented Jul 29, 2015 at 14:56
  • what would you expect your adapter to do with a stringbuilder? Commented Jul 29, 2015 at 15:08
  • @Jan it is on line 31. Commented Jul 29, 2015 at 15:24

1 Answer 1

3

there is not ArrayAdapter's constructor that takes a StringBuilder as parameter. Use an ArrayList

ArrayList<String> info = new ArrayList<>();
for (ActivityManager.RunningAppProcessInfo p : list) {
   info.add(p.processName);
}

Should do it.

Edit:

you should also be using

ArrayAdapter<String> instead of ArrayAdapter<StringBuilder>

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

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.