我的列表视图中有一个编辑文本和一个“保存”按钮。当用户单击“保存”按钮时,我必须清除文本。我试过像
txtDescription.setText("");
但没有奏效。任何人都知道它为什么不起作用? Adapter类已附加
请帮助我,提前致谢!!
private class ListAdapters extends ArrayAdapter<ApplicationBean> {
private ArrayList<ApplicationBean> items;
private int position;
public ListAdapters(Context context, int textViewResourceId,
ArrayList<ApplicationBean> mTitleList) {
super(context, textViewResourceId, mTitleList);
this.items = mTitleList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
this.position = position;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.applicationlistitem, null);
}
final ApplicationBean o = (ApplicationBean) items.get(position);
if (o != null) {
txtDescription = (EditText) v
.findViewById(R.id.description_text);
submitButton = (Button) v.findViewById(R.id.submit_btn);
submitButton.setTag(position);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PostRequest p = new PostRequest(Integer.parseInt(v
.getTag().toString()));
p.execute();
}
});
}
return v;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return items.size();
}
}
private class PostRequest extends AsyncTask<Void, Void, String> {
ProgressDialog dlgprogress;
int position;
public PostRequest(int selectedIndex) {
position = selectedIndex;
}
@Override
protected void onPostExecute(String result) {
dlgprogress.dismiss();
final Dialog dlg = new AlertDialog.Builder(mContext)
.setTitle("Message")
.setMessage(result)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int whichButton) {
dlgprogress.dismiss();
dlgprogress.cancel();
txtDescription.setText("");
}
}).create();
dlg.show();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
dlgprogress = ProgressDialog.show(mContext, "", "Please wait");
// dlgprogress.show(mContext, "", "Please wait");
super.onPreExecute();
}
@Override
protected String doInBackground(Void... arg0) {
//do something.............
//..........................
dlgprogress.dismiss();
return rsponse;
}
}
applicationlistitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout
android:id="@+id/lineatlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DESCRIPTION : "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="@+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textMultiLine"
android:textColor="#000000">
</EditText>
</LinearLayout>
<Button
android:id="@+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT" />
</LinearLayout>
答案 0 :(得分:2)
维护listview的自定义适配器类
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
Context context = getApplicationContext();
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.search_list_item, null);
EditText txtDescription= (EditText)row.findViewById(R.id.txtDescription);
Button Savebtn = (Button)row.findViewById(R.id.SaveBtn);
Savebtn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
txtDescription.setText("");
// And your additional coding
}
});
}
如果您不熟悉listview的自定义适配器,请选中here
答案 1 :(得分:1)
您还可以将任意object
设置为按钮视图中的TAG。
所以在你的getView()
方法中这样做,我认为它会起作用。
//EDIT
txtDescription.setTag(position); // set position to edittext
submitButton = (Button) v.findViewById(R.id.submit_btn);
submitButton.setTag(txtDescription); // set the current edittext object
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText tv = (EditText)v.getTag(); // get edittext object
tv.setText("");
//Edit
PostRequest p = new PostRequest(Integer.parseInt(tv
.getTag().toString())); // get position from edittext
}
});
编辑发送完整视图
我没有对此进行测试,但我认为如果不起作用它会起作用,然后在发送editext时将Checkbox发送给构造函数。
class PostTask extends AsyncTask<Void,Void,Void>
{
CheckBox cb;
EditText et;
int pos;
PostTask(int pos,View view)
{
cb = view.findViewById(R.id.cbox1);
et = view.findViewById(R.id.et1);
pos = Integer.parseInt(et.getTag().toString());
}
}
//现在在getView中更改
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PostRequest p = new PostRequest((View)v.getParent()); // get position from edittext
}
});
答案 2 :(得分:0)
你有每个列表项的editText和提交按钮吗?
好吧,listView有一个转换视图的系统......它不会像你的数组大小那样创建视图,但只创建足以在屏幕上显示用户然后在你滚动时开始转换它们你想要的...所以如果你清除一个editText,你可以看到它充满了其他的值...所以我建议你有另一个数组来提供你的listView,就像你保存你的editText值和提交按钮点击你删除它们在适配器上,您应该使用此数组设置editText的文本,因此如果值已经提交,它将为空。
以下是您的问题的解决方案:
尝试使用与列表大小相同的数组:
ArrayList<String> texts = new ArrayList<String>();
for(int i = 0; i < items.size(); i++)
texts.add(“”);
所以你的所有editTexts一开始都是空的...... 然后在自定义适配器中的getView方法上设置它们:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
this.position = position;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.applicationlistitem, null);
}
final ApplicationBean o = (ApplicationBean) items.get(position);
if (o != null) {
txtDescription = (EditText) v
.findViewById(R.id.description_text);
// you should set your every views’ editText here
txtDescription.setText(texts.get(position));
// here you save texts also they changes
txtDescription.addTextChangedListener({
@Override
public void afterTextChanged(Editable s) {
texts.remove(position);
texts.add(position, s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) { }
});
submitButton = (Button) v.findViewById(R.id.submit_btn);
submitButton.setTag(position);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
texts.remove(position);
texts.add(position, “”);
PostRequest p = new PostRequest(Integer.parseInt(v
.getTag().toString()));
p.execute();
}
});
}
return v;
}