Reputation: 23
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();
}
}
Upvotes: 0
Views: 364
Reputation: 321
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()
Upvotes: 1