Developer
Developer

Reputation: 6350

Getting Error in BaseAdapter Android

I have created a class that extends BaseAdapter.I don't have any custom.xml to inflate it . Simple i have written this code

 public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();//Getting Error here to create a method getLayoutInflater
        View row;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        return (row);
    }

On getLayoutInflater() it is showing me the error to create a method.If i am creating this class in the main activity then it is working properly.So what i have to do remove this error

For creating the ListView i am calling the adapter as

myAdapter = new dataListAdapter(this,passengerList);
listView.setAdapter(myAdapter);

For Updation of the ListView item i am calling the adapter as

 update.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(isSuccess){
                    dialog.cancel();
                    passengerList[position] = newString;
                    listView.setAdapter(new dataListAdapter(this,passengerList));
                }
            }
        });

MY BaseAdapter Class

class PassengerListView extends BaseAdapter {
    String[] Title;
    Activity activity;

    public dataListAdapter(MainActivity mainActivity, String[] text) {
        Title = text;
        activity = mainActivity;
    }

    public String[] getAllValues() {
        return Title;
    }

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

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();//Getting Error here to create a method getLayoutInflater
        View row = null;
        if(convertView == null) {
            row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        return (row);
    }

So on the updation code it showing me this error :

The constructor PassengerListView(new View.OnClickListener(){}, String[]) is undefined

Upvotes: 0

Views: 1908

Answers (4)

Raghunandan
Raghunandan

Reputation: 133560

Its a method of activity class.

http://developer.android.com/reference/android/app/Activity.html#getLayoutInflater()

You need to pass the activity context to the constructor of asynctask and use the same

     new CustomAdapter(ActivityName.this); // pass the activity context

Then in your adapter

     LayoutInflater mInflater; 
     Context mContext;
     public CustomAdapter(Context context)
     {
        // use the activity context now
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     } 

In `getView

     row = mInflater.inflate(android.R.layout.customlayouttoinflate, parent, false);

Also you need to use a view holder.

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

To update you need to update the list that populates your listview and call notidyDataSetChanged on your adapter.

Edit:

From my comments

You are doing listView.setAdapter(new dataListAdapter(this,passengerList))in button click listener and you are using this instead of ActivityName.this .

Also i don't know why you have

 new dataListAdapter(this,passengerList);

Should be

 new PassengerListView (ActivityName.this,passengerList);

Change your constructor to

public PassengerListView(MainActivity mainActivity, String[] text) {
    Title = text;
    activity = mainActivity;
}

Upvotes: 2

Mr.Sandy
Mr.Sandy

Reputation: 4349

Try this I think it is useful for you....

     LayoutInflater mInflater;
     PassengerListView(Activity activity,String[] data)
     {
         this.activity=activity;
         this.data=data;
         mInflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);    
     }

Above is the constructor for your custom Adapter class that have two parameters which is change as per your requirements...

and below is the code for create a view and return it...

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

        if(convertView == null) {
            convertView = inflater.inflate(android.R.layout.simple_list_item_1, false);
        }
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        convertView.setTag(title);
        return convertView;
    }   

Upvotes: 0

Rajiv
Rajiv

Reputation: 306

Answer given by Raghunandan seems correct but only you don't need to inflate everytime. your code could be like this

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = mContext.getLayoutInflater();//mContext being the private variable intialized by constructor.
    View row = convertView;
    if(convertView == null) {
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    }
    TextView title;
    title = (TextView) row.findViewById(android.R.id.text1);
    title.setText(Title[position]);
    return (row);
}

Upvotes: 0

user2652394
user2652394

Reputation: 1686

BaseAdapter is not Activity therefore, it can NOT get the method of Activity, you need to pass the Context, in this case is your Activity to do so:

LayoutInflater inflater = your_activity.getLayoutInflater();//Getting Error here to create a method getLayoutInflater

Hope this helps

Upvotes: 1

Related Questions