Hi I am using freeze concept in listview,
Row contains Textview and Edit texts,If i change any Edit text it should update the other column Textview is it possible? Currenly i am using https://github.com/InQBarna/TableFixHeaders Android library
Here is my adapter class:
When i change the age in edittext i need to change height based on age input.
public class FamilyNexusAdapter extends BaseTableAdapter {
private final int[] widths = { 100, 100, 100 };
private final float density;
private Vector<ProductMasterBO> items;
public FamilyNexusAdapter(Context context, Vector<ProductMasterBO> items) {
density = context.getResources().getDisplayMetrics().density;
this.items = items;
}
#Override
public int getRowCount() {
return this.items.size();
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public View getView(final int row, int column, View convertView,
ViewGroup parent) {
String headerString[] = { "column 1", "column 2", "column 3",
};
if (row == -1) {
convertView = getLayoutInflater().inflate(
R.layout.item_table_header_first, parent, false);
((TextView) convertView.findViewById(R.id.product_name))
.setText(headerString[column + 1]);
} else{
user= (UserMaster) items.get(row);
if(column==-1){
txt=user.getuserName();
((TextView) convertView.findViewById(R.id.user_name))
.setText(headerString[column + 1]);
}else if(column==1){
txt=user.getage()+"";
EditText age = (EditText) convertView
.findViewById(R.id.age);
age.setText(txt);
age .addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String ageString= s.toString();
if (!qty.equals("")) {
}}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
//i want to handle when i am trying to change age height should be change
}
});
}else if(column==2){
txt=user.getheight()+"";
((TextView) convertView.findViewById(R.id.user_name))
.setText(txt);
}
return convertView;
}
Use a viewHolder pattern and set the tag property of the EditText to refer the TextView.
Related
I'm trying to add some results from a quiz so that when user gets each right answer it adds 1 to it but the code below never gives me the right answer the 'correctAns = correctAns+1'gives me all 5 at once, if I make the correctAns as local variable this will give me 1 for every right answer but it will NOT add up. Can any one please help.
public class AdapterListView extends ArrayAdapter<Questions> {
private int correctAns = 0;
public AdapterListView(Context context, int resource, ArrayList<Questions> questionsArrayList) {
super(context, resource);
this.mContext = context;
this.setQuestionsArrayList(questionsArrayList);
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
....
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
holder.editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (holder.editText.getText().toString().trim().length() > 0) {
int inputNum = Integer.parseInt(String.valueOf(holder.editText.getText().toString().trim()));
if (getAnswersList().get(position) != inputNum) {
holder.imgFalse.setVisibility(View.VISIBLE);
holder.imgTrue.setVisibility(View.GONE);
} else {
correctAns = correctAns+1;
holder.imgTrue.setVisibility(View.VISIBLE);
holder.imgFalse.setVisibility(View.GONE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
return convertView;
}
static class ViewHolder {
TextView questionTextView;
EditText editText;
ImageView imgTrue, imgFalse;
}
}
You should increase the value of variable n
Replace
setCorrectAns(++n);
With this
n=n+1;
How to get the values from the edit text .
Hello everyone im facing a problem with android task in which there so many edit text available in the edit text .
i need to get the values from 1 to 4 all edit texts ..?
Below is my get view method of Adapter.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.social_media_row, null);
holder = new ViewHolder();
holder.social_id = (EditText) convertView.findViewById(R.id.socialMediaId);
convertView.setTag(holder);
holder.social_id.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
Log.i("AfterTextChange",holder.social_id.getText().toString());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
SocialMediaModel contact = contactsList.get(position);
holder.social_id.setText(contact.getSocialMediaId());
holder.social_id.setTag(contact);
return convertView;
}
Update : i want to get the values when user click on the button
savebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/here what i can do
}
});
//at top Globally declare it
private String[] valueList = new String[];
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.social_media_row, null);
holder = new ViewHolder();
holder.social_id = (EditText) convertView.findViewById(R.id.socialMediaId);
convertView.setTag(holder);
holder.social_id.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
valueList[pos] = s.toString();
}
#Override
public void afterTextChanged(Editable s) {
Log.i("AfterTextChange",holder.social_id.getText().toString());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
SocialMediaModel contact = contactsList.get(position);
holder.social_id.setText(contact.getSocialMediaId());
holder.social_id.setTag(contact);
return convertView;
}
// method to get Values
public String[] getValueList(){
return valueList;
}
Now on button click event put this code
String[] myvl = yourAdapteName.getValueList();
You can implement a method like this one for each EditText with the list position as parameter :
public String getValueFromFirstEditText(int position){
//here you need to recreate the id for the first editText
String result = textValues.get("theFirstEditTextAtPos:"+position);
if(result ==null)
result = "default value";
return result;
}
i hope this code help you
USE Adapter cell position
follow this code
in getView(in Adapter Class)
edittext.getText().toString();
Declare this in your declaration
protected AdapterView.OnItemClickListener messageClickedHandler;
Click event for the list view using Adapter view
messageClickedHandler = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
String strName=rowItems.get(position).getStrName();
Toast.makeText(getApplicationContext(),"Name : "+strName , Toast.LENGTH_LONG).show();
}
};
list.setOnItemClickListener(messageClickedHandler);
}
rowItems is an array list Object , getStrName() from the user defined class.
I have ListView that has a an EditText in every row.When i enter the value in the EditText it duplicate itself in every fourth row in the ListView . How can I prevent that.
public class ItemsAdapter extends ArrayAdapter<Items>{
Double Price;
Double Quantity;
String _Quantity;
private int resource;
private LayoutInflater inflater;
private Context context;
Items items;
public ItemsAdapter(Context ctx, int resourceId, List<Items> objects){
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
ItemViewHolder viewHolder;
items = getItem( position );
if (convertView == null) {
viewHolder=new ItemViewHolder();
convertView = ( RelativeLayout ) inflater.inflate( resource, null );
viewHolder.txtQuantity =(EditText)convertView.findViewById(R.id.txtQuantity);
viewHolder.lblItemsName=(TextView) convertView.findViewById(R.id.lblItemName);
viewHolder.lblPrice = (TextView) convertView.findViewById(R.id.lblPrice);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
viewHolder.lblItemsName.setText(items.getItemName());
viewHolder.lblPrice.setText(items.getPrice().toString());
return convertView;
}
public class ItemViewHolder {
public EditText txtQuantity ;
public TextView lblItemsName;
public TextView lblPrice;
}
}`
When you start enter the quantity in each cell of ListView, you should save the quantity to the items
After that, you load the quantity from item
... your above code
}
viewHolder.txtQuantity.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void afterTextChanged(Editable s) {
items.setQuantity(s); // set quantity for each item when you enter quantity
}
});
viewHolder.lblItemsName.setText(items.getItemName());
viewHolder.lblPrice.setText(items.getPrice().toString());
viewHolder.txtQuantity.setText(items.getQuantity()); // load quantity for each item
...
Hope this help
The duplication happens because views are recycled in Listview when you scroll. To avoid this you should reset the text in Edittext in getView() method.
If you want to retain the text in EditText when you come back to the view again you should do something like Phan Văn Linh suggested.
I figure it out
public ItemAdapter(Context context, int textViewResourceId,
ArrayList<Cart> productList) {
super(context, textViewResourceId, productList);
this.productList = new ArrayList<Cart>();
this.productList.addAll(productList);
}
#Override
public int getCount() {
return this.productList.size();
};
#Override
public View getView(final int position, View view, ViewGroup parent) {
DecimalFormat df = new DecimalFormat("0.00##");
// productList= DBAdapter.getCartList();
final Cart product = productList.get(position);
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_cart, null);
EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
//attach the TextWatcher listener to the EditText
quantity.addTextChangedListener(new MyTextWatcher(view));
}
TextView ItemNumber=(TextView)view.findViewById(R.id.itemNumber);
ItemNumber.setVisibility(View.GONE);
EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
quantity.setTag(product);
if(product.getQuantity() != 0){
quantity.setText(String.valueOf(product.getQuantity()));
}
else {
quantity.setText("1");
}
TextView itemNumber = (TextView) view.findViewById(R.id.itemNumber);
itemNumber.setText(product.getItemNumber());
TextView ItemName = (TextView) view.findViewById(R.id.lblItemName);
ItemName.setText(product.getItemName());
TextView price = (TextView) view.findViewById(R.id.lblPrice);
price.setText( df.format(product.getPrice()));
TextView ext = (TextView) view.findViewById(R.id.lblTotal);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getTotal()));
}
else {
ext.setText("");
}
return view;
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do nothing
}
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("0.00##");
String qtyString = s.toString().trim();
int quantity = qtyString.equals("") ? 0:Integer.valueOf(qtyString);
EditText qtyView = (EditText) view.findViewById(R.id.txtQuantity);
Cart product = (Cart) qtyView.getTag();
if(product.getQuantity() != quantity){
Double currPrice = product.getTotal();
Double extPrice = quantity * product.getPrice();
Double priceDiff = Double.valueOf(df.format(extPrice - currPrice));
product.setQuantity(quantity);
product.setTotal(extPrice);
TextView ext = (TextView) view.findViewById(R.id.lblTotal);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getTotal()));
}
else {
ext.setText("");
}
if(product.getQuantity() != 0){
qtyView.setText(String.valueOf(product.getQuantity()));
}
else {
qtyView.setText("");
}
orderTotal += priceDiff;
TextView cartTotal = (TextView) findViewById(R.id.cartTotal);
cartTotal.setText(df.format(orderTotal));
}
return;
}
}
I have list item with EditText in it, I dont know how many items there will be. I have a problem when I enter some text in EditText, and then scroll down a ListView, after I've scroll up again there is no text in my first EditText, or there is some text from other EditText from ListView.
Code :
public class MatHangAdapter
extends ArrayAdapter<MatHangModel> {
private static final int LAYOUT_ITEM_MAP_DOWNLOAD = R.layout.item_mat_hang;
private final LayoutInflater mInflater;
Context context;
public MatHangAdapter(final Context context) {
super(context,
LAYOUT_ITEM_MAP_DOWNLOAD,
new CopyOnWriteArrayList<MatHangModel>());
this.mInflater = LayoutInflater.from(context);
this.context = context;
}
/**
* input data from outside.
*/
public void fillData(List<MatHangModel> dataModels) {
this.clear();
this.addAll(dataModels);
}
#Override
public long getItemId(final int position) {
return position;
}
#Override
public View getView(int position,
View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
final View view =
this.mInflater.inflate(LAYOUT_ITEM_MAP_DOWNLOAD,
parent,
false);
holder = new ViewHolder(getContext(),
view);
view.setTag(holder);
convertView = view;
}
//fill data.
holder.fillData(this.getItem(position));
return convertView;
}
public static class ViewHolder {
private final Context mContext;
// Fields.
#InjectView(R.id.tv_scan_barcode)
TextView tvScanBarcode;
#InjectView(R.id.tv_qty_price)
TextView tvQtyPrice;
#InjectView(R.id.et_amount)
EditText etAmount;
public ViewHolder(Context context,
View view) {
ButterKnife.inject(this,
view);// Initialize UI controls.
mContext = context;
//apply fonts....
// CommonUtil.setFontForMainTitleTopicViewRecursive(tvItemMenu);
}
/**
* fill data.
*/
public void fillData(MatHangModel matHangModel) {
//fill data
if (matHangModel != null) {
tvScanBarcode.setText(CommonUtil.getStringData(matHangModel.getBracode()));
tvQtyPrice.setText(""+CommonUtil.getIntegerData(matHangModel.getPrice()));
etAmount.setText(""+CommonUtil.getIntegerData(matHangModel.getAmount()));
etAmount.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence charSequence,
int start,
int before, int count) {
}
#Override
public void onTextChanged(final CharSequence charSequence,
final int i,
final int i1,
final int i2) {
}
#Override
public void afterTextChanged(final Editable editable) {
}
});
}
}
#OnClick(R.id.tv_scan_barcode)
void onSceanBarcode(View view){
CommonUtil.showViewWithAnimation(Techniques.FadeIn,view);
}
}
}
It's happen because of the ListView recycling mechanism, you need to update your Adapter data with the new value of the EditTexts.
For better performance, you can do it inside afterTextChanged() of the TextWatcher( to update the data only when you finish editing the text):
#Override
public void afterTextChanged(final Editable editable) {
// update your model
// matHangModel.setAmount(Integer.parseInt(editable.toString()));
// update your adapter data with the new value of "matHangModel"
}
EDIT
1) Add an update method in your adapter:
public void updateItem(MatHangModel model, int position){
this.insert(model, position);
}
2) Change your fillData method to pass to position as param, because you'll need it in the update:
public void fillData(MatHangModel matHangModel, int position) {
//fill data
if (matHangModel != null) {
tvScanBarcode.setText(CommonUtil.getStringData(matHangModel.getBracode()));
tvQtyPrice.setText(""+CommonUtil.getIntegerData(matHangModel.getPrice()));
etAmount.setText(""+CommonUtil.getIntegerData(matHangModel.getAmount()));
etAmount.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence charSequence,
int start,
int before, int count) {
}
#Override
public void onTextChanged(final CharSequence charSequence,
final int i,
final int i1,
final int i2) {
}
#Override
public void afterTextChanged(final Editable editable) {
matHangModel.setAmount(Integer.parseInt(editable.toString())); // here you update your model data
updateItem(matHangModel, position); // here you update your adapter data
}
});
}
}
As you know, Android recycle the views in a ListView so the text you entered in the EditText may be lost if you don't store it.
In the onTextChanged method of the TextWatcher store the text you entered with something like matHangModel.setAmount(etAmount.getText().toString())
Try this:
#Override
public void onTextChanged(final CharSequence charSequence,
final int i,
final int i1,
final int i2) {
matHangModel.setAmount(charSequence.toString()); //if amount is int then parse value. In layout EditText set android:inputType="number"
android:ems="10"
}
I am using a custom list view which contains two Buttons Yes or No. When I clicked on No button, a layout containing an edit text will be displayed. There are more than 15 items in the list view. When I tried to type and save the value in the edittext of the 1st item using TextWatcher(), then the value is save for both 5th and 1st position ie, the same value is saving in both 1st and 5th position.
The code I am using is:
holder.subQuestionAnswer.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
questionsModel.get(position).getQuestion_subquestion().get(0).setAnswer(holder.subQuestionAnswer.getText()
.toString());
Log.e("value", "no_active<>"+position+"<>"+questionsModel.get(position).getQuestion_subquestion().get(0).getAnswer().toString());
}
});
}
How can i avoid this?
It is because of recycling of view. Please check the following adapter. Use the logic according to your needs.
public class MultiSelectionProductAdapter extends ArrayAdapter<ProductListBean> {
private Context context;
private ArrayList<ProductListBean> productList;
private ArrayList<ProductListBean> mOriginalValues;
private LayoutInflater li;
public MultiSelectionProductAdapter(Context ctx, int simpleListItem1,
ArrayList<ProductListBean> data) {
super(ctx, simpleListItem1, data);
this.context = ctx;
this.productList = data;
li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return productList.size();
}
#Override
public ProductListBean getItem(int position) {
return productList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = li.inflate(R.layout.component_inventory_prod_list, null);
holder = new ViewHolder();
holder.tvProductName = (TextView) convertView.findViewById(R.id.tvProductName);
holder.tvProdShortCode = (TextView) convertView.findViewById(R.id.tvShortName);
holder.tvLastOrderedQty = (TextView) convertView.findViewById(R.id.tvLastOrderedQty);
holder.etQty = (EditText) convertView.findViewById(R.id.etQty);
holder.parentRL = (LinearLayout) convertView.findViewById(R.id.parentRL);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ProductListBean tempBean = productList.get(position);
holder.etQty.setId(position);
final String productName = tempBean.getProductName();
final String productCode = tempBean.getProductCode();
final String productShortCode = tempBean.getProductShortCode();
if (mSelectedProd != null && mSelectedProd.contains(productCode)) {
final int indexOfProd = mSelectedProd.indexOf(productCode);
holder.etQty.setText(mSelectedProducts.get(indexOfProd).getEnteredQty());
}
holder.tvProductName.setText(productName);
holder.tvProdShortCode.setText(productShortCode);
holder.tvLastOrderedQty.setText(tempBean.getLastOrderedQty());
holder.etQty.setText(tempBean.getEnteredQty());
holder.parentRL.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.etQty.requestFocus();
}
});
holder.etQty.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
final int pos = holder.etQty.getId();
final String qty = holder.etQty.getText().toString();
if (qty.length() > 0) {
if (String.valueOf(qty.charAt(0)).equals("0")) {
holder.etQty.setError("Invalid Quantity");
holder.etQty.setText("");
} else {
productList.get(pos).setEnteredQty(holder.etQty.getText().toString());
}
} else {
productList.get(pos).setEnteredQty("");
}
}
});
return convertView;
}
static class ViewHolder {
TextView tvProductName, tvProdShortCode, tvLastOrderedQty;
EditText etQty;
LinearLayout parentRL;
}