如何删除自定义适配器中列表视图的项目?

时间:2015-07-23 08:42:03

标签: android listview adapter

当用户点击对话框的确定​​按钮时,我需要删除列表视图的项目。所以我需要在自定义适配器类中进行处理。我只想知道如何删除自定义适配器中列表视图的项目。
这是我的代码:

public class AddressAdapter extends BaseAdapter implements OnClickListener{
ArrayList<Model_Address> arr_list;
Activity activity;
LayoutInflater inflater;

Typeface gotham_light,gotham_light_italic,gotham_medium,gotham_medium_italic,gotham_book,gotham_book_italic;

public String tag="AddressAdapter";
RelativeLayout rl_main;
ProgressDialog dialog;


public AddressAdapter(Activity activity,ArrayList<Model_Address> arr_list,RelativeLayout rl_main)
{

    this.activity=activity;
    this.arr_list=arr_list;
    this.rl_main=rl_main;
    inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    gotham_light=Typeface.createFromAsset(activity.getAssets(), "fonts/gotham_light.ttf");
    gotham_light_italic=Typeface.createFromAsset(activity.getAssets(), "fonts/gotham_light_italic.ttf");
    gotham_medium=Typeface.createFromAsset(activity.getAssets(), "fonts/gotham_medium.ttf");
    gotham_medium_italic=Typeface.createFromAsset(activity.getAssets(), "fonts/gotham_medium_italic.ttf");
    gotham_book_italic=Typeface.createFromAsset(activity.getAssets(), "fonts/gotham_book_italic.ttf");
    gotham_book=Typeface.createFromAsset(activity.getAssets(), "fonts/gotham_book.otf");
}

@Override
public int getCount() {

    if (arr_list.size()>0) {
        int count=arr_list.size();
        return count;
    }
    return 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    int id=arr_list.get(position).getId();
    return id;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view=convertView;
    final ViewHolder holder;

    if (view==null) {
        view=inflater.inflate(R.layout.address_item, null);
        holder=new ViewHolder();
        holder.rl_main=(RelativeLayout)view.findViewById(R.id.rl_main);
        holder.rl_submain=(RelativeLayout)view.findViewById(R.id.rl_submain);
        holder.tv_type=(TextView)view.findViewById(R.id.tv_type);
        holder.tv_address=(TextView)view.findViewById(R.id.tv_address);
        holder.tv_status=(TextView)view.findViewById(R.id.tv_status);
        holder.iv_list=(ImageView)view.findViewById(R.id.iv_edel);

        holder.tv_type.setTypeface(gotham_book);
        holder.tv_address.setTypeface(gotham_light);
        holder.tv_status.setTypeface(gotham_book);

        view.setTag(holder);
    } else {
        holder=(ViewHolder)view.getTag();
    }

    if (arr_list.size()<=0) {
        Toast.makeText(activity, "No Data Available", Toast.LENGTH_SHORT).show();
    } else {

        final Model_Address temp_model=arr_list.get(position);

        holder.tv_type.setText(temp_model.getType());
        holder.tv_address.setText(temp_model.getAddress());
        holder.tv_status.setText(temp_model.getDelivery_status());
        holder.iv_list.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                PopupMenu popMenu=new PopupMenu(activity, holder.iv_list);
                popMenu.getMenuInflater().inflate(R.menu.edit_delete_menu, popMenu.getMenu());
                popMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        String selection=item.getTitle().toString();
                        Toast.makeText(activity, "u="+temp_model.getAddress(), Toast.LENGTH_SHORT).show();
                        switch (selection) {
                            case "Edit":
                                Log.d(tag, "edit>>"+holder.tv_address.getText().toString()+" a="+holder.tv_type.getText().toString());
                                Intent intent=new Intent(activity, Add_Address_Uae.class);
                                intent.putExtra("address", ""+temp_model.getAddress());
                                intent.putExtra("type", temp_model.getType());
                                intent.putExtra("city", temp_model.getCity());
                                intent.putExtra("locality", temp_model.getLocality());
                                intent.putExtra("store_id", temp_model.getStore_id());
                                intent.putExtra("address_id", String.valueOf(temp_model.getId()));
                                activity.startActivity(intent);
                                activity.finish();
                                break;
                            case "Delete":
                                Log.d(tag, "delete"+holder.tv_address.getText().toString()+" a="+holder.tv_type.getText().toString());
                                dialogAlert2("Alert!", ""+activity.getResources().getString(R.string.delete_confirm),String.valueOf(temp_model.getId()),position);
                                break;
                        }
                        return true;
                    }
                });
                popMenu.show();

            }
        });
        view.setOnClickListener(new OnItemClickListener(position));
    }
    return view;
}

private class ViewHolder
{
    RelativeLayout rl_main,rl_submain;
    TextView tv_type,tv_address,tv_status;
    ImageView iv_list;

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

private class OnItemClickListener implements OnClickListener{

    private int position=0;

    public OnItemClickListener(int position)
    {
        this.position=position;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        AddressView addressView=(AddressView) activity;
        addressView.onItemClick(position);

    }

}

public void dialogAlert2(String title,String tv_sub,final String address_id,final int position)
{
    DialogForAlert dAlert=new DialogForAlert(activity, rl_main, new DialogForAlert.OnOKButtonClickListener() {

        @Override
        public void onOkClick() {
            // TODO Auto-generated method stub
            Toast.makeText(activity, "ok clicked", Toast.LENGTH_SHORT).show();
            new DeleteItemAsync(address_id,position).execute();
            notifyDataSetChanged();

        }
    }, new DialogForAlert.OnCancelButtonClickListener() {

        @Override
        public void onCancelClick() {
            // TODO Auto-generated method stub
            Toast.makeText(activity, "cancel clicked", Toast.LENGTH_SHORT).show();

        }
    });

    dAlert.setTitle(title);
    dAlert.setMessage(tv_sub);
    dAlert.setType(DialogForAlert.SHOW_TWO_BUTTON);

}

class DeleteItemAsync extends AsyncTask<Void, Void, Void>
{
    String address_id="";
    int position=0;

    DeleteItemAsync(String address_id,int position)
    {
        this.address_id=address_id;
        this.position=position;
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        startDialog();
        WifiManager wm=(WifiManager)activity.getSystemService(activity.WIFI_SERVICE);
        Constants.ip_addr=Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        deleteItemListener.setParameters(Constants.customer_id, "delete", Constants.device_id, Constants.device_type,
                        URLEncoder.encode(Constants.ip_addr), address_id);
        String result=deleteItemListener.callURL();
        JsonParsingForForget jsonParsingForForget=new JsonParsingForForget(result);
        jsonParsingForForget.parse();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        stopDialog();
        if (Constants.sucess.matches("1")) {
            AddressView.removeItem(position,address_id);
        } else {
            dialogAlert(activity.getResources().getString(R.string.error), ""+Constants.sucesstxt);
        }
    }

}

DeleteItemListener deleteItemListener=new DeleteItemListener() {
    String params="";
    @Override
    public String callURL() {
        // TODO Auto-generated method stub
        WebService webService=new WebService(Constants.URL_KEY_LOCATION+params.replace(" ", "%20"));
        webService.callWebServiceByGetMethod();
        String result=webService.getResult();
        return result;
    }

    @Override
    public void setParameters(String customer_id, String action,
            String device_id, String device_type, String ip, String address_id) {
        // TODO Auto-generated method stub
        params="customer_id="+customer_id+"&action="+action+"&device_id="+device_id+"&device_type="+device_type+
                "&ip="+ip+"&address_id="+address_id;

    }
};

public void startDialog()
{
    try {
        dialog=new ProgressDialog(activity);
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();
        dialog.setContentView(R.layout.progress_dialog);
        dialog.setCanceledOnTouchOutside(false);
    } catch (Exception e) {
        // TODO: handle exception
    }

}

public void stopDialog()
{
    try {
        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }

}


public void dialogAlert(String title,String tv_sub)
{
    DialogForAlert dAlert=new DialogForAlert(activity, rl_main, new DialogForAlert.OnOKButtonClickListener() {

        @Override
        public void onOkClick() {
            // TODO Auto-generated method stub
        }
    });
    dAlert.setTitle(title);
    dAlert.setMessage(tv_sub);
    dAlert.setType(DialogForAlert.SHOW_ONE_BUTTON);

}

}

1 个答案:

答案 0 :(得分:3)

arr_list.remove(position);
notifyDataSetChanged();