ListView not inflating - android

Good day to all.
I am currently working on a customized ListView which is not inflating (is not becoming visible) inside the activity.
Below are the xml and ListView adapter classes.
main.xml
<LinearLayout
android:id="#+id/llListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_below="#+id/rlFirstRow">
<ListView
android:id="#+id/lvErrorsReport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"/>
</LinearLayout>
listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/llError"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txtErrorInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llError">
<TextView
android:id="#+id/txtStatus"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Status: "/>
<TextView
android:id="#+id/txtStatusInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llMaterials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llStatus">
<TextView
android:id="#+id/txtMaterials"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Materials: "/>
<TextView
android:id="#+id/txtMaterialsInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llMaterials">
<Button
android:id="#+id/btnShowMaterials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Materials"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
BaseAdapter class
public class RepairReportListViewAdapter extends BaseAdapter
{
ArrayList<String> errors;
ArrayList<String> statuses;
ArrayList<Boolean> materials;
Context mContext;
LayoutInflater inflater;
public RepairReportListViewAdapter(Context context, ArrayList<String> err, ArrayList<String> stat, ArrayList<Boolean> mat)
{
this.mContext = context;
this.errors = err;
this.statuses = stat;
this.materials = mat;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return 0;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.repairreport_listview, null);
holder.error = (TextView)convertView.findViewById(R.id.txtErrorInfo);
holder.status = (TextView)convertView.findViewById(R.id.txtStatusInfo);
holder.material = (TextView)convertView.findViewById(R.id.txtMaterialsInfo);
holder.showMaterials = (Button)convertView.findViewById(R.id.btnShowMaterials);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.error.setText(this.errors.get(position).toString());
holder.status.setText(this.statuses.get(position).toString());
if(this.materials.get(position) == true)
{
holder.material.setText("Materials where used to fix this error. Click on the button below to view the materials.");
holder.showMaterials.setVisibility(Button.VISIBLE);
holder.showMaterials.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//LATER
}
});
}
else
{
holder.material.setText("No materials where used to fix this error.");
}
return convertView;
}
static class ViewHolder
{
TextView error;
TextView status;
TextView material;
Button showMaterials;
}
}
I've already tried changing the layouts as suggested here but the ListView is still not revealed.
Any ideas why it is not showing? Sorry for the code dump but I really cannot figure out what the heck is going on.
P.S. I did set the adapter to the ListView in the main activity.
adapter = new RepairReportListViewAdapter(this, errorNames, statuses, materialsInUse);
lvErrors.setAdapter(adapter);

your ListView does not inflate nothing because getCount is returning 0
change
#Override
public int getCount()
{
return 0;
}
with
#Override
public int getCount()
{
return (stat == null) ? 0 : stat.size();
}
Also, instead of keeping three differents ArrayList, you can create a class that holds all the info you need and an ArrayList of this class

Related

Animate button inside custom adapter for listview

I made a ListView with a custom adapter with my own layout, and on this layout there is a text view, a button and three image buttons that are at first with setVisibility.GONE
I am trying to animate this button for when the user clicks on it, it changes its position from right to left to give space for the three image buttons.
The problem is, the only item in which the animation is working is the last item on the list. I want the animation to work in all of the items.
Here's the code for my adapter:
public class ListaAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;
public ListaAdapter (ArrayList<String> list, Context context, ListView a) {
this.list = list;
this.context = context;
this.lista = a;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.textoItem);
listItemText.setText(list.get(position));
final LinearLayout btnLayout = (LinearLayout)
view.findViewById(R.id.btnLayout);
relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);
btnAbrir = (Button)view.findViewById(R.id.btnAbrir);
ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);
ImageButton btnAceitar =
(ImageButton)view.findViewById(R.id.btnAceitar);
ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);
animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);
btnLayout.setVisibility(View.GONE);
btnAbrir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnLayout.setVisibility(View.VISIBLE);
animate();
}
});
return view;
}
private void animate()
{
animation.setDuration(500);
animation.start();
}
}
And this is my custom_list.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white">
<LinearLayout
android:id="#+id/btnLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_barra">
<ImageButton
android:id="#+id/btnNaoSei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:backgroundTint="#color/compras_barra"
android:contextClickable="false"
app:srcCompat="#drawable/a12" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNaoSei"
android:layout_centerHorizontal="true"
android:text="NÃO SEI"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras">
<ImageButton
android:id="#+id/btnAceitar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:background="#color/compras"
app:srcCompat="#drawable/a11" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnAceitar"
android:paddingEnd="#dimen/_3sdp"
android:paddingTop="#dimen/_8sdp"
android:text="ACEITAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_texto2">
<ImageButton
android:id="#+id/btnNegar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#color/compras_texto2"
app:srcCompat="#drawable/a10" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:paddingBottom="#dimen/_1sdp"
android:text="NEGAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btnAbrir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/a09"
android:layout_alignTop="#+id/textoItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textoItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:maxWidth="200dp"
android:text="TextView"
android:textColor="#color/compras_texto2" />
</RelativeLayout>
Any idea on how to accomplish this? Thanks in advance.
The things you need to adopt the following method and create a ViewHolder so that your adapter will know each element of each row in the List
The last UI element of the row only animates because this the only last known id of components for your Adapater when he was creating the Views in your List. So it will animates for the last row elements
You have to add ViewHolder nested class inside your Adapter and declare your UI components. Use setTag and getTag methods inside the getView
Overall you have to create your adapter things like this in getView
public class SListAdapter extends ArrayAdapter<String> {
private Context context;
private String[] seCtnColors;
private List<Subscription> item;
private ViewHolder mainViewHolder = null;
private ViewHolder viewHolder;
private LayoutInflater inflater;
private View row;
public SListAdapter(Context c, List<Subscription> subscriptions)
{
super(c, R.layout.row,R.id.rowNameTV);
this.context=c;
this.item = subscriptions;
}
#Override
public int getCount() {
return this.item.size();
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
row = null;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row,parent,false);
viewHolder = new ViewHolder();
initAdapterUI();
setAdapterDataset(position);
return row;
}
private void initAdapterUI() {
viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
row.setTag(viewHolder);
}
private void setAdapterDataset(int position) {
mainViewHolder = (ViewHolder) row.getTag();
mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
}
public class ViewHolder{
View animatedGifViewHol;
View alertBarVerticalViewHol;
TextView firstNameTVHol;
TextView switchStateTVHol;
TextView phoneNumberTVHol;
}
}

Can't click on element in ListView Android

I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?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"
android:background="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?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"
android:background="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.

ListView with BaseAdapter and LinearLayout?

I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?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">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

GridView breaks while scrolling

I am working on a custom launcher application. For this reason I created a custom GridView that displays all installed applications.
The problem is that when I scroll through the apps, the gridview has a weird behavior. Sometimes it will break the horizontal alignment because of a multiline application name. Sometimes the scrolling bounds go crazy and you end up scrolling outside the gridview.
Here is a video of the problems I describe above.
I am using the following code.
public class CustomGridView extends BaseAdapter{
private Context mContext;
private List<Application> app;
public CustomGridView(Context c, List<Application> app) {
super();
mContext = c;
this.app = app;
}
#Override
public int getCount() {
return app.size();
}
#Override
public Object getItem(int position) {
return app.get(position).appPkg;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_item, null);
view = new ViewHolder();
view.appName = (TextView) convertView.findViewById(R.id.grid_text);
view.appIcon = (ImageView) convertView.findViewById(R.id.grid_image);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.appName.setText(app.get(position).appName);
view.appIcon.setImageDrawable(app.get(position).appIcon);
return convertView;
}
static class ViewHolder
{
ImageView appIcon;
TextView appName;
}
}
For the grid_item I use the following.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:gravity="fill_horizontal|center_horizontal">
<ImageView
android:layout_height="62dp"
android:id="#+id/grid_image"
android:layout_width="62dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/grid_text"
android:layout_marginTop="2dp"
android:textSize="12sp"
android:ellipsize="marquee"
android:gravity="center|center_horizontal" android:textAlignment="center"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
And here is the activity layout
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" android:layout_alignParentTop="true">
</GridView>
The problem with the height of TextView. So, make TextView single line like:
android:singleLine="true"
or, If u want 2 or 3 lines for some of the items, then make all the items have 2 or 3 lines like:
android:minLines="2"

Custom adapter implementation

I have a question, currently I am using CursorTreeAdapter however I want to change it to use any other data not just cursors, but to be honest im not sure how would I do that, I think implement some other adapter and then override needed methods? But could any one show me some way? I'm confused right now and don't know to what direction I should go.
Thanks for help.
Abstract classes you can extend: BaseAdapter ArrayAdapter.
You can see this example:
public class FeedAdapter extends BaseAdapter {
private ArrayList<FeedItem> items;
private LayoutInflater layoutInflater;
FeedAdapter(Context context, ArrayList<FeedItem> list, int bgColor){
items = list;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public FeedItem getItem(int index) {
return items.get(index);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (null == convertView) {
row = layoutInflater.inflate(R.layout.romanblack_feed_item, null);
} else {
row = convertView;
}
TextView title = (TextView) row.findViewById(R.id.romanblack_rss_title);
TextView pubdate = (TextView) row.findViewById(R.id.romanblack_rss_pubdate);
String titleString = items.get(position).getTitle();
title.setText(titleString);
if(items.get(position).getTextColor() != Color.TRANSPARENT){
title.setTextColor(items.get(position).getTextColor());
}else{
title.setTextColor(Color.DKGRAY);
}
pubdate.setText(items.get(position).getPubdate());
return row;
}
}
and layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/romanblack_feed_item"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/romanblack_rss_bg_feed"
android:orientation="vertical">
<TextView
android:text="Title"
android:id="#+id/romanblack_rss_title"
android:textSize="14sp"
android:textColor="#222"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="2011-01-01"
android:id="#+id/romanblack_rss_pubdate"
android:textSize="10sp"
android:textColor="#666"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

Categories

Resources