Mahdi Giveie
Mahdi Giveie

Reputation: 640

Unable to set Adapter for AlertDialog.Builder

I am trying to implement this : Share Facebook

In my application, I have a button, I want when user clicks on my button the AlertDialog appear but I figure it out that the adapter is not set to AlerDialog.

I use this code :

 shareb = (ImageView) findViewById(R.id.shareb);
 shareb.setOnClickListener(new View.OnClickListener() {

     public void onClick(View v) {

         sendIntent = new Intent();
         sendIntent.setAction(Intent.ACTION_SEND);
         activities = mContext.getPackageManager().queryIntentActivities(sendIntent, 0);
         builder = new AlertDialog.Builder(mContext);
         builder.setTitle("Share with...");
         final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity) mContext, R.layout.basiclistview, activities.toArray());
         builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

             public void onClick(DialogInterface dialog, int which) {
                 // TODO Auto-generated method stub
                 ResolveInfo info = (ResolveInfo) adapter.getItem(which);
                 if (info.activityInfo.packageName.contains("facebook")) {
                     postFacebookMessage();
                 } else {
                     Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                     intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                     intent.setType("*/*");
                     intent.putExtra(Intent.EXTRA_SUBJECT, "I just read " + knowTitle);
                     intent.putExtra(Intent.EXTRA_TEXT, knowTitle + "Read the full article via MomsApp by EnfaMama A+ at http://meadjohnsonasia.com.my/mobileapp");
                     ((Activity) mContext).startActivity(intent);
                 }

             }

         });

         builder.create().show();
     }
 });

and then i use this As Adapter :

public class ShareIntentListAdapter extends ArrayAdapter < Object > {
    Activity context;
    private final Object[] items;

    int layoutId;


    public ShareIntentListAdapter(Activity context, int layoutId, Object[] items) {
        super(context, layoutId, items);

        this.context = context;
        this.items = items;
        this.layoutId = layoutId;


    }

    public View getView(int pos, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View row = inflater.inflate(layoutId, null);
        TextView label = (TextView) row.findViewById(R.id.text1);
        label.setText(((ResolveInfo) items[pos]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
        ImageView image = (ImageView) row.findViewById(R.id.logo);
        image.setImageDrawable(((ResolveInfo) items[pos]).activityInfo.applicationInfo.loadIcon(context.getPackageManager()));
        System.out.println(layoutId);
        return (row);

    }


}

I found that getView function is not running! Because I set System.out.println(), and it didn't print.

When I run this code, share with... it appears without any content!

How can I fix this?

Upvotes: 0

Views: 4485

Answers (1)

Talha
Talha

Reputation: 12717

instantiate your sendIntend like below ,

sendIntent = new Intent(Intent.ACTION_SEND, null);
sendIntent.addCategory(Intent.CATEGORY_DEFAULT);
sendIntent.setType("text/plain");

then your list will be filled.

i mean,

public class MainActivity extends Activity {

    ImageView shareb;
    Intent sendIntent;
    AlertDialog.Builder builder;
    List<ResolveInfo> activities;
    Context mContext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext= getApplicationContext();

        shareb = (ImageView) findViewById(R.id.imageView1);
        shareb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {               
                sendIntent = new Intent(Intent.ACTION_SEND, null);
                sendIntent.addCategory(Intent.CATEGORY_DEFAULT);
                sendIntent.setType("text/plain");

                activities =  mContext.getPackageManager().queryIntentActivities(sendIntent, 0);

                builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Share with...");
                final ShareIntentListAdapter adapter = new ShareIntentListAdapter(MainActivity.this, R.layout.basiclistview, activities.toArray());                
                builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        ResolveInfo info = (ResolveInfo) adapter.getItem(which);
                        if (info.activityInfo.packageName.contains("facebook")) {
                            //postFacebookMessage();
                        } else {
                            Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                            intent.setType("*/*");
                            intent.putExtra(Intent.EXTRA_SUBJECT, "I just read ");
                            intent.putExtra(Intent.EXTRA_TEXT, "Read the full article via MomsApp by EnfaMama A+ at http://meadjohnsonasia.com.my/mobileapp");
                            ((Activity) mContext).startActivity(intent);
                        }

                    }

                });

                builder.create().show();
            }
        });
    }

}

Upvotes: 1

Related Questions