Reputation: 237
I know this has been asked so many times on here, but absolutely nothing seems to be working.
I have a ListView, where it is being loaded, and then on a button click, the list is being changed in a file, and then I need to reload the new data. Currently, it is just adding the file contents onto the end of the old list, not creating a new one. I have no idea what I am doing wrong, I've tried boxAdapter.notifyDataSetChanged();
where boxAdapter is linking to my ListAdapter.
I have a method, which i am calling to load the listview
public void setUpPage(){
fillData();
boxAdapter = new ListAdapter(this, events);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
//boxAdapter.notifyDataSetChanged();
}
Then in my public void removeClick(View v)
I am calling setUpPage();
I have tried putting boxAdapter,notifyDataSetChanged();
at the end of my button method, but that's not helping either. Any help would be appreciated, maybe I should be putting the notifydatasetchanged somewhere else?
edit: full listadapter:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Event> objects;
ListAdapter(Context context, ArrayList<Event> events) {
ctx = context;
objects = events;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void updateList(){
notifyDataSetChanged();
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_item, parent, false);
}
Event p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((TextView) view.findViewById(R.id.description)).setText(p.description);
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
CheckBox selectCB = (CheckBox) view.findViewById(R.id.selectBox);
selectCB.setButtonDrawable(R.drawable.unchecked);
selectCB.setOnCheckedChangeListener(myCheckChangList);
selectCB.setTag(position);
selectCB.setChecked(p.box);
return view;
}
Event getProduct(int position) {
return ((Event) getItem(position));
}
ArrayList<Event> getBox() {
ArrayList<Event> box = new ArrayList<Event>();
for (Event p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
if(buttonView.isChecked()) {
buttonView.setButtonDrawable(R.drawable.checked);
}
else{
buttonView.setButtonDrawable(R.drawable.unchecked);
}
}
};
}
filling the data:
void fillData() {
String ret = "";
try {
InputStream inputStream = openFileInput("UserEvents.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
String[] separated = receiveString.split("~");
String event = separated[0]; // this will contain "name"
String eventloc = separated[1]; // this will contain the rest
try {
JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
if (mainNode != null) //puts the values into an array
{
for (int i = 0; i < mainNode.length(); i++) {
JSONObject eachObject = mainNode.getJSONObject(i);
String location = eachObject.getString("eventLocation");
String eventtime = eachObject.getString("eventTime");
String loctime = location + " " + eventtime;
if(loctime.equals(eventloc))
{
String eventdesc = eachObject.getString("eventDescription");
int img = R.drawable.ic_launcher; //from here use event name as to which image to show
events.add(new Event(event, eventloc, eventdesc, img, false));
}
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("h", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("h", "Can not read file: " + e.toString());
}
}
Upvotes: 0
Views: 388
Reputation: 4480
I would suggest you create a public static
updateList
method on the ListAdapter
like:
public void updateList(){
yourAdapter.notifyDataSetChanged();
}
and call this method whenever you want to update your list
Upvotes: 0
Reputation: 1971
Hello what i would suggested in your point. Do not initialize below code every time when you call setUpPage().
boxAdapter = new ListAdapter(this, events);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
Just initialize once in on Create method of activity or fragment. then when you have raw data available. In your case you read the data from "UserEvents.txt". once you collect all data just add the array list you created. and see the blow code.
void fillData() {
String ret = "";
try {
InputStream inputStream = openFileInput("UserEvents.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null)
{
stringBuilder.append(receiveString);
String[] separated = receiveString.split("~");
String event = separated[0]; // this will contain "name"
String eventloc = separated[1]; // this will contain the rest
try {
JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
if (mainNode != null) //puts the values into an array
{
for (int i = 0; i < mainNode.length(); i++) {
JSONObject eachObject = mainNode.getJSONObject(i);
String location = eachObject.getString("eventLocation");
String eventtime = eachObject.getString("eventTime");
String loctime = location + " " + eventtime;
if(loctime.equals(eventloc))
{
String eventdesc = eachObject.getString("eventDescription");
int img = R.drawable.ic_launcher; //from here use event name as to which image to show
events.add(new Event(event, eventloc, eventdesc, img, false));
}
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("h", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("h", "Can not read file: " + e.toString());
}
boxAdapter.notifyDataSetChanged();
}
Also can you tell us how your base adapter use the array list or content list. it would better if you show the adapter.
Thank you
Upvotes: 0
Reputation: 1261
Try this..
runOnUiThread(new Runnable() {
@Override
public void run() {
boxAdapter,notifyDataSetChanged();
}
});
Upvotes: 1