Я пытаюсь отобразить целочисленное значение в текстовом представлении, но оно постоянно создает sh мое android приложение, вот содержимое
Json
[
{
"id":8,
"name":"Recruitment",
"banner":"category_CAT1.jpg",
"description":"Job Recruitment Post.",
"newss":4
}
]
В адаптере
public TextView news;
public ViewHolder(View v) {
super(v);
news = v.findViewById(R.id.newss);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Category c = categorylist.get(position);
holder.news.setText(c.getnewss());
}
});
}
И в модуле
Integer newss;
public Integer getnewss() {
return newss;
}
public void setnewss(Integer newss) {
this.newss = newss;
}
ОШИБКА ЕСТЬ
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
04-05 14:39:34.575 16109 16109 E AndroidRuntime at com.androbaron.dailynews.adapter.CategoryListAdapter.onBindViewHolder(CategoryListAdapter.java:81)
04-05 14:39:34.575 16109 16109 E AndroidRuntime at com.androbaron.dailynews.adapter.CategoryListAdapter.onBindViewHolder(Unknown Source:8)
Вот текстовое представление в макете
<RelativeLayout>
<TextView
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="@color/secondary_text"
android:id="@+id/newss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30.0dip"/>
Заранее спасибо.
Редактировать отсюда
Адаптер. 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 = ImageLoader.getInstance();
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.name);
holder.news.setText("" + c.newss);
//holder.news.setText(String.valueof(c.getnewss()));
// holder.news.setText(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).name;
String str_newss = list.get(i).newss;
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();
}
}
}
Макет здесь
<?xml version="1.0" encoding="utf-8"?>
<com.balysv.materialripple.MaterialRippleLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/RippleStyleBlack"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="@+id/lyt_parent"
android:layout_width="match_parent"
android:layout_height="90.0dip"
android:layout_margin="@dimen/spacing_medium"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4.0dip"
app:cardElevation="2.0dip">
<LinearLayout
android:layout_width="80.0dip"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:background="@color/Black"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"/>
</LinearLayout>
<LinearLayout
android:gravity="center_vertical"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="100.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textStyle="normal"
android:textColor="@color/primary_text"
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sample Title"
android:layout_weight="1.0"/>
<RelativeLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="25.0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/spacing_mlarge"
android:layout_height="@dimen/spacing_mlarge"
android:src="@drawable/ab_news_small"
android:tint="@android:color/black"/>
<TextView
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="@color/secondary_text"
android:id="@+id/newss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30.0dip"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</com.balysv.materialripple.MaterialRippleLayout>
И адаптер, и его макет здесь и сейчас
Игнорировать это
Я пытаюсь отобразить целочисленное значение в текстовом представлении, но оно постоянно превращается в sh мое android приложение, вот содержимое