Pemadaman senarai senarai Android tidak dimuat semula secara automatik
PHP中文网
PHP中文网 2017-05-18 10:51:47
0
1
881

ListView tidak dimuat semula secara automatik apabila elemen dipadamkan, notifyDataSetChanged() adalah tidak sah

CityActivity

package com.nero.neroyang.wheather;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.nero.neroyang.wheather.Listener.MyOnClickListener;
import com.nero.neroyang.wheather.adapter.CityListAdapter;

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

/**
 * Created by neroyang on 2017/5/17.
 */

public class CityActivity extends Activity implements AdapterView.OnItemClickListener {

    private ListView listView;

    private List<String> cities;

    private List<Map<String,Object>> litems;

    private CityListAdapter cityListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_list);

        initView();
    }

    @NonNull
    @Override
    public LayoutInflater getLayoutInflater() {
        return super.getLayoutInflater();
    }

    private void initView() {

        listView = (ListView) this.findViewById(R.id.city_list);


        cities  = new ArrayList<String>();
        for(int i = 0;i<100;i++) {
            cities.add("威海"+i);
        }
        cityListAdapter = new CityListAdapter(cities,getApplicationContext(),getClickListener);
        listView.setAdapter(cityListAdapter);
        listView.setOnItemClickListener(this);



    }

    private MyOnClickListener getClickListener = new MyOnClickListener() {
        @Override
        public void myOnClick(int position, View v) {
            cities.remove(position);
            cityListAdapter.setData(cities);
            cityListAdapter.notifyDataSetChanged();
        }
    };



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}

Penyesuai

package com.nero.neroyang.wheather.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.nero.neroyang.wheather.CityActivity;
import com.nero.neroyang.wheather.Listener.MyOnClickListener;
import com.nero.neroyang.wheather.MainActivity;
import com.nero.neroyang.wheather.R;

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

/**
 * Created by neroyang on 2017/5/17.
 */

public class CityListAdapter extends BaseAdapter {


    private List<String> cityList;
    private Context context;
    private LayoutInflater inflater;

    private MyOnClickListener mylistener;

    public CityListAdapter(List<String> citylist,Context context,MyOnClickListener listener) {
        this.cityList  = citylist;
        this.context  =context;
        mylistener = listener;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<String> data){
        cityList = data;
    }
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {

        return cityList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {


        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.city_item, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.city_item);
            holder.imageView = (ImageView) convertView.findViewById(R.id.delete_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(cityList.get(position));
        holder.imageView.setOnClickListener(mylistener);
        holder.imageView.setTag(position);

        return convertView;

    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public int getViewTypeCount() {

        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }


    public class ViewHolder {
        public TextView textView;
        public ImageView imageView;
    }
}

OnclickListener

package com.nero.neroyang.wheather.Listener;

import android.view.View;

/**
 * Created by neroyang on 2017/5/17.
 */

public abstract class MyOnClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        myOnClick((Integer) v.getTag(), v);
    }

    public abstract void myOnClick(int position, View v);
}
PHP中文网
PHP中文网

认证高级PHP讲师

membalas semua(1)
巴扎黑

Letak ayat ini di luar

cityListAdapter.setData(cities);
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan