不能从里面的CursorAdapter行删除的EditText [英] Can not remove EditText from a row from inside CursorAdapter

查看:119
本文介绍了不能从里面的CursorAdapter行删除的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在低于code,我设法得到一个的EditText 来连续在内线发挥作用。还设法使按钮获得点击(不整排被点击),并触发一个的AsyncTask 只要点击情况。
在code运行良好,并没有工作。但是,每当我preSS我希望它在被删除的按钮 onPostExecute 异步的。

(在下面的code,我不发布 setTag viewTag 的东西。做不介意的话,我可以得到被点击哪一行被点击的按钮时)。
所以请专注于制作的EditText 无形的,而不是。

 公共类myCursorAdapter扩展的CursorAdapter
{
字符串ID;公共myCursorAdapter(上下文的背景下,光标C,INT的地位)
{
        超(背景下,C,地位);
} 保护静态类RowViewHolder
 {
   公众的TextView resentTV;
 }@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父)
{
        LayoutInflater充气= LayoutInflater.from(parent.getContext());
        查看retView = inflater.inflate(R.layout.row,父母,假);        RowViewHolder持有人=新RowViewHolder();
        holder.resentTV =(TextView中)retView.findViewById(R.id.resendTextViewVIEW);
        holder.resentTV.setOnClickListener(mOnTitleClickListener);        返回retView;
} @覆盖
公共无效bindView(查看六,上下文的背景下,光标光标)
{
        DbAllHelper分贝= DbAllHelper.getInstance(上下文);        TextView的nameTV =(TextView中)vi.findViewById(R.id.nameTextViewVIEW);
        字符串名称= cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)));
        nameTV.setText(名);        TextView的resendTV =(TextView中)vi.findViewById(R.id.resendTextViewVIEW);
        resendTV.setVisibility(View.GONE);        串ID = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0)));}私人OnClickListener mOnTitleClickListener =新OnClickListener()
{
        @覆盖
        公共无效的onClick(视图v)
        {
                新myAsynck()执行(); //做网络的东西
        }
};类MySync扩展的AsyncTask<字符串,字符串,字符串>
{
    @覆盖
    在preExecute保护无效()
    {
        super.on preExecute();
    }    保护字符串doInBackground(字符串参数... args)
    {
        //做一些网络的东西
        返回null;
    }    保护无效onPostExecute(字符串str)
    {
       //此处问题
       //删除了导致这个按钮异步运行该行的EditText
    }
}


解决方案

下面是一个简单的解决方案。只要创建一个稍微不同的的AsyncTask 并传递给它的查看要执行任务后躲。
下面的实现使用的WeakReference<视图> ,以避免可能的泄漏:

 类MySync扩展的AsyncTask<字符串,字符串,字符串>
    {        WeakReference的<视图>隐藏;        公共MySync(视图v){
            超();
            toHide =新的WeakReference<视图>(五);
        }        @覆盖
        在preExecute保护无效()
        {
            super.on preExecute();
        }        保护字符串doInBackground(字符串参数... args)
        {
            //做一些网络的东西
            返回null;
        }        保护无效onPostExecute(字符串str)
        {
            如果(toHide = NULL&放大器;!&安培;!toHide.get()= NULL)
                。toHide.get()setVisibility(View.GONE);
        }
    }

现在,调用新的的AsyncTask 通过在目标查看

  @覆盖
   公共无效的onClick(视图v){
       新myAsynck(ⅴ).execute(); //做网络的东西
   }

如果你需要隐藏查看这是从不同的目标视图的(这是,你的情况,按钮),你可以使用这样的事情找到另一个孩子从父启动:

 的ViewGroup VG =((ViewGroup中)toHide.get()的getParent());
    查看otherView = vg.findViewById(R.id.editText);
    otherView.setVisibility(View.GONE);

In the below code, I have managed to get an EditText to function inside a row. Also managed to make the button get clicked (without the whole row being clicked) and fire an AsyncTask whenever the click happens. The code runs good and does the job. However, whenever I press the button I want it to be removed in the onPostExecute of the async.

(in the below code, I do not post the setTag and the viewTag stuff. Do not mind it, as I can get which row was clicked when the button was clicked.) So please focus on making the EditText invisible rather than that.

public class myCursorAdapter extends CursorAdapter
{      
String id;

public myCursorAdapter (Context context, Cursor c, int status)
{
        super(context, c, status);
}

 protected static class RowViewHolder
 {
   public TextView resentTV;
 }

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.row, parent, false);

        RowViewHolder holder = new RowViewHolder();
        holder.resentTV = (TextView) retView.findViewById(R.id.resendTextViewVIEW);
        holder.resentTV.setOnClickListener(mOnTitleClickListener);

        return retView;
}

 @Override
public void bindView(View vi, Context context, Cursor cursor)
{
        DbAllHelper db = DbAllHelper.getInstance(context);

        TextView nameTV = (TextView) vi.findViewById(R.id.nameTextViewVIEW);
        String name = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)));
        nameTV.setText(name);

        TextView resendTV = (TextView) vi.findViewById(R.id.resendTextViewVIEW);
        resendTV.setVisibility(View.GONE);

        String id= cursor.getString(cursor.getColumnIndex(cursor.getColumnName(0)));

}

private OnClickListener mOnTitleClickListener = new OnClickListener()
{
        @Override
        public void onClick(View v)
        {
                new myAsynck().execute(); //does network stuff
        }
};



class MySync extends AsyncTask<String, String, String>
{
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    protected String doInBackground(String... args)
    {   
        //do some network stuff
        return null;
    }

    protected void onPostExecute(String str)
    {
       //problem here
       //remove the EditText of the row that had the button that caused this async to run
    }
}

解决方案

Here's a simple solution. Just create a slightly different AsyncTask and pass to it the View which you want to hide after the task execution. The following implementation uses a WeakReference<View> to avoid possible leaks:

   class MySync extends AsyncTask<String, String, String>
    {

        WeakReference<View> toHide;

        public MySync(View v){
            super();
            toHide = new WeakReference<View>(v);
        }

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

        protected String doInBackground(String... args)
        {   
            //do some network stuff
            return null;
        }

        protected void onPostExecute(String str)
        {
            if(toHide != null && toHide.get() != null)
                toHide.get().setVisibility(View.GONE);
        }
    }

Now, invoke your new AsyncTask passing the target View:

   @Override
   public void onClick(View v) {
       new myAsynck(v).execute(); //does network stuff
   }

If you need to hide a View which is different from the target View (which is, in your case, the button), you can use something like this to find another child starting from the parent:

    ViewGroup vg = ((ViewGroup)toHide.get().getParent());
    View otherView = vg.findViewById(R.id.editText);
    otherView.setVisibility(View.GONE);

这篇关于不能从里面的CursorAdapter行删除的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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