Acceder

Ver la Versión Completa : Problema listview.SetemptyView


rafaxplayer
01/09/14, 09:06:24
saludos gente , tengo un problema con un list que no consigo establecer un view para cuando no hay datos , ya lo he echo otras veces pero en este caso no encuentro porque no lo muestra aunque parece todo estar bien.

mi código:

this.listView = ((ListView) findViewById(R.id.listView1));
RelativeLayout ly = (RelativeLayout)findViewById(R.layout.empty_list);
this.listView.setEmptyView(ly);

Esto seria el layout que quiero utilizar:

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

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:src="@drawable/ic_nodata" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:text="No Data"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColorHint="@color/LightGrey"
android:textColorLink="@color/DarkGray"
android:textStyle="bold" />

</RelativeLayout>


y el adapter que aplico despues:

public class NewPostAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList<HashMap<String, String>> data;
private Context con;
static Typeface face;

public NewPostAdapter(Context context,
ArrayList<HashMap<String, String>> data) {
mInflater = LayoutInflater.from(context);
this.data = data;
this.con = context;

}

public HashMap<String, String> getItem(int position) {
return data.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {

holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_listview, null);

holder.txttitle = (TextView) convertView.findViewById(R.id.title);
holder.txtlastpostedname = (TextView) convertView
.findViewById(R.id.lastpostedname);
holder.txtnpostlast = (TextView) convertView
.findViewById(R.id.npostlast);
holder.txtnpostdate = (TextView) convertView
.findViewById(R.id.lastpostdate);
holder.txtforum = (TextView) convertView.findViewById(R.id.forum);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
// holder = (ViewHolder) convertView.getTag();
}
holder.txttitle.setText(data.get(position).get("title"));
// holder.txttitle.setTypeface(face);
holder.txtlastpostedname.setText("Author: "
+ data.get(position).get("postedname"));

holder.txtnpostlast.setText(data.get(position).get ("number"));
holder.txtnpostdate.setText(data.get(position).get ("datetime"));
holder.txtforum.setText(data.get(position).get("forum"));
return convertView;
}

static class ViewHolder {
TextView txttitle;
TextView txtlastpostedname;
TextView txtnpostlast;
TextView txtnpostdate;
TextView txtforum;
}

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

No se he probado de todo y no doy con el error, alguna sugerencia?

Dexafree
01/09/14, 11:00:28
Pues sobre el codigo que has puesto, varias cosas:

1.-

RelativeLayout ly = (RelativeLayout)findViewById(R.layout.empty_list);


Imagino que el layout que has posteado debajo es el empty_list.xml

Bien,así no es como se asigna un objeto de RelativeLayout.

Si te fijas, el método que utilizas es findViewById, y lo que tu le estás pasando es un layout, por tanto ahí es posible que no te lo encuentre.
Tendrías que ponerle un id al <RelativeLayout padre y entonces pasarle ese id

2.- La forma más fácil de tener un emptyView es, en el propio lugar donde tengas el ListView, poner otra View debajo (que será el emptyView) con android:visibility:GONE

Y cuando vayas a crear el adapter, si el arrayList tiene 0 elementos, simplemente tendrías que:

listView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);


Y ya está :ok:

kriogeN
01/09/14, 11:47:36
Lo que yo hago es si el getCount()==0 muestro el "empty view", obviamente uso getItemViewType y getViewTypeCount

Dexafree
01/09/14, 13:01:30
obviamente uso getItemViewType y getViewTypeCount

Para que necesitas saber el ViewType y el ViewTypeCount? :pensando:

Simplemente con el getAdapter().getCount() == 0 deberia bastarte, no?

kriogeN
01/09/14, 13:37:41
Porque hago un ViewType para cuando getCount es = 0.

Así cuando hago el getView se el Layout que debo inflar.

En el fondo es lo mismo, pero queda más limpio. Además que en los casos donde se me ha dado son como mínimo 3 tipos de fila, así aprovecho e introduzco el 4º tipo para la lista vacía.

rafaxplayer
07/09/14, 09:52:03
Bien ya lo solucione , gracias a todos

jmmunoz
08/09/14, 21:55:29
Bien ya lo solucione , gracias a todos

Si indicas como lo solucionaste ppdras ayudar a fututos compañeros

rafaxplayer
09/09/14, 08:56:14
Si es verdad , he echo exactamente lo que comenta Dexafree en el post Nº 2 , gracias de nuevo.