ArrayAdapter更新列表 - 未按预期工作?

时间:2014-03-17 22:10:09

标签: android android-arrayadapter

好的,所以我有一个ArrayAdapter,我有这个显示的行,每行包括一些文本和一个删除按钮。删除按钮是我的问题...我有一个监听器听取点击,然后它调用remove(position)删除视图。 (这就是我想要的)...虽然当我在UI线程中调用onNotifyDataSetChanged()时,它会重新添加已删除的行,因为它没有从该列表中删除(只有视图)。 ..我不知道如何解决这个问题。

总结......

我可以使用删除按钮删除要删除的视图,但在调用onNotifyDataSetChanged()后,视图会重新添加。

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;

}

}

2 个答案:

答案 0 :(得分:0)

您似乎不需要notifyDataSetChanged,因为它包含活动列表的实例。这就是列表返回其原始状态的原因。通过调用remove,它正在更改列表。请参阅此类似主题以获得更好的解释:notifyDataSetChanged example

答案 1 :(得分:0)

here所示,ArrayAdapter.remove()不适用于普通java数组,如String [],而且你在MainActivity中也是如此:

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

请考虑使用getStringArrayList

您也不需要手动调用notifyDataSetChange(),您可以看到this