在设置自定义数组适配器

时间:2018-03-26 11:00:11

标签: android listview android-arrayadapter custom-adapter notifydatasetchanged

我有以下代码,即为用户在微调器中选择的元素获取Cursor。

HOperListAdapter selectedWorkersAdapter = new HOperListAdapter(this, R.layout.listoperacompanante, R.id.lblLaNombre, hopers);
    selectedWorkersAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    lvMnOperAcompanantes.setAdapter(selectedWorkersAdapter);

    if (m_TipoTrabajo == OPERARIO ) {
        spOeManoDeObra.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Object o = parent.getItemAtPosition(pos);
                selectedWorker = (o != null) ? (Cursor) o : null;
                btnAddOper.setEnabled(true);
            }     
        });
    }

当用户点击按钮以在spinner ::

上添加所选项目时
public void clickBtnAddOper(View v) {
    if(selectedWorker != null){
        HoperBE h = new HoperBE();
        h.operario = selectedWorker.getInt(COLUMN_OPERARIO);
        h.nom = selectedWorker.getString(COLUMN_NOMBRE);
        h.operario_v9 = selectedWorker.getString(COLUMN_OPERARIO_V9);
        if(!hopers.contains(h)){
            hopers.add(h);
            HOperListAdapter adapter = (HOperListAdapter) lvMnOperAcompanantes.getAdapter();
            adapter.addAll(hopers);
            //adapter.add(h);
            adapter.notifyDataSetChanged();
        }
    }
}

这是适配器的类:::

public class HOperListAdapter extends ArrayAdapter {

List<HoperBE> hopers;
Context context;
int layout, textToShow;

public HOperListAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List objects) {
    super(context, resource, textViewResourceId, objects);
    this.context = context;
    this.hopers = objects;
    this.layout = resource;
    this.textToShow = textViewResourceId;
}

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

    HOpersHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(context);

    if(convertView == null){
        convertView = inflater.inflate(layout, null);
        viewHolder = new HOpersHolder(convertView);
        convertView.setTag(viewHolder);
    }else{
        viewHolder = (HOpersHolder) convertView.getTag();
    }

    if(!hopers.isEmpty() && hopers != null){
        viewHolder.oper.setText(hopers.get(position).nom);
    }

    return convertView;
}

private class HOpersHolder{
    TextView oper;

    public HOpersHolder(View view){
        oper = (TextView) view.findViewById(textToShow);
    }
}

}

列表视图只添加了第一个元素,但没有更多..就像notifyDataSetChanged不起作用。

0 个答案:

没有答案