I am not able to display previous data displayed in ListView when using notifyDataSetChanged()
method of adapter. Used String array (even tried ArrayList<String>
) for storing data.
I have tried almost all answers relevant to this question but couldn't get the correct result. Any help will be appreciated. In my code I am trying to modify the String array.
MainActivity
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
String countryArray[];
Button add;
CustomAdapter adapter;
ListView list,newList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countryArray = new String[3];
countryArray[0] = "India";
countryArray[1] = "USA";
countryArray[2] = "England";
adapter = new CustomAdapter(this,android.R.layout.simple_list_item_1,countryArray);
list = (ListView) findViewById(R.id.lvCheckList);
newList = list;
list.setAdapter(adapter);
add = (Button) findViewById(R.id.bAdd);
add.setOnClickListener(this);
}
@Override
public void onClick(View v) {
countryArray[2] = "belgium";
adapter.notifyDataSetChanged();
}
}
CustomAdapter
public class CustomAdapter extends ArrayAdapter<String> {
Context context;
String[] values;
View rowView;
public CustomAdapter(Context context, int val, String[] objects) {
super(context,val,objects);
this.context = context;
this.values = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.test, parent, false);
}
TextView tx = (TextView) rowView.findViewById(R.id.tv1);
ImageView iv = (ImageView) rowView.findViewById(R.id.iv1);
tx.setText(values[position]);
iv.setImageResource(R.drawable.Image1);
return rowView;
}
}
Please check this line in your code.
rowView = convertView;
And when you put notifydatasetchanged()
method, rows will update and refresh. So put this method in getView()
after fetch data.
Please check this link .
You can override the onResume method which belongs to Activity. You need to add this method to your activity.
@Override
public void onResume()
{
super.onResume();
adapter = new CustomAdapter(this,android.R.layout.simple_list_item_1,countryArray);
list.setAdapter(adapter);
}
Change your getView() method of custom adapter like this..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
rowView = convertView; //you are missing this line
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.test, parent, false);
}
TextView tx = (TextView) rowView.findViewById(R.id.tv1);
ImageView iv = (ImageView) rowView.findViewById(R.id.iv1);
tx.setText(values[position]);
iv.setImageResource(R.drawable.Image1);
return rowView;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.