2012-11-22 40 views
0

我想實現這個:Share Facebook無法設置適配器AlertDialog.Builder

在我的申請,我有一個按鈕,我想,當用戶點擊了我的按鈕AlertDialog出現,但我弄明白了適配器未設置爲AlerDialog。

我用這個代碼:

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(); 
    } 
}); 

,然後我用這個作爲適配器:

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); 

    } 


} 

我發現getView功能不運行!因爲我設置了System.out.println(),並且沒有打印。

當我運行這段代碼時,分享......沒有任何內容!

我該如何解決這個問題?

+1

你測試,看看你是否有在'activities'名單的東西嗎? – Luksprog

+0

這就是我剛發現的!我沒有任何活動列表! activities = mContext.getPackageManager()。queryIntentActivities(sendIntent,0); 當我打印它的空!似乎我什麼也沒得到,但爲什麼? –

+0

嘗試在'queryIntentActivities'方法中添加'PackageManager.MATCH_DEFAULT_ONLY'標誌而不是'0'。 – Luksprog

回答

1

實例化sendIntend像下面,

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

那麼你的列表將被填補。

我的意思是,

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(); 
      } 
     }); 
    } 

} 
+0

列表emty再 –

+0

https://github.com/talhakosen/ShareIntent你可以嘗試我的例子,我把它放在github上?它在我身邊運作 – Talha

+0

它現在的作品,謝謝 –

相關問題