Activity 使用 Listview 不显示项目

Activity with Listview does not show the items

我有 activity "VerSolicitudesPendientesActivity.java" 当我 select 主菜单的一项时加载。 此 activity 包含一个不显示列表的列表视图,但包含 TextView。 这是 activity:

public class VerSolicitudesPendientesActivity extends Activity {

/* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.lista_solicitudes);
    System.out.println("OncreateSolicitudes");
    System.out.println("Contenido enlazado: Solicitudes.");
    Bundle bundle= getIntent().getExtras();
    System.out.println("Extras obtenidos");

    String usuario= bundle.getString("usuario");
    System.out.println("Usuario que revisa solicitudes: "+ usuario);

    cargaSolicitudes(usuario);
    System.out.println("Accediendo a solicitudes");


}
public void cargaSolicitudes(String usuario){
    System.out.println("Llegamos a carga solicitudes.");
    ListView lista= (ListView) findViewById(R.id.listaSolicitudes);
    //System.out.println("Lista obtenida del xml.");

    ArrayList<Solicitante> arraySolicitudes= new ArrayList<Solicitante>();
    Solicitante solicitante;
    //System.out.println("Antes de cargar solicitudes (Solicitantes)");

    //Introduccion de datos en el array. Aqui es donde se deben cargar los datos de la BB.DD
    solicitante= new Solicitante("Pablo","zz@zz",getResources().getDrawable(R.drawable.fotoamigo));
    arraySolicitudes.add(solicitante);
    //System.out.println("Solicitante 1 cargado "+ solicitante.nombreSolicitante);

    solicitante= new Solicitante("Jon","zz@yy",getResources().getDrawable(R.drawable.fotoamigo2));
    arraySolicitudes.add(solicitante);
    //System.out.println("Solicitante 2 cargado "+ solicitante.nombreSolicitante);

    System.out.println("Solicitudes cargadas en adapter");

    // Creamos el adapter
    SolicitudesAdapter adapter= new SolicitudesAdapter(this,arraySolicitudes,getBaseContext());
    // Una vez hecha la conexión pasamos los datos.
    lista.setAdapter(adapter);

}
public void aceptaSolicitud(View v){
    Solicitante solicitante= (Solicitante) v.getTag();
    Toast.makeText(getBaseContext(), "Solicitud de "+solicitante+" ha sido aceptada.", Toast.LENGTH_SHORT).show();


}
public void rechazaSolicitud(View v){
    Solicitante solicitante= (Solicitante) v.getTag();
    Toast.makeText(getBaseContext(), "Solicitud de "+solicitante+" ha sido rechazada.", Toast.LENGTH_SHORT).show();

}

}

这是我的自定义适配器:

public class SolicitudesAdapter extends BaseAdapter {

protected Activity activity;
protected ArrayList<Solicitante> solicitantes;
protected Context mContext;
public SolicitudesAdapter(Activity activity, ArrayList<Solicitante> solicitantes,Context context){
    this.activity= activity;
    this.solicitantes= solicitantes;
    this.mContext=context;
}
public Context getmContext() {
    return mContext;
}
public void setmContext(Context mContext) {
    this.mContext = mContext;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
@SuppressLint({ "ViewHolder", "InflateParams" })
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inf=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v= inf.inflate(R.layout.lista_solicitudes,null);
    //Asociar el layout de la lista que hemos creado.
    //Definimos un objeto a partir del array, vamos a cargar el contenido
    //de ese objeto en el view de la lista.
    System.out.println("Dentro del adapter");

    Solicitante soli= solicitantes.get(position);
        //Cargamos la fotografía del amigo.
        ImageView foto= (ImageView)v.findViewById(R.id.imagenSolicitud);
        foto.setImageDrawable(soli.getFoto());
        System.out.println("Imagen botonAmigo cargada en la view");
        //Cargamos el nombre del evento
        TextView nombre= (TextView)v.findViewById(R.id.textoSolicitud);
        String cadena =soli.getNombreSolicitante()+" quiere ser tu amigo.";
        nombre.setText(cadena);
        Button botonAcepta= (Button)v.findViewById(R.id.botonAceptaSolicitud);
        botonAcepta.setTag(soli);
        Button botonRechaza= (Button)v.findViewById(R.id.botonRechazaSolicitud);
        botonRechaza.setTag(soli);
        System.out.println("Hemos adaptado a "+ soli.nombreSolicitante);


    return v;
}

这是 xml 包含用于在 activity 中显示的列表视图和文本视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/pruebaTexto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Prueba de que se carga el contenido de la actividad." />

<ListView
    android:id="@+id/listaSolicitudes"
    android:layout_width="match_parent"    
    android:layout_height="match_parent"
    android:clickable="true"
    tools:listitem="@layout/solicitudlist" >
</ListView>

在此 xml 文件的图形模式下,我可以在 TextView 下方正确看到列表。 最后,这是列表中的项目。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/borde_naranja" >

<TextView
    android:id="@+id/textoSolicitud"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="16dp"
    android:layout_marginTop="15dp"
    android:text="@string/fraseSolicitud" />

<ImageView
    android:id="@+id/imagenSolicitud"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/textoSolicitud"
    android:layout_marginLeft="15dp"
    android:src="@drawable/fotoamigo" />

<Button
    android:id="@+id/botonAceptaSolicitud"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textoSolicitud"
    android:layout_centerHorizontal="true"
    android:background="@drawable/borde_rellenonaranja"
    android:onClick="aceptaSolicitud"
    android:text="@string/aceptar"
    android:textSize="12sp" />

<Button
    android:id="@+id/botonRechazaSolicitud"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/botonAceptaSolicitud"
    android:layout_alignBottom="@+id/botonAceptaSolicitud"
    android:layout_alignRight="@+id/textoSolicitud"
    android:layout_marginRight="16dp"
    android:background="@drawable/borde_rellenonaranja"
    android:onClick="rechazaSolicitud"
    android:text="@string/rechazar"
    android:textSize="12sp" />

我没有任何编译错误。当 activity 启动时,我可以看到 textView 但看不到 listView。该程序已重新修订,但我不知道哪个可能是问题所在。我希望在这里找到帮助。非常感谢。

您返回适配器中 ArrayList 的错误计数。此计数用于呈现视图。

在你的适配器中改变这个

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return solicitantes.size();
}