【问题标题】:list adapter shows null on debug android列表适配器在调试 android 上显示 null
【发布时间】:2017-12-05 07:11:46
【问题描述】:

我有一个列表视图,可以从 Common 类的静态数组列表中填充一些数据。列表适配器在调试时显示为空。我正在使用 ArrayAdapter,我在堆栈上看到了很多解决方案,但仍然是同样的问题。请帮助我

我调用适配器的代码

    displayListAdapter = new DisplayListAdapter(this);
    listView_contacts.setAdapter(displayListAdapter);

我的适配器类

public class DisplayListAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private Context context;
FragmentTransaction ft;
int clickedPosition;
String displayContacts,displayContactNumber;
private WebServiceCoordinator mWebServiceCoordinator;
TextView name_contactList;
Button call_contact;

public DisplayListAdapter(Context context) {
    super(context, R.layout.contacts_list1_row);
    this.mInflater = LayoutInflater.from(context);
    this.context = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.contacts_list1_row, parent, false);

    name_contactList = (TextView) rowView.findViewById(R.id.contactlist_name);
    call_contact = (Button) rowView.findViewById(R.id.contactlist_call);
    try {
        //if(Common.FLAG_contactsLoaded)
        //{
        displayContacts = Common.selectedContactName.get(position);
        displayContactNumber = Common.selectedContactNos.get(position);
        //}
            /*else {
                displayContacts = ((DisplayContacts) context).selectedContactName.get(position);
                displayContactNumber = ((DisplayContacts) context).selectedContactNos.get(position);
            }*/
        //String displayContacts = Common.selectedContactName.get(position);
        //String displayContactNumber = Common.selectedContactNos.get(position);
        Toast.makeText(context, "name to display" + displayContacts, Toast.LENGTH_SHORT).show();
        Toast.makeText(context, "no.to display" + displayContactNumber, Toast.LENGTH_SHORT).show();
        //name_contactList.setText(((DisplayContacts) context).selectedContactName.get(position));
        name_contactList.setText(displayContacts);
        //name_contactList.setText("dummy text");
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.i("populating error",e.getLocalizedMessage());
    }
    return rowView;
}

}

【问题讨论】:

    标签: android listview android-arrayadapter


    【解决方案1】:

    您是否向适配器添加了数据?我没有看到任何与之相关的代码。
    你应该这样写:

    displayListAdapter = new DisplayListAdapter(this);  
    displayListAdapter.add("1");  
    displayListAdapter.add("2"); 
    

    或使用

    super(context, R.layout.contacts_list1_row, "your data list")
    

    构造函数代替

    super(context, R.layout.contacts_list1_row);  
    

    希望能有所帮助:)

    【讨论】:

    • 数据正在使用 SplashActivity 中的公共类添加。您可以在适配器 Common.selectedContactNos.get(position); 中看到它;
    • @user8601021 我明白了,但这还不够,您必须在数据和适配器之间建立连接,以便您必须“添加”或使用正确的构造函数,否则永远不会调用 getView 函数适配器首先不知道数据计数。
    • 我在构造函数中添加了一个虚拟数组列表,它可以工作我没有从数组列表中获取联系人姓名的正确位置和相应的获取的联系人号码。所以请帮助我在构造函数中添加两个数组列表
    • @user8601021 “存储联系人姓名和号码”的列表是您应该添加到构造函数中的确切列表,而不是其他虚拟数组列表兄弟。我们只使用一个列表来存储数据并将所有内容(id、数字等)作为单个列表项放入一个类对象中。然后我们把这个列表放到构造函数中,位置就可以了。或者你把所有的 Common 类都贴出来,我帮你修?
    • @user8601021 或者您只需要添加存储联系人姓名的列表即可,如果您确认联系人姓名列表的大小和联系人号码列表的大小相同。
    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多