如何ListView控件刷新后删除Android上的按钮Click事件中的项目? [英] how to Listview Refresh after Delete an item on Button Click event in android?

查看:150
本文介绍了如何ListView控件刷新后删除Android上的按钮Click事件中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除列表视图中的项目,并在同一时间刷新列表视图删除一个项目之后。如何可能的?

我使用得到使用JSON解析从数据库中的所有项目上,单击按钮删除一个选择的项目。从数据库中删除成功,但在同一时间的ListView不会刷新。怎么办?

我使用JSON解析。没有本地数据库。

在此情况下,删除项目时如何刷新列表视图?
请指导我。
先谢谢了。

我的code是,
Detail.java文件

 公共类详细延伸活动{
    ListView控件lstDetail = NULL;
    / ** *字符串/
    字符串urlGetDetailData = NULL;    / **声明另一个变量列表视图* /
    适配器1适配器1 = NULL;
    ArrayList的<详情> myList中=新的ArrayList<详情>();
    / **的Hashmap为ListView的* /
    ArrayList的<&HashMap的LT;字符串,字符串>> DataList控件= NULL;    / ** JSON节点名称* /
    公共静态最后弦乐TAG_MEMBER_ID =mem_id;
    公共静态最后弦乐TAG_ID =ID;
    公共静态最后弦乐TAG_USER_ID =用户id;
    公共静态最后弦乐TAG_STATUS =状态;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        onCreateActivity(R.layout.detail);        initializeWidgets();
    }    私人无效initializeWidgets(){
        /** 列表显示 */
        lstDetail =(ListView控件)findViewById(R.id.lstDetail);        urlGetDetailData =htt​​p://example.com/getdata.php?id=
                + strId;        新GetDetailData()执行();
        myList.remove(位置);
        Adapter1.this.notifyDataSetChanged();    }    / **
     *异步任务类,通过HTTP调用获得JSON
     * * /
    私有类GetDetailData扩展的AsyncTask<太虚,太虚,太虚> {
        的JSONObject的JSONObject;
        JSONArray jsonarray;        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
        }        @覆盖
        保护无效doInBackground(虚空......为arg0){
            DataList控件=新的ArrayList<&HashMap的LT;字符串,字符串>>();
            //从给定的URL地址JSON对象
            的JSONObject = JSONFunctions.getJSONfromURL(urlGetDetailData);            尝试{
                //找到在JSON数组名
                jsonarray = jsonobject.getJSONArray(数据);                的for(int i = 0; I< jsonarray.length();我++){                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                    的JSONObject = jsonarray.getJSONObject(ⅰ);
                    // Retrive JSON对象                    map.put(mem_id,将String.valueOf(的JSONObject
                            .getString(TAG_MEMBER_ID)));
                    map.put(ID,
                            jsonobject.getString(TAG_ID));                    map.put(用户id,jsonobject.getString(TAG_USER_ID));
                    map.put(状态,jsonobject.getString(TAG_STATUS));                    dataList.add(地图);
                }
            }赶上(JSONException E){
                Log.e(错误,e.getMessage());
                e.printStackTrace();
            }            返回null;
        }        @覆盖
        保护无效onPostExecute(虚空结果){
            super.onPostExecute(结果);
            如果(dataList.size()!= 0){
                lstDetail.setVisibility(View.VISIBLE);
                适配器1 =新的适配器1(Detail.this,
                        DataList控件);
                lstDetail.setAdapter(适配器1);
            }其他{
                lstDetail.setVisibility(View.GONE);
            }
        }
    }
}

和适配器类是,
Adapter1.java文件

 公共类适配器1扩展BaseAdapter {
    公众的ArrayList<&HashMap的LT;字符串,字符串>> arrData = NULL;
    上下文的背景下= NULL;
    LayoutInflater layoutInflater = NULL;
    HashMap的<字符串,字符串> getDetailData =新的HashMap<字符串,字符串>();    / ** *字符串/
    字符串strMemberId = NULL,urlDelete = NULL;    / ** *构造函数/
    公共适配器1(上下文的背景下,
            ArrayList的<&HashMap的LT;字符串,字符串>> arrData){
        layoutInflater =(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context =背景;
        this.arrData = arrData;
    }    @覆盖
    公众诠释的getCount(){
        返回arrData.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回arrData.get(位置);
    }    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        ViewHolder viewHolder = NULL;
        如果(convertView == NULL){
            convertView = layoutInflater.inflate(
                    R.layout.list_item,NULL);            viewHolder =新ViewHolder();
            的getData = arrData.get(位置);            / **初始化窗口小部件* /            viewHolder.imgCancel =(ImageView的)convertView
                    .findViewById(R.id.imgCancel);            viewHolder.imgCancel
                    .setOnClickListener(新OnClickListener(){
                        @覆盖
                        公共无效的onClick(视图v){
                            strMemberId = arrData.get(
                                    位置)获得(
                                    Detail.TAG_MEMBER_ID);
                            urlDelete =htt​​p://example.com/delete.php?mem_id=
                                    + strMemberId;
                            新DeleteComments()执行();
                        }
                    });            / ** *的TextView /
            viewHolder.txtMemberId =(TextView中)convertView
                    .findViewById(R.id.txtMemberId);
            viewHolder.txtId =(TextView中)convertView
                    .findViewById(R.id.txtId);            viewHolder.txtDesc =(TextView中)convertView
                    .findViewById(R.id.txtDesc);            / **设定值* /
            viewHolder.txtMemberId.setText(getDetailData
                    获得(Detail.TAG_MEMBER_ID));
            viewHolder.txtId.setText(getDetailData
                    获得(Detail.TAG_ID));            viewHolder.txtDesc.setText(getDetailData
                    获得(Detail.TAG_STATUS));            convertView.setTag(viewHolder);        }其他{
            viewHolder =(ViewHolder)convertView.getTag();
        }
        返回convertView;
    }    / ** ViewHolder类* /
    @燮pressLint(NewApi)
    公共静态类ViewHolder {
        ImageView的imgCancel = NULL;
        TextView的txtMemberId = NULL,txtId = NULL,txtDesc = NULL;
    }    公共类DeleteComments扩展的AsyncTask<太虚,太虚,太虚> {
        在preExecute保护无效(){
            super.on preExecute();
        }        @覆盖
        保护无效doInBackground(虚空......为arg0){
            SH的ServiceHandler =新的ServiceHandler();
            字符串jsonStr = sh.makeServiceCall(urlDelete,
                    ServiceHandler.GET);
            Log.d(回应:删除加入注释,>中+ jsonStr);            返回null;
        }        保护无效onPostExecute(虚空结果){
            super.onPostExecute(结果);        };
    }
}

detail.xml文件是,

 <?XML版本=1.0编码=UTF-8&GT?;
    <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>        < ListView控件
                机器人:ID =@ + ID / lstDetail
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT>
            < /&的ListView GT;
    < / RelativeLayout的>list_item.xml文件是
< XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>    <的LinearLayout
        机器人:ID =@ + ID / contentLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_vertical
        机器人:方向=垂直>        <的TextView
            机器人:ID =@ + ID / txtId
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=111/>        <的TextView
            机器人:ID =@ + ID / txtDesc
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>
    < / LinearLayout中>    < ImageView的
        机器人:ID =@ + ID / imgCancel
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_centerVertical =真
        机器人:SRC =@绘制/取消/>    <的TextView
        机器人:ID =@ + ID / txtMemberId
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / txtUserEventId
        机器人:layout_alignBottom =@ + ID / txtUserEventId
        机器人:layout_alignParentLeft =真
        机器人:文字=222/>
< / RelativeLayout的>


解决方案
在自定义适配器呼叫

this.notifyDataSetChanged(); ,其中正在执行删除功能和删除从ArrayList的元件,其被设置为适配器

I want to delete an item from Listview, and at a time Refresh Listview after deleting an item. How to possible?

I am using get all item using JSON Parsing from database and delete an selected an item on click of button. delete successfully from database but Listview not refresh at a time. how to do?

I am using Json Parsing. not local database.

In This case, How to refresh Listview when Deleting Item? please Guide me. Thanks in Advance.

My Code is, Detail.java File

public class Detail extends Activity {
    ListView lstDetail = null;
    /** String */
    String urlGetDetailData = null;

    /** Declare another variable for Listview */
    Adapter1 adapter1 = null;
    ArrayList<Detail> myList = new ArrayList<Detail>();
    /** Hashmap for ListView */
    ArrayList<HashMap<String, String>> dataList = null;

    /** JSON Node names */
    public static final String TAG_MEMBER_ID = "mem_id";
    public static final String TAG_ID = "id";
    public static final String TAG_USER_ID = "userid";
    public static final String TAG_STATUS = "Status";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        onCreateActivity(R.layout.detail);

        initializeWidgets();
    }

    private void initializeWidgets() {
        /** ListView */
        lstDetail = (ListView) findViewById(R.id.lstDetail);

        urlGetDetailData = "http://example.com/getdata.php?id="
                + strId;

        new GetDetailData().execute();
        myList.remove(position);
        Adapter1.this.notifyDataSetChanged();

    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetDetailData extends AsyncTask<Void, Void, Void> {
        JSONObject jsonobject;
        JSONArray jsonarray;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            dataList = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONFunctions.getJSONfromURL(urlGetDetailData);

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("data");

                for (int i = 0; i < jsonarray.length(); i++) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects

                    map.put("mem_id", String.valueOf(jsonobject
                            .getString(TAG_MEMBER_ID)));
                    map.put("id",
                            jsonobject.getString(TAG_ID));

                    map.put("userid", jsonobject.getString(TAG_USER_ID));
                    map.put("Status", jsonobject.getString(TAG_STATUS));

                    dataList.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (dataList.size() != 0) {
                lstDetail.setVisibility(View.VISIBLE);
                Adapter1 = new Adapter1(Detail.this,
                        dataList);
                lstDetail.setAdapter(Adapter1);
            } else {
                lstDetail.setVisibility(View.GONE);
            }
        }
    }
}

And Adapter Class is, Adapter1.java File

public class Adapter1 extends BaseAdapter {
    public ArrayList<HashMap<String, String>> arrData = null;
    Context context = null;
    LayoutInflater layoutInflater = null;
    HashMap<String, String> getDetailData = new HashMap<String, String>();

    /** String */
    String strMemberId = null, urlDelete = null;

    /** Constructor */
    public Adapter1(Context context,
            ArrayList<HashMap<String, String>> arrData) {
        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.arrData  = arrData;
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(
                    R.layout.list_item, null);

            viewHolder = new ViewHolder();
            getData = arrData.get(position);

            /** Initialize Widgets */

            viewHolder.imgCancel = (ImageView) convertView
                    .findViewById(R.id.imgCancel);

            viewHolder.imgCancel
                    .setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            strMemberId = arrData.get(
                                    position).get(
                                    Detail.TAG_MEMBER_ID);
                            urlDelete = "http://example.com/delete.php?mem_id="
                                    + strMemberId;
                            new DeleteComments().execute();
                        }
                    });

            /** TextView */
            viewHolder.txtMemberId = (TextView) convertView
                    .findViewById(R.id.txtMemberId);
            viewHolder.txtId = (TextView) convertView
                    .findViewById(R.id.txtId);

            viewHolder.txtDesc = (TextView) convertView
                    .findViewById(R.id.txtDesc);

            /** Set Value */


            viewHolder.txtMemberId.setText(getDetailData 
                    .get(Detail.TAG_MEMBER_ID));
            viewHolder.txtId.setText(getDetailData
                    .get(Detail.TAG_ID));

            viewHolder.txtDesc.setText(getDetailData
                    .get(Detail.TAG_STATUS));

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    /** ViewHolder Class */
    @SuppressLint("NewApi")
    public static class ViewHolder {
        ImageView imgCancel = null;
        TextView txtMemberId = null, txtId = null,txtDesc = null;
    }

    public class DeleteComments extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            ServiceHandler sh = new ServiceHandler();
            String jsonStr = sh.makeServiceCall(urlDelete,
                    ServiceHandler.GET);
            Log.d("Response : delete join comments", ">" + jsonStr);

            return null;
        }

        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

        };
    }
}

detail.xml File is,

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ListView
                android:id="@+id/lstDetail"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>
    </RelativeLayout>

list_item.xml file is,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="111" />

        <TextView
            android:id="@+id/txtDesc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imgCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/cancel" />

    <TextView
        android:id="@+id/txtMemberId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtUserEventId"
        android:layout_alignBottom="@+id/txtUserEventId"
        android:layout_alignParentLeft="true"
        android:text="222" />
</RelativeLayout>

解决方案

in your custom adapter call this.notifyDataSetChanged(); where you are performing delete functionality and deleting that element from arrayList which is set to that adapter

这篇关于如何ListView控件刷新后删除Android上的按钮Click事件中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆