list adapter在调试android上显示null

时间:2017-12-05 07:11:46

标签: android listview android-arrayadapter

我有一个listview来填充类Common的静态arraylist中的一些数据。列表适配器在调试时显示null。我正在使用ArrayAdapter,我在堆栈上看到了很多解决方案,但仍然是同样的问题。请帮助我< / p>

我在调用适配器的代码

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

}

1 个答案:

答案 0 :(得分:0)

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

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);  
希望这可能会有所帮助:)