简体   繁体   English

Android中的textview字符串在自定义适配器中未像以前那样显示

[英]String to textview in Android not displaying like it did before within custom adapter

I am using the same custom adapter format that I used in one of my other apps. 我正在使用与其他应用程序之一相同的自定义适配器格式。 I only changed the string references but I am getting the following error when running the app: 我只更改了字符串引用,但在运行应用程序时遇到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I've been trying to wrap my brain around this but I can't figure out what the problem is. 我一直在努力解决这个问题,但我不知道问题出在哪里。 Everything is exactly the same as in the other app but for some reason doesn't work in this one. 一切都与另一个应用程序完全相同,但由于某种原因在此应用程序中不起作用。 Everything is exactly the same. 一切都完全一样。 The only difference is the database table that the data is being drawn from. 唯一的区别是从中提取数据的数据库表。 Here's my getView that is producing the error: 这是我的getView产生错误:

public class Holder
{
    TextView title;
    TextView description;
    NetworkImageView img;
    RatingBar rating;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.product_row, null);
    holder.title=(TextView) rowView.findViewById(R.id.productName);
    holder.img=(NetworkImageView) rowView.findViewById(R.id.productPic);
    holder.rating=(RatingBar)rowView.findViewById(R.id.ratingBar2);
    holder.title.setText(result.get(position).getName());
    holder.rating.setRating(Float.valueOf(result.get(position).getRating().toString()));

    // If you are using NetworkImageView
    holder.img.setImageUrl(result.get(position).getImageURL(), VolleyController.getInstance().getImageLoader());

    return rowView;
}

My row data/model is exactly the same too. 我的行数据/模型也完全相同。 The line that is giving the error is: 给出错误的行是:

holder.title.setText(result.get(position).getName());

In my RowData model getName is like this: 在我的RowData模型中,getName是这样的:

public String getName(){
    return name;

Replace yours code with these lines of code . 用这些代码行替换您的代码。 In yours code you are not using the View convertView of getView() method of BaseAdapter instead of it, you used your own View that is rowView ,therefore you are facing these problem.So use the View of the getView() method of adapter class 在您的代码中,您没有使用BaseAdaptergetView()方法的View convertView而是使用了自己的View ,即rowView ,因此遇到了这些问题。因此请使用adapter类的getView()方法的View

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    Holder holder=new Holder();

    if (convertView == null) {

     convertView = inflater.inflate(R.layout.product_row, null);
     holder.title=(TextView) convertView.findViewById(R.id.productName);
     holder.img=(NetworkImageView) convertView.findViewById(R.id.productPic);
     holder.rating=(RatingBar)convertView.findViewById(R.id.ratingBar2);

     convertView.setTag(holder);
   }
   else
   {
       holder = (ViewHolder) convertView.getTag();
   }
       holder.title.setText(result.get(position).getName());
       holder.rating.setRating(Float.valueOf(result.get(position).getRating().toString()));


    // If you are using NetworkImageView
     holder.img.setImageUrl(result.get(position).getImageURL(), VolleyController.getInstance().getImageLoader());

    return convertView;
}

First of all I ask you the question why you used View Holder . 首先,我问您一个为什么使用View Holder的问题。 because It is the wrong/wrost implementation of view Holder . 因为这是view Holder的错误/错误实现。 please read this blog it will help you to understand the view holder pattern https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html 请阅读此博客,它将帮助您了解view holder模式https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html

And please check the id of TextView in product_row.xml. 并且请检查product_row.xmlTextView的ID I thought its just a misspelled id of TestView . 我认为这只是TestView的拼写错误ID。

Use the View reference from the getView() do not create separate View . getView()使用View引用不要创建单独的View

Use 采用

 `holder.title=(TextView) convertView.findViewById(R.id.productName);`

it may solve the problem 它可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM