gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:54:08 25 4
gpt4 key购买 nike

在下面的代码中,我设法让 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
}
}

最佳答案

这是一个简单的解决方案。只需创建一个略有不同的 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这与目标 View (在您的情况下是按钮)不同,您可以使用类似这样的方法从父级开始查找另一个子级:

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

关于android - 无法从 CursorAdapter 内部的一行中删除 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com