通过ListItem.add绘制列表视图始终返回position = 0

时间:2017-02-22 17:56:19

标签: android listview

我有一个包含3个元素的数组,我尝试在列表视图中绘制。问题是它只绘制第一个条目,因为getView总是返回一个位置= 0。

为什么?我做错了什么?

我的主要java(片段):

public class PSGlobalFragment extends Fragment {
List<PSGitem> listPSGitem;
ListView list;
PSGadaptater psgAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {  
    return inflater.inflate(R.layout.psglobal, container, false);
}

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    String ip;

    listPSGitem = new ArrayList<>();
    psgAdapter = new PSGadaptater(getActivity(), listPSGitem);

    listPSGitem.clear();
    StoreDevDiscovery store = new StoreDevDiscovery();
    // this is where I store the data  
    int count = store.getMax();

    for(int i=0;i<count;i++){
        ip = store.getIPDiscovery(i);

        PSGitem item = new PSGitem();
        item.setIp(ip);

        listPSGitem.add(item);
        list.setAdapter(psgAdapter);

    }
}

和我的适配器:

public class PSGadaptater extends BaseAdapter {
    private int size = 0;
    private List<PSGitem> listIp;
    private LayoutInflater layoutInflater;
    Context context;

    public PSGadaptater(Context c, List<PSGitem> objects) {
        context = c;
        listIp = objects;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void notifyDataSetChanged() {
        size = listIp.size();
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listIp.size();
    }

    public Object getItem(int position) {
        return listIp.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    private class ViewIPHolder {
        TextView ip_psg;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewIPHolder viewHolder;

        if(convertView == null) {
            viewHolder = new ViewIPHolder();

            convertView = layoutInflater.inflate(R.layout.listview_item_psg, null);

            viewHolder.ip_psg = (TextView) convertView.findViewById(R.id.ipaddr_psg);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewIPHolder) convertView.getTag();
        }

        viewHolder.ip_psg.setText(listIp.get(position).getIpaddr());
  // position always = 0 this is my issue    
        return convertView;
    }

}

和PSCitem.java:

public class PSGitem {
    private String ip1;
    public String getIp(){
        return ip1;
    }

    public void setIp(String ip){
        ip1 = ip;
    }
}

2 个答案:

答案 0 :(得分:1)

问题是您是从一组空项目创建适配器:

listPSGitem = new ArrayList<>();
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);

如果您希望稍后将项添加到适配器,则应将这些项添加到适配器listIp列表变量中,然后使用notifyDataSetChanged()方法让适配器了解此更改。

答案 1 :(得分:0)

更改您的onActivityCreated方法,如下所示。

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

String ip;

listPSGitem = new ArrayList<>();


listPSGitem.clear();
StoreDevDiscovery store = new StoreDevDiscovery();
// this is where I store the data  
int count = store.getMax();

for(int i=0;i<count;i++){
    ip = store.getIPDiscovery(i);

    PSGitem item = new PSGitem();
    item.setIp(ip);

    listPSGitem.add(item);


}
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
list.setAdapter(psgAdapter);
}