How to change text in edit text which is embedded in a list view. i want to the change the text's value on button click
holder.txtQty.setTag(position);
holder.plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String val = holder.txtQty.getText().toString();
Float f = new Float(val);
f = f + 1;
holder.txtQty.setText(f.toString().replace(".0", "").trim());
}
});
but on this only the first and last rows edit text change . i need the corresponding edit text to change
I would get the position in the list of the text you wish to change, write a public method to replace the content at that position and then notify the custom adapter that the data set has changed.
For example:
public void replaceItemAt(int position, String newText)
{
//Replace the item in the array list
this.textfieldarraylist.set(position, newText);
//Let the custom adapter know it needs to refresh the view
this.notifyDataSetChanged();
}
if you want to change text of edittext then you can store it in an arraylist. suppose if 3rd element is clicked then you can just get(position) from arraylist and then set it. Initially set all values with default. so in this way you can set and finally get text from your arraylist.
every time when edittext text will change then get the position and then save it to your arraylist...
Related
Currently: i have a recycler view where it has delete button and an edit text for each item.
Each item in the recycler view contains edit text that the user can input.
The scenario is i have 4 item in the list resulting 4 edit text.
When i try to fill up edit Text in the item(n) and tried to delete
the item(n) and add again. The edit Text value in the edit text remains.
How can i clear the value of the edit text?
Here is my current code when i try to delete a specific item in the list:
mHeaderText.remove(pos);
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i = 1 ; mHeaderText.size() >= i ; i++) {
temp.add(i);
Log.d("ADDDING","ADDDING"+i);
}
mHeaderText.clear();
mHeaderText.addAll(temp);
notifyDataSetChanged();
Try this code ..
In adapter class in onBindView method when you perform click event then set edittext value as null.
edittext.setText("");
and if your edittext box in activity layout then make interface into adapter class and handling click event like this way...
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(String data); //pass any data to shared it.
}
after that..
#Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
final String data = mStringList.get(position);
holder.textView.setText(data);
holder.firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClick.getPosition(data); // shared data.
}
});
}
after that in activity adapter bind into recycler view it means adapter not null then called below line..
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
#Override
public void getPosition(String data) {
// hear perform any operation.
editTextbox.setText("");
adpater.notifyDataSetChanged();
}
});
Use
edittext.setText("");
to clear text where edittext is reference to EditText. Since you haven;t posted complete code so i can guide to this only. If you provide more code, then I can help you more.
One thing is sure. You will need to use
edittext.setText("") to clear the value.
But you need to be careful. You need to keep this as the first line in your delete row function, i.e. before you are removing that edittext item from your list.
Here is my code: I have placed the "remove row" function inside onBindViewHolder() method of Recyclerview Adapter.
holder.delImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// You need to place it in the start
holder.editText.setText("");
// Remove the item on remove/button click
myArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, myArrayList.size());
Toast.makeText(context, "Removed : " + position +" item", Toast.LENGTH_SHORT).show();
}
});
How can I show different content after selecting an item from the Spinner?
I want to create a Spinner with locations of chain stores.
I want the spinner to be always there on top. The only thing that
changes to be the content under the spinner
Create a simple method in your activity to refresh the layout below the Spinner(which will remain untouched). That method will be called from a OnItemSelectedListener set on the Spinner. It would be something like this:
private void changeAdress(int newSelectedAdress) {
// The ImageView and the TextView will be already in the layout
ImageView map = (ImageView) findViewById(R.id.theIdOfTheImage);
// Set the image. You know the current address selected by the user
// (the newSelectedAddress int) so get it from the array/list/database
// where you stored it
// also set the image
}
Called the method above from the onItemSelected callback:
yourSpinnerRefference.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
changeAddress(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
If this is not what you want, please explain.
Edit: Make two arrays to hold your data:
int[] images = {R.drawable.imag1, R.drawable.imag2 ..etc...};
//also for the text
String[] text = {"text1", "text2 ...etc...};
Then use those two arrays in the method I recommended above:
private void changeAdress(int newSelectedAddress) {
((ImageView)findViewById(R.id.mapView1)).setImageResource(images[newSelectedAddress]);
// assing an id to the TextView in your layout and do the same as above.
}
There is no need for multiple ImageView and TextViews.
I have a list view which is populated using a custom adapter.The list view contains 2 lists and an image.Format- list list image.
I want to delete items from both the lists at the same time when that button is clicked.
I tried using onclick and onitemclick but the app crashed.
How do i delete both the lists at the same time?The list are not saved in the date base.Items are stored in an Vector.
What is use of index and view?
when custom adapter extends Base Adpater we get a getView method which contains a parameter position .Is this same as index value?
code-holder.imgBtn.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View arg0) {
Index index = (Integer) arg0.getTag();
if(index>=0){
prescriptionRemoval = ProgressDialog.show(AddToCartActivity.this, null, getResources().getString(R.string.prescriptionRemoval));
String removeDrug=drugNameList.get(index);
String removePrescription=prescriptionList.get(index);
String removeTotal=removeDrug+removePrescription;
listCheck.remove(removeTotal);
drugNameList.remove(index.intValue());
prescriptionList.remove(index.intValue());
notifyDataSetChanged();
handler.sendEmptyMessage(DIALOG_PRESCRIPTION_REMOVAL);
}
}
})
Found the answer.Get the position and remove both lists with the position value
and notifydatasetchanged()
I am developing an app where i need to set spinner values dynamically based on previous screen values. My code...
Main.java.
String[] values = {"All","Only Walk-in","Only Phone","Only Web","Walkin-phone","Walkin-web","phone-web"};
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,apttypes);
spinner.setAdapter(adapter);*/
But here what i want is from previous screen i am getting some value (like spinner postion). based on that i need to set spinner value to display...
Means from previous screen if i got values =0 means,
i need to set spinner value to display "All" from values array at top.
If i got value= 5 means,
i want to set spinner value to display as "Walkin-web"
How can i do that. can anyone help me with this...
Pass the value in from the previous Activity using extras in the Intent you use to launch it. Then when you've read the value call
int position = getIntent().getIntExtra( "name", defaultValue );
spinner.setSelection( position );
Which will move the spinner to the index you selected.
Use following array code and create new array adapter each time
String[] str=new String[maxsize-4];
you can implement onItemClick event on Spinner like this and setSelection(position)
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
payfeeTabStudentNameSpinner.setSelection(position);
spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Inside your First Activity A.java
public static String Position = YOUR_SPINNER1.getSelectedItem().toString();
Inside your Second Activity B.java
if(A.Position.equals("0")){
//Set your adapter accordingly
}else if(A.Position.equals("1")){
//Set your adapter accordingly
}
You can assign the Spinner's Position using the following code..
Spinner s1;
-----------
-----------
int position=valueFromPrevious;
s1.setSelection(position);
-----------
-----------
pass the values from previous activity using Extras and then when u want use it in your current activity follow this:
If u get String value then typecast it into interger by parseInt method...
String strtext4 = getIntent().getStringExtra("qualification");
then
int position = Integer.parseInt(strtext4);
after this just set it to your spinner
qualificationspinner.setSelection(position);
I have the listview in my activity. In the list view I have an edit text box with a filled value. I am filling the value using a hashmap. If the user can change the edit text value in list view, I need to update the hashmap and refresh the list view with update values
Can anybody tell me how to do this?
Without code, making a bunch of assumptions here...
Inside your custom adapter (in your getView method), use setOnFocusChangeListener on the EditText and have it fire your code to update the hashmap.
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) { // run when focus is lost
String value = v.getText().toString(); // get the value from the EditText
// Your code to update hashmap
}
}
});