如何使用 UsersAdapter 将数据设置到列表视图中?

How to set the data into listview by using the UsersAdapter?

我正在 Couchbase lite 中开发应用程序 database.My 问题是当我从 couchbase lite 数据库查询数据并通过 ArrayAdapter 将数据设置到列表视图时,数据如下所示 image.The ArrayAdapter代码是

  CouchDb couchDb = new CouchDb(getApplicationContext());
    ArrayList<ContactData> data = couchDb.getData();
    ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_simple,R.id.textname,data);
    list.setAdapter(adapter);

为了解决这个问题,我们创建了自定义 UsersAdapter class

public class UsersAdapter extends ArrayAdapter<ContactData>{

    public UsersAdapter(MainActivity mainActivity, ArrayList<ContactData> data) {
        super(mainActivity,R.layout.list_simple,data);
    }

    public class ViewHolder {
        TextView name;
    }

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

        ContactData contactData = getItem(position);
        ViewHolder viewHolder;

        if(convertView == null)
        {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_simple, parent, false);
            viewHolder.name = (TextView) convertView.findViewById(R.id.textname);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.name.setText(contactData.getNames());

        return convertView;
    }
}

在上面的adapter这一行出现空指针异常viewHolder.name.setText(contactData.getNames());

这是联系人数据 java class

public class ContactData {

String names;

public ContactData(String names) {
    this.names = names;
}

public ContactData()
{

}

public String getNames() {
    return names;
}

public void setNames(String names) {
    this.names = names;
}

}

请帮助我如何解决这个问题,并告诉我为什么值在 UsersAdapter 中返回 null 的原因。

发生的异常是

 E/AndroidRuntime: FATAL EXCEPTION: main                                                                           Process: com.seyali.couchbasedatabase2, PID: 5641                                                                           java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.seyali.couchbasedatabase2.MainActivity$UsersAdapter$ViewHolder.name' on a null object reference
                                                                             at com.seyali.couchbasedatabase2.MainActivity$UsersAdapter.getView(MainActivity.java:107)
                                                                             at android.widget.AbsListView.obtainView(AbsListView.java:2363)

您已经创建了 UsersAdapter,但没有在任何地方使用它。像这样使用它:

CouchDb couchDb = new CouchDb(getApplicationContext());
ArrayList<ContactData> data = couchDb.getData();
UsersAdapter adapter = new UsersAdapter(this, data);
list.setAdapter(adapter);

NullPointerException 是因为项目为空。尝试这样做:

private ArrayList<ContactData> data;

public UsersAdapter(MainActivity mainActivity, ArrayList<ContactData> data) {
    super(mainActivity,R.layout.list_simple,data);
    this.data = data;
}

然后在 getView 方法中:

ContactData contactData = data.get(position);

希望对您有所帮助!!!

使用这个:

private ArrayList<ContactData> data;
private Context context;

public UsersAdapter(Context contect, ArrayList<ContactData> data) {
this.context=context;
this.data = data;
}


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

    ContactData contactData = data.get(position);
    ViewHolder viewHolder;

    if(convertView == null)
    {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.list_simple, parent, false);
        viewHolder.name = (TextView) convertView.findViewById(R.id.textname);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.name.setText(contactData.getNames());

    return convertView;
}