Set Text to Integer Value

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;
}

how to set text an integer and get int without getting error

TextView text1 = (TextView) findViewById(R.id.calories);
text1.setText(""+productCalories);

Or

text1.setText(String.valueOf(productCalories));

TextView.setText(int) causes app to crash

Yes, use String.valueOf(), like this:

private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));
}

Integer value in TextView

TextView tv = new TextView(this);
tv.setText("" + 4);

add value and insert into setText

I won't have much stress on errors as it's already covered by previous answers. But here you may find the correct solution by commenting out your line of codes that have problems with some explanation of the issues, and writing directly below of each line the corrected code

TextView pvoce = (TextView) findViewById(R.id.pvoce);
TextView papp = (TextView) findViewById(R.id.papp);

if (escolhaApp == "pedra" && escolhaUsuario == "tesoura" || escolhaApp == "papel" && escolhaUsuario == "pedra" || escolhaApp == "tesoura" && escolhaUsuario == "papel") {
textoResultado.setText(" You Lose ");
// String soma = 1; // wrong as java is statically typed language, you can't assign a int into a String
int soma = 1;

// int totalapp = (soma + papp); // papp is of TextView type, you need to get its text by getText(), convert it to String by .toString(), and then into int to make the integer addition valid
int totalapp = soma + Integer.parseInt(papp.getText().toString());

String totalappString = Integer.toString(totalapp);
papp.setText(totalappString);

} else if (escolhaUsuario == "pedra" && escolhaApp == "tesoura" || escolhaUsuario == "papel" && escolhaApp == "pedra" || escolhaUsuario == "tesoura" && escolhaApp == "papel") {
textoResultado.setText(" You Win ");
int soma = 1;
// int totalvoce = soma + pvoce; // pvoce is of TextView type, you need to get its text by getText(), convert it to String by .toString(), and then into int to make the integer addition valid
int totalvoce = soma + Integer.parseInt(pvoce.getText().toString());

String totalvoceString = Integer.toString(totalvoce);
pvoce.setText(totalvoceString);
}

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



Related Topics



Leave a reply



Submit