Luke Batley
Luke Batley

Reputation: 2402

Android Custom Array adapter not running

Hi i haver created a custom array adapter to populate a list when i run the application it doesn't crash but nothing happens i have tried implementing logs inside the adapter to see which part it get sticks but it's not running any of it? does anyone know A. what i'm doing wrong or B. how to correctly implement a custom array adapter?

heres my adapter

import java.util.ArrayList;

import android.content.ClipData.Item;
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.ImageView;
import android.widget.TextView;

public class DocumentArrayAdapter extends ArrayAdapter<String> {



    // declaring our ArrayList of items
    private ArrayList<String> docTitles;
    private ArrayList<String> docTypes;
    private ArrayList<String> docModified;
    /* here we must override the constructor for ArrayAdapter
    * the only variable we care about now is ArrayList<Item> objects,
    * because it is the list of objects we want to display.
    */
    public DocumentArrayAdapter(Context context, int layoutResourceId, ArrayList<String> docTitles, ArrayList<String> docTypes, ArrayList<String> docModified) {
        super(context, layoutResourceId);
        this.docTitles = docTitles;
        this.docTypes = docTypes;
        this.docModified = docModified;

    }

    /*
     * we are overriding the getView method here - this is what defines how each
     * list item will look.
     */
    public View getView(int position, View convertView, ViewGroup parent){

        // assign the view we are converting to a local variable
        View v = convertView;
        Log.v("Main","v, = " + v);
        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.document_cell, null);
        }

        /*
         * Recall that the variable position is sent in as an argument to this method.
         * The variable simply refers to the position of the current object in the list. (The ArrayAdapter
         * iterates through the list we sent it)
         * 
         * Therefore, i refers to the current Item object.
         */
        String title = docTitles.get(position);
        String types = docTypes.get(position);
        String modified = docModified.get(position);

        Log.v("Main","DocumentArrayAdapter, = " + title + " " + types + " " + modified);

        if (title != null) {

            // This is how you obtain a reference to the TextViews.
            // These TextViews are created in the XML files we defined.

            TextView docTitle = (TextView) v.findViewById(R.id.name);
            TextView docType = (TextView) v.findViewById(R.id.doctype);
            TextView docMod = (TextView) v.findViewById(R.id.modified);
            ImageView docImage = (ImageView) v.findViewById(R.id.docicon);


            // check to see if each individual textview is null.
            // if not, assign some text!
            if (docTitle != null){
                docTitle.setText(title);
            }
            if (docTypes != null){
                docType.setText(types);
            }
            if (docTitle != null){
                docMod.setText(modified);
            }


        }

        // the view must be returned to our activity
        return v;

    }} 

and heres where i'm calling it inside a fragment docNamesArray,DocTypeArray and DocModifiedArray are all Arraylists.

docsList = (ListView) rootView.findViewById(R.id.list);
  adapter = new DocumentArrayAdapter (getActivity(),R.layout.document_cell, docNamesArray,docTypeArray,docModifiedArray);
              docsList.setAdapter(adapter); 

Upvotes: 1

Views: 156

Answers (2)

Sai
Sai

Reputation: 1

In your adapter Constructor layoutInflater = LayoutInflater.from(context);

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

if (convertView == null) {

                convertView = layoutInflater.inflate(R.layout.activitylayout,
                        null);

holder = new ViewHolder();

                holder.tt = (TextView) convertView.findViewById(R.id.toptext);




}else {

                holder = (ViewHolder) convertView.getTag();
            }



holder.tt.setText(text);

return convertView;

}

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Change this

 super(context, layoutResourceId);

to

 super(context, layoutResourceId,docTitles);

You should also consider using a ViewHolder pattern for smooth scrolling and performance.

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Upvotes: 2

Related Questions