ListView适配器无法应用于Modal类

时间:2017-04-25 00:36:28

标签: android firebase firebase-realtime-database

当我尝试将适配器放入listView时出现错误。它不能应用于我的模态类,即UserInformation.java。我该如何解决这个问题?

错误:mlistView.setAdapter(adapter)

public class FindDonorActivity extends AppCompatActivity {


ListView mlistView;
DatabaseReference databaseReference;
List<UserInformation> userList;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_donor);

    databaseReference = FirebaseDatabase.getInstance().getReference("user");

    mlistView = (ListView) findViewById(R.id.mlistView);

    userList = new ArrayList<>();
}

@Override
protected void onStart() {
    super.onStart();

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            userList.clear();

            for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                UserInformation user = userSnapshot.getValue(UserInformation.class);
                userList.add(user);



            }


            UserInformation adapter = new UserInformation(FindDonorActivity.this, userList);
            mlistView.setAdapter(adapter);

        }



        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}



}

用户信息.java

public class UserInformation {

        public String useridnumber;
        public String name;
        public String email;
        public String state;
        public String bloodgroup;
        public String lastdonated;

        public UserInformation(FindDonorActivity findDonorActivity, List<UserInformation> userList){


        }

        public UserInformation (String useridnumber, String name, String email, String state, String bloodgroup, String lastdonated){

            this.useridnumber = useridnumber;
            this.name = name;
            this.email = email;
            this.state = state;
            this.bloodgroup = bloodgroup;
            this.lastdonated = lastdonated;

        }

    public String getUseridnumber() {

        return useridnumber;
    }



    public String getBloodgroup() {
        return bloodgroup;
    }

    public String getState() {
        return state;
    }

    public String getLastdonated() {
        return lastdonated;
    }

    public String getEmail() {
        return email;
    }
}

更新:这是我的适配器类

public class UserList extends ArrayAdapter<UserInformation> {

private Activity context;
private List<UserInformation> userList;

public UserList (Activity context, List<UserInformation> userList){
super(context, R.layout.list_layout, userList);
this.context = context;
this.userList = userList;

}

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

    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

    TextView tvbloodlist = (TextView) listViewItem.findViewById(R.id.tvbloodlist);
    TextView tvstatelist = (TextView) listViewItem.findViewById(R.id.tvstatelist);
    TextView tvdonatedlist = (TextView) listViewItem.findViewById(R.id.tvdonatedlist);
    TextView tvemaillist = (TextView) listViewItem.findViewById(R.id.tvemaillist);

    UserInformation user = userList.get(position);

    tvbloodlist.setText(user.getBloodgroup());
    tvstatelist.setText(user.getState());
    tvdonatedlist.setText(user.getLastdonated());
    tvemaillist.setText(user.getEmail());

    return listViewItem;
}

}

3 个答案:

答案 0 :(得分:0)

您需要使用ArrayAdapter并将ArrayListUserInformation个对象传递给它。 UserInformation对象不是ArrayAdapter。这应该有帮助 - ArrayAdapter

编辑 - 您不应该将FindDonorActivity的实例传递给UserInformation对象。您的UserInformation不应该引用任何Activity

答案 1 :(得分:0)

UserInformation adapter = new UserInformation(FindDonorActivity.this,userList);             mlistView.setAdapter(适配器); 这不是一个适配器,这是您需要为lisview创建自定义适配器的模型类。

喜欢这个。

公共类BookAdapter扩展了BaseAdapter {

private Context context;
private ArrayList<Books> values;
private static LayoutInflater inflater = null;

public BookAdapter(Context context, ArrayList<Books> values) {
    // super(context, R.layout.listitem, values);
    this.context = context;
    this.values = values;

    inflater = (LayoutInflater)context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    Log.e(">>Inpost value<<", values + "");
    // mLibraryCtrl.getBookList().size()+"");
}

static class ViewHolder {
    TextView mCompanyBookTxt;
    ImageView image;
    ImageView info;

    TextView name, desc;
    LinearLayout listlayout,gridlayout;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(values!=null && values.size()>0)
    return values.size();
    else
    return 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return values.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View rowView = convertView;
    ViewHolder holder;
    if (rowView == null) {
        holder = new ViewHolder();
        rowView = inflater.inflate(R.layout.grid_list_view, parent, false);
        holder.mCompanyBookTxt = (TextView) rowView.findViewById(R.id.txt);
        holder.image = (ImageView) rowView.findViewById(R.id.image);
        holder.info = (ImageView) rowView.findViewById(R.id.info);

        holder.name = (TextView) rowView.findViewById(R.id.name);
        holder.desc = (TextView) rowView.findViewById(R.id.desc);

        holder.listlayout = (LinearLayout) rowView.findViewById(R.id.listlayout);
        holder.gridlayout = (LinearLayout) rowView.findViewById(R.id.gridlayout);

        rowView.setTag(holder);
    } else {

        holder = (ViewHolder) rowView.getTag();

    }

holder.name .setText(values.get(position).getName());

答案 2 :(得分:0)

你有什么模型(Pojo)普通旧Java对象, 你需要的是一个BaseAdapter或ArrayAdapter的适配器, 我希望这个例子给你留下,并为你提供问候。如果您有疑问,请告诉我 //我的模特

public class HistorialModel {

    private List<DatosHistorial> datosHistorials;
    private boolean estatus;
    private int codigo;
    private String mensaje;


    public HistorialModel(List<DatosHistorial> datosHistorials, boolean estatus, int codigo, String mensaje) {
        this.datosHistorials = datosHistorials;
        this.estatus = estatus;
        this.codigo = codigo;
        this.mensaje = mensaje;
    }

    public List<DatosHistorial> getDatosHistorials() {
        return datosHistorials;
    }

    public void setDatosHistorials(List<DatosHistorial> datosHistorials) {
        this.datosHistorials = datosHistorials;
    }

    public boolean isEstatus() {
        return estatus;
    }

    public void setEstatus(boolean estatus) {
        this.estatus = estatus;
    }

    public int getCodigo() {
        return codigo;
    }

    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    public String getMensaje() {
        return mensaje;
    }

    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

    public static class DatosHistorial {
        private int idPedido;
        private String logHistorial;


        public DatosHistorial(int idPedido, String logHistorial) {
            this.idPedido = idPedido;
            this.logHistorial = logHistorial;
        }

        public int getIdPedido() {
            return idPedido;
        }

        public void setIdPedido(int idPedido) {
            this.idPedido = idPedido;
        }

        public String getLogHistorial() {
            return logHistorial;
        }

        public void setLogHistorial(String logHistorial) {
            this.logHistorial = logHistorial;
        }

    }


}

//我的适配器

public class AdapterHistorial extends ArrayAdapter<HistorialModel.DatosHistorial> {
    private TextView textView;
    private Context context;

    public AdapterHistorial(Context context, List<HistorialModel.DatosHistorial> datosHistorials) {
        super(context, 0, datosHistorials);
    }

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


        LayoutInflater inflater =
                (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rootView = convertView;
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.item_historial, viewGroup, false);
        }

        textView = (TextView) rootView.findViewById(R.id.logHistorial);
        HistorialModel.DatosHistorial historial = getItem(position);
        textView.setText(historial.getLogHistorial());
        return rootView;
    }

}

//我的项目资源xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <LinearLayout
         android:padding="10dp"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

         <TextView
             android:id="@+id/logHistorial"
             android:textColor="@color/negro"
             android:layout_width="match_parent"
             android:text="4202 - 18 Mar 2017 - $25.00."
             android:layout_height="wrap_content"/>
     </LinearLayout>
</LinearLayout>