0

Ok, so I have an ArrayAdapter and I have this displaying rows, each row includes some text and a delete button. The delete button is my problem... I have a listener listen for click and then it calls remove(position) which removes the view. (which is what I want)... although when I call onNotifyDataSetChanged() in the UI thread it re-adds the deleted row, presumable because it's not been deleted from that list (only the view)... I'm unaware on how I can fix this.

So to summarize....

I can delete the view which I want to delete using the delete button but after onNotifyDataSetChanged() is called the view is re-added.

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    render = new tweetView(this);

    LinearLayout canvas = (LinearLayout)findViewById(R.id.content_frame);
    final ListView tweetList = (ListView)findViewById(R.id.tweetView);

    tweetListFull = new ArrayList<Tweet>();
    int resID = R.layout.tweet;
    aa = new tweetAdapter(this, resID, tweetListFull);

    Log.w("Check","Here1");        
    tweetList.setAdapter(aa);
    Log.w("Check","Here1");
    //tweetList.addView()
    canvas.addView(render);
    tweetList.setClickable(true);
    tweetList.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int i, long l) {

            try {
                //Nothing here yet.

            }
            catch(Exception e) {
               // System.out.println("Nothing here yet.);
            }
        }
    }); 

    // UI //        
    drawerListViewItems = getResources().getStringArray(R.array.items); 
    // get ListView defined in activity_main.xml
    drawerListView = (ListView) findViewById(R.id.left_drawer); 
    // Set the adapter for the list view
    drawerListView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_listview_item, drawerListViewItems));
LocationManager locationManager;
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    String provider = LocationManager.GPS_PROVIDER;

    int updateTime = 5000; // 5 seconds.
    int distance = 1; // meters 

    LocationListener myLocListener = new LocationListener(){
        @Override
        public void onLocationChanged(Location arg0) {
            Log.w("location",String.valueOf(arg0));
            twitterTest tweet = new twitterTest();
            Log.w("Test","Location Changed");
            Log.w("test","Long: "+String.valueOf(arg0.getLongitude())+ "Lat: "+String.valueOf(arg0.getLatitude()));
            tweet.execute(arg0.getLongitude(),arg0.getLatitude());  // The argument for this is the query to search for.

        }
        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }           
    };
    locationManager.requestLocationUpdates(provider, updateTime,distance, myLocListener);


}

tweetAdapter

package com.example.networkingcoursework;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class tweetAdapter extends ArrayAdapter<Tweet>{

int resource;
Tweet tweet = null;
Context context;
//ArrayList<Tweet> tweetListFull;

public tweetAdapter(Context _context, int _resource,ArrayList<Tweet> _objects) {
    super(_context, _resource, _objects);
    resource = _resource;
    //tweetListFull = _objects;
    context = _context;
    // TODO Auto-generated constructor stub
}


@Override
public View getView(final int position, View convertView, ViewGroup parent){
    LinearLayout tweetView;
    tweet = getItem(position);

    String tweetStatus = tweet.getStatus();

    if(convertView == null){
        tweetView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource,tweetView, true);
    }
    else{
        tweetView = (LinearLayout) convertView;     
    }

    TextView idView = (TextView)tweetView.findViewById(R.id.tweetText);
    idView.setText(String.valueOf(tweetStatus));
    Button tweetClose = (Button)tweetView.findViewById(R.id.tweetClose);
    tweetClose.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            int i = (Integer)v.getTag();

            remove(getItem(position));
            notifyDataSetChanged();
        }
    });
    tweetClose.setTag(position);


    return tweetView;

}

}

Leth0_
  • 544
  • 2
  • 6
  • 15
  • 1
    int i = (Integer)v.getTag(); remove(getItem(position)); Should it not be used the i value instead of the position? – Pedro Lopes Mar 17 '14 at 22:43
  • Just edited it to try and it didn't change anything unfortunately, the view is still deleted but the `onDataSetChanged()` on the MainActivity keeps on causing it to redraw after a locationChange. – Leth0_ Mar 17 '14 at 22:46

2 Answers2

0

It seems that you don't need notifyDataSetChanged because it holds an instance of the list from the activity. That is why the list is returning to its original state. By calling remove, it is changing the list. See this similar topic for a better explanation: notifyDataSetChanged example

Community
  • 1
  • 1
osprey_PW
  • 145
  • 6
  • Thanks for the thought, unfortunately after commenting out the `notifyDataSetChanged` the list is still the same. The way I test this is by checking the length of the list before I delete items and then again after I delete items. Before and after the list size stays as 15. Just going to read the thread you linked too. – Leth0_ Mar 17 '14 at 22:49
  • Found another possible solution. You are initializing your ArrayAdapter with a array of strings. It needs to be initialized with an arrayList. When ArrayAdapter is initialized with an array it cannot be modified. Change this line 'drawerListViewItems = getResources().getStringArray(R.array.items);'Check out this solution:[link](http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception) – osprey_PW Mar 18 '14 at 16:45
0

As seen here, ArrayAdapter.remove() would not work with plain java arrays such as String[], and you do in MainActivity:

drawerListViewItems = getResources().getStringArray(R.array.items); 

Consider to use getStringArrayList instead.

Also you don't need to manually call notifyDataSetChange(), you can see this

Community
  • 1
  • 1
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34