Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List View adapter not working, getView not called.

I think i'm turning crazy. Something so simple has bind a custom adapter to a Listview is giving me a headache.

Post the code and explain then:

MainActivity.java

package com.example.pruebalist;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private static String[] data = new String[] {"0","1","2","3"};

    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.v("MainActivity","Inside MainActivity");
        setContentView(R.layout.main);
        ListView lstView = (ListView)findViewById(R.id.listNoticias);

        ArrayAdapter<String> adapter = new LstAdapter(this, R.layout.row, data);
        lstView.setAdapter(adapter);
    }

}

LstAdapter.java

package com.example.pruebalist;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LstAdapter extends ArrayAdapter<String>{

    private String[] mData;
    private Context mContext;
    int layoutResourceId;

    public LstAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        mContext = context;
        mData = values;
        layoutResourceId = textViewResourceId;

        Log.v("LstAdapter","Inside LstAdapter");
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        Log.v("LstAdapter","Inside getView");

        if(v==null){
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            v = inflater.inflate(layoutResourceId, parent,false);
        }

        String item = mData[position];
        if(item!=null){
            TextView txtItem = (TextView)v.findViewById(R.id.texto);
            if(txtItem!=null){
                txtItem.setText(item);
            }
        }

        return v;
    }

}

The ListView is never show. And getView is never used, logCat doesn't show "Inside Getview".

What's wrong?

like image 647
Rafael Osuna Dominguez Avatar asked Aug 20 '12 13:08

Rafael Osuna Dominguez


People also ask

What does an Adaptor do for a view like a ListView?

The adapter extracts the correct data from the data object and assigns this data to the views in the row of the ListView . These items are typically called the data model of the list.

What is adapter list out adapter which you know?

In Android, Adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to an Adapter view then view can takes the data from the adapter view and shows the data on different views like as ListView, GridView, Spinner etc.


1 Answers

Main problem is

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

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

change it to

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

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return mData[arg0];
    }

Check out this Vogella Tutorial on List View & List Activity, Try to use ViewHolder in List Adapter it will increase your view performance.

like image 163
rajpara Avatar answered Nov 06 '22 13:11

rajpara