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;
}
}