0

i have an issue with ArrayAdapter and listView. I'm adding strings to list in ArrayAdapter which, as debugger showed me, works without a flaw, however i can't see anything that is added on my listView. Things i tried to do with it (based on similar questions): + add it inside runOnUiThread, but this is not helping or i am not using it correctly + invalidating view + setting adapter once more + adding to values and using `adapter.notifyDataSetChanged() and I have no idea what can i do to make it work

Here's my code if it helps:

public class RunnerApp extends Activity {

        private ListView listView;
    private ArrayList<String> values;
    private ArrayAdapter<String> adapter;
    private Intent newTable;
    private String newName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_runner_app);
        listView = (ListView) findViewById(R.id.mylist);
        values = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
       listView.setAdapter(adapter);
        adapter.add("x");
        adapter.notifyDataSetChanged();
    }

    //handled by android:onClick in layout file
    public void addTable(View v){
        newTable = new Intent(this, AddTable.class);
        startActivityForResult(newTable, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null){
            data.getData();
            Log.d("add", "got intent");
            newName = data.getStringExtra("com.example.runnerapp.NAME");
            Log.d("add", "string " + newName);
            Runnable run = new Runnable(){
                public void run(){
                    if(newName != null) {
                        values.add(newName);
                        adapter.notifyDataSetChanged();
                    }
                }
            };
            runOnUiThread(run);
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        setContentView(R.layout.activity_runner_app);
    }
}


public class AddTable extends Activity {

    public final static String EXTRA_NAME = "com.example.runnerapp.NAME";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_table);
    }

    //handled by android:onClick in layout file
    public void addThisTable(View v) {
        Intent addTable = new Intent();
        EditText editText = (EditText) findViewById(R.id.addTableField);
        String name = editText.getText().toString();
        addTable.putExtra(EXTRA_NAME, name);
        setResult(Activity.RESULT_OK, addTable);
        this.finish();
    }

}
5
  • It looks like there's a call to addTable possibly missing? Commented Jan 18, 2013 at 19:50
  • it's done by a button with android:onClick in layout, should I also post the layout file? Commented Jan 18, 2013 at 19:53
  • You should comment the code to reflect that, and actually could you post the AddTable class? Commented Jan 18, 2013 at 19:55
  • And you may want to try taking out the setContentView(...) from the onResume() just because it will totally reset your view from scratch. It should really only be needed in onCreate() Commented Jan 18, 2013 at 20:00
  • taking out setContentView() helped. I put it there earlier while having some other issues that caused app to crash on returning to the RunnerApp activity and didn't clean up Commented Jan 18, 2013 at 20:07

1 Answer 1

1

You should try taking out the setContentView(...) from the onResume() just because it will totally reset your view from scratch. It should really only be needed in onCreate()

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.