无法从CursorAdapter内部的行中删除EditText

时间:2015-01-31 18:56:35

标签: android

在下面的代码中,我设法让EditText在一行内运行。还设法点击按钮(没有点击整行)并在点击发生时触发AsyncTask。 代码运行良好并完成工作。但是,每当我按下按钮时,我希望它在异步的onPostExecute中删除。

(在下面的代码中,我没有发布setTagviewTag内容。请不要介意,因为我可以在点击按钮时点击哪一行。) 所以请专注于使EditText不可见而不是那个。

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
    }
}

1 个答案:

答案 0 :(得分:2)

这是一个简单的解决方案。只需创建一个略有不同的AsyncTask并将任务执行后要隐藏的View传递给它。 以下实现使用WeakReference<View>来避免可能的泄漏:

   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);
        }
    }

现在,调用新的AsyncTask传递目标View

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

如果您需要隐藏与目标视图(在您的情况下是按钮)不同的View,您可以使用这样的内容来查找另一个孩子从父母开始:

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