1

In my activity, I have one button, when I click on the button its shows the custom dialog containing title & ListView.

I set adapter for ListView but getView() method not called.my array is ArrayList<String> & its size is 3.

Here is my code.

In Activity.java ::

    notification_btn = (Button) findViewById(R.id.notifications_btn);
    notification_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Dialog dialog = new Dialog(Home.this,android.R.style.Theme_Translucent_NoTitleBar);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.notifications_layout);


            ListView list = (ListView) dialog.findViewById(R.id.listView_notifications);
            TextView title = (TextView) dialog.findViewById(R.id.dialogTitle);
            TextView footer = (TextView) dialog.findViewById(R.id.notification_footer);

            title.setText("Title");


            NotificationAdapter adapter = new NotificationAdapter(Home.this,array);
            list.setAdapter(adapter);


            dialog.show();
        }
    });

NotificationAdapter.java ::

public class NotificationAdapter extends BaseAdapter{

ArrayList<String> items;
private Context context;
private LayoutInflater mInflater;

static class ViewHolder {
    TextView text;
}

public NotificationAdapter(Context context, ArrayList<String> items) {
    this.context = context;
    this.items = items;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int arg0) {
    return items.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return arg0;
}


// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes
        convertView = mInflater.inflate(R.layout.notifiation_row, null);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text_notification);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    try {

            holder.text.setText(items.get(position));


    } catch (Exception e) {
        VLogger.getLogger().info("Exception occured :: "+e);
    }

    return convertView;
}
  }

why getView() method not called? I didn't find the reason. please help.

4
  • check the size of items, if it's size is 0 then your getview method is not called. Aug 14, 2012 at 11:42
  • Is adapter's getView() @Override?
    – user370305
    Aug 14, 2012 at 11:43
  • From 1.6 jdk, @Override is not mandatory. Aug 14, 2012 at 11:47
  • same problem, may i know how u solve it
    – Rohit
    Oct 4, 2013 at 13:28

2 Answers 2

0

As soon as you set adapter to a list:

listview.setAdapter(adapter)

You have to notify the adapter about the changed data set so it will refill the list:

adapter.notifyDataSetChanged()
0

The solution is to redefine "getitem" and "getcount"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.