android listview 删除之后怎么刷新 listview

这个是TitleAdapter

package com.example.ming.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class TitleAdapter extends BaseAdapter {
Context context;
List postlist2;
String id2;
String id="";
int ii=0;

public TitleAdapter(Context context, List<Article> postlist) {

    this.context = context;
    this.postlist2 = postlist;

}

@Override
public int getCount() {
    return postlist2.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(context).inflate(R.layout.activity_article, null);
    Object obj = convertView.getTag();

    ii=position;

    ViewHolder holder;//实现缓存 高效加载数据
    //如果为空
    if (obj == null) {
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        //緩存 通過viewholder存儲
        holder = (ViewHolder) obj;
    }
    Article posit = postlist2.get(position);
    holder.tv_tltleid.setText(posit.getWid() + "");
    holder.tv_tltlecontent.setText(posit.getTitle() + "");

    return convertView;
}

public class ViewHolder  {
    public View lv_title;
    public TextView tv_tltleid;
    public TextView tv_tltlecontent;
    public Button btn_neirong;
    public Button btn_delete;
    public ViewHolder(View rootView) {
        this.lv_title = rootView;
        this.tv_tltleid = (TextView) rootView.findViewById(R.id.tv_tltleid);
        this.tv_tltlecontent = (TextView) rootView.findViewById(R.id.tv_tltlecontent);

        this.btn_neirong=(Button)rootView.findViewById(R.id.btn_neirong);
        btn_neirong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent2 = new Intent(context, ContentActivity.class);
                String id2=tv_tltleid.getText().toString();
                Log.i("aaa",id2);
                intent2.putExtra("wid",id2);
                context.startActivity(intent2);
            }
        });


        this.btn_delete = (Button) rootView.findViewById(R.id.btn_delete);
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String id = tv_tltleid.getText().toString();
                Log.i("1111", id);
                Log.i("11112", id);
                String url = "http://192.168.43.124:8080/test1/LoginServlet?jsp=delete&wid=" + id;
                new LoginTask().execute(url);
            }
        });

    }

    class LoginTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            String par = params[0];
            URL url = null;
            try {
                url = new URL(par);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            String result = HttpUtil.doPost(url);
            return result;
        }

        //第四步
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            //保存数据

            if (result.equals("1")) {

                Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();

                return;
            } else {
                if (result.equals("-1")) {
                    Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    }



}

}

这个是TitleActivity

package com.example.ming.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**

  • Created by Ming on 2019/6/16. */

public class TitleActivity extends AppCompatActivity {

private ListView listView;

Article article;
JSONArray jsonArray;
ListView lv_title;
Context context;
Button button;
String id2="used";

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

    Intent intent=getIntent();

    id2=intent.getStringExtra("id2");
    context = this;
    String url = "http://192.168.43.124:8080/test1/LoginServlet?jsp=titleck&id2="+id2;
    new LoginTask().execute(url);

}



class LoginTask extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... params) {
        String par = params[0];
        URL url = null;
        try {
            url = new URL(par);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        String result = HttpUtil.doPost(url);

        return result;
    }

    //第四步
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);


        final List<Article> list = new ArrayList<Article>();

        Gson gson1 = new Gson();
        final List<Article> list2 = gson1.fromJson(result, new TypeToken<List<Article>>() {
        }.getType());
        for (Article person1 : list) {
            System.out.println(person1.toString());
        }


        lv_title=(ListView)findViewById(R.id.lv_title);

        lv_title.setAdapter(new TitleAdapter(context,list2));


    }

}

}
1.

3个回答

notifyDataSetChanged();

as_csdn
取个什么名字好。 回复weixin_45274972: 参考这篇文章吧:https://www.jianshu.com/p/50515c3160d3
大约一年之前 回复
as_csdn
取个什么名字好。 回复weixin_45274972: 一般来说,通常的做法是在对应的adapter中写一个删除方法,类似这样的:public void deletItem(int pos) { DebugLog.e(pos + "删除的position"); mData.remove(pos); this.notifyDataSetChanged(); },执行删除操作时调用,其中的mData就是数据列表,参数是要删除的数据的position
大约一年之前 回复
weixin_45274972
weixin_45274972 this.btn_delete = (Button) rootView.findViewById(R.id.btn_delete); btn_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id = tv_tltleid.getText().toString(); Log.i("1111", id); //192.168.43.124 String url = "http://220.202.158.368080/test1/LoginServlet?jsp=delete&wid=" + id; new LoginTask().execute(url); notifyDataSetChanged(); } }); 是这样写吗?但是他还是没有刷新数据
大约一年之前 回复
weixin_45274972
weixin_45274972 回复流浪_过客: this.btn_delete = (Button) rootView.findViewById(R.id.btn_delete); btn_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id = tv_tltleid.getText().toString(); Log.i("1111", id); Log.i("11112", id); String url = "http://192.168.43.1248080/test1/LoginServlet?jsp=delete&wid=" + id; new LoginTask().execute(url); } });
大约一年之前 回复
as_csdn
取个什么名字好。 回复weixin_45274972: 在你执行完删除操作后,调用这个方法就行了,这是adapter自带的
大约一年之前 回复
weixin_45274972
weixin_45274972 请问是怎么写呢?能详细一点吗?我不太懂安卓不好意思。
大约一年之前 回复

建议使用recyclerview. listview也可以实现删除. 但是不太好用.

qq_32508519
Jimmy_buer 回复weixin_45274972: 当然可以. listview完全支持删除刷新.只是早都不用这个了.
大约一年之前 回复
fjd19921225
彩虹直至黑白iii丶 建议使用recyclerview. listview也可以实现删除. 但是不太好用.
大约一年之前 回复
weixin_45274972
weixin_45274972 那我这个删除就不能刷新了吗?
大约一年之前 回复

先用postlist2 .remove(position) 在调用notifyDataSetChanged();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问