Reputation: 1
I'm a newbie to android programming.Pls can anybody explain why I get 'adapter' unresolved in the emphasized part of my code.My programs parent actvity first adds some data to an arraylist which is converted to a string array and displayed using a listview. On the click of a button, it starts a secondary activity that returns a string which is added to the arraylist and the new data should be updated on the display.whenever I put the "adapter.notifyDataSetChanged();" I get the message "adapter" unresolved.Can anyone explain why or what I can do to resolve it.See code thank you.
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ShitActivity extends Activity {
ArrayList<String> arrlist=new ArrayList<String>();
private static final int REQUEST_CODE=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list=(ListView)findViewById(R.id.list);
arrlist.add("android");
arrlist.add("android");
arrlist.add("iphone");
arrlist.add("android");
arrlist.add("Blackberry");
arrlist.add("webos");
setButtonClickListener();
String[] arr=arrlist.toArray(new String[arrlist.size()]);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,R.layout.list_item,R.id.test1,arr);
list.setAdapter(adapter);
}
private void setButtonClickListener(){
Button Read1=(Button)findViewById(R.id.Read1);
Read1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent (ShitActivity.this,Deliverstring.class);
startActivityForResult(i,REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==REQUEST_CODE){
String in1=data.getStringExtra("datareturn");
arrlist.add(in1);
***adapter.notifyDataSetChanged();***
}
}}
Upvotes: 0
Views: 837
Reputation: 4268
You are declaring your adapter in onCreate. Put it outside of a function so it can be used elsewhere.
private ArrayAdapter<String> adapter;
in onCreate
adapter=new ArrayAdapter<String> (this,R.layout.list_item,R.id.test1,arr);
Upvotes: 1