Reputation: 1561
I have a edit text and a Save button inside my list view. I have to clear the text when the user clicks the save button. I have tried like
txtDescription.setText("");
But not worked. Any one know why it is not worked ? The Adapter class is attached
Please Help Me, Thanks in Advance!!
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>
Upvotes: 2
Views: 3355
Reputation: 34301
You can also set any object
as TAG to your button view.
So do it like this in your getView()
method and I supppose it will work.
//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
}
});
EDIT TO SEND Complete View
I have not tested this, but I suppose it will work if don't work then also send the Checkbox to constructor as you send editext.
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());
}
}
//Now in getView change
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PostRequest p = new PostRequest((View)v.getParent()); // get position from edittext
}
});
Upvotes: 1
Reputation: 4860
You got editText and submit button on every list item??
All right, listView has the system that converts views... It does not create view as much as your array's size, but only creates enough to show user on screen then it starts convert them however you want while you're scrolling... so if you clear one editText, you can see it filled with some other's value... So i would recommend you to have another array to feed your listView, like you save your editText value and on submit button click you delete them and on adapter you should set editText's text with this array, so it'll be empty if the value's already submitted.
And here is a solution for your issue:
Try to have an array same sized with your list:
ArrayList<String> texts = new ArrayList<String>();
for(int i = 0; i < items.size(); i++)
texts.add(“”);
so all of your editTexts will be empty at first... Then you set them on your getView method in your custom adapter:
@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;
}
Upvotes: 0
Reputation: 21191
Maintain the custom adapter class for the 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
}
});
}
If you are new to custom adapter for the listview check here
Upvotes: 2