Integer value in TextView
TextView tv = new TextView(this);
tv.setText("" + 4);
I am not able to show integer value with textview in adapter
Try below code:
Model Class Category.java
public class Category {
int id,newss;
String name,banner,description;
public Category(int id, int newss, String name, String banner, String description) {
this.id = id;
this.newss = newss;
this.name = name;
this.banner = banner;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNewss() {
return newss;
}
public void setNewss(int newss) {
this.newss = newss;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Adapter Class CategoryListAdapter.java
public class CategoryListAdapter extends RecyclerView.Adapter<CategoryListAdapter.ViewHolder> implements Filterable {
private final int mBackground;
private List<Category> original_items = new ArrayList<>();
private List<Category> categorylist = new ArrayList<Category>();
private ItemFilter mFilter = new ItemFilter();
private final TypedValue mTypedValue = new TypedValue();
private Context ctx;
private ImageLoader imgloader=new ImageLoader();
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView name;
public TextView news;
public ImageView image;
public CardView lyt_parent;
public ViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.name);
image = (ImageView) v.findViewById(R.id.image);
news = v.findViewById(R.id.newss);
lyt_parent = (CardView) v.findViewById(R.id.lyt_parent);
}
}
public Filter getFilter() {
return mFilter;
}
public CategoryListAdapter(Context ctx, List<Category> items) {
this.ctx = ctx;
original_items = items;
categorylist = items;
ctx.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
}
@Override
public CategoryListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
v.setBackgroundResource(mBackground);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Category c = categorylist.get(position);
holder.name.setText(c.getName());
holder.news.setText(String.valueOf(c.getNewss()));
imgloader.displayImage(Constant.getURLimgCategory(c.banner), holder.image);
holder.lyt_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(ctx, ActivityCategoryDetails.class);
i.putExtra(ActivityCategoryDetails.EXTRA_OBJCT, c);
ctx.startActivity(i);
}
});
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return categorylist.size();
}
@Override
public long getItemId(int position) {
return position;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String query = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Category> list = original_items;
final List<Category> result_list = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++) {
String str_title = list.get(i).getName();
String str_newss = String.valueOf(list.get(i).getNewss());
if (str_title.toLowerCase().contains(query) || str_newss.toLowerCase().contains(query)) {
result_list.add(list.get(i));
}
}
results.values = result_list;
results.count = result_list.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
categorylist = (List<Category>) results.values;
notifyDataSetChanged();
}
}
}
Code in Activity
CategoryListAdapter categoryListAdapter=new CategoryListAdapter(this,categoryArrayList);
recyclerViewMain.setLayoutManager(new LinearLayoutManager(this));
recyclerViewMain.setAdapter(categoryListAdapter);
The main cause of crash is your model class
I hope it works for you
Set Text to Integer Value
Generally,
tv.setText(String.valueOf(int));
If the value is inside another class? You can make a getter for the value you want in that class:
public int getValue() {
return value;
}
So that you can access it from the other one:
BUT if you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.
tv.setText(String.valueOf(theOtherClassInstance.getValue()));
EDIT If your int is firstResult
as per your comment below, then the getter becomes:
public int getFirstResult() {
return firstResult;
}
Add or Subtract the Integer Value of TextView using Number Picker in android
readingPicker.setValueChangedListener(new ValueChangedListener() {
@Override
public void valueChanged(int value, ActionEnum action) {
switch (action) {
case INCREMENT:
double currentValue = Double.parseDouble(pointsTextView.getText().toString());
currentValue = currentValue + 0.5;
pointsTextView.setText(String.valueOf(currentValue));
break;
case DECREMENT:
double currentValue1 = Double.parseDouble(pointsTextView.getText().toString());
currentValue1 = currentValue1 - 0.5;
pointsTextView.setText(String.valueOf(currentValue1));
}
}
});
This way you can achieve the desired result. Getting current value of pointsTextView
and then doing add or subtract operation on it and again, setting that value to the pointsTextView
.
Setting an integer value in textView shows null, How to solve this in android?
As your code posted and comments makes me assuming that you are not setting text into TextView inside listener. So I suggest you to put
valueClaimed.setText(String.valueOf(claimedNos));
inside onDataChange
method like this
public int claimedNos=0;
uDatabase.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
claimedNos = dataSnapshot.child("Claimed").getValue(Integer.class);
Log.d("TAG","claimed nos in account :"+claimedNos);
valueClaimed.setText(String.valueOf(claimedNos));
}
Your code is setting value before listner listen dataChange event and it was making null and zero so now above code will set claimedNos after receiving it from firebase,
Display automatically an Integer value from Edit Text (entered by the user) to Text View without any actions (such as onButtonClick) in Android
Just use textwatcher and put the logic in it. Just make sure change the id of edittext
and textview
et.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) {
String value = s.toString();
double outputValue = Integer.parseInt(value) * 0.05;
textView.setText(String.valueOf(outputValue));
}
});
Make sure in edittext that it only capture the numerical value.
How do I convert the value of a TextView to integer
First, The conversion is NOT from textview
to int
. textView aint a datatype. If you want the result in int use parseInt()
method. int i = Integer.parseInt(value)
.
[EDIT]
Try this, this will work.
TextView tv = (TextView) findViewById(R.id.tv);
EditText et = (EditText) findViewById(R.id.et);
try {
double d = Double.valueOf(String.valueOf(et.getText()));
tv.setText(String.valueOf( (int) d));
}catch (NumberFormatException e){
Toast.makeText(MainActivity.this, "Enter valid number.", Toast.LENGTH_SHORT).show();
}
I have simplified it for your understanding. The code can still be optimized.
Related Topics
The Import Com.Google.Android.Gms Cannot Be Resolved
How to Include a Username When Storing Email and Password Using Firebase (Baas) in an Android App
Manifest Merger Failed Targeting Android 12
Android: How to Check If the Server Is Available
Android:Maximum Allowed Width & Height of Bitmap
Ongloballayoutlistener: Deprecation and Compatibility
My Current Location Always Returns Null. How to Fix This
Avoid Reloading Activity with Asynctask on Orientation Change in Android
How to Change the Package Name of an Android App on Google Play
Android Kitkat Securityexception When Trying to Read from Mediastore
Android Share Images from Assets Folder
Best Practices: Layouts on Android (Programmatic VS Xml)
Findviewbyid() Returns Null When I Call It in Oncreate()
What File System Path Is Used by Android's Context.Openfileoutput()