Android:如何使用Intent.createChooser()共享列表视图数据

时间:2013-10-08 07:50:57

标签: android android-intent android-actionbar

您好我的应用程序中有一个listview和一个微调器的片段,我使用cursoradapter从我的数据库中获取数据到listview,我想通过电子邮件分享这个listview数据,我正在我的操作栏上实现shareintent操作,但是如何将listview数据导入意图选择器。

以下是我的代码:

 actionBar.addAction(new IntentAction(this, createShareIntent(), R.drawable.ic_title_share_default));

  private Intent createShareIntent() {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "Shared from the ActionBar widget.");
        return Intent.createChooser(intent, "Share");
    }

下面是我的cursorAdapter

public class bottomlistmonthlyvolume extends CursorAdapter {
    LayoutInflater inflater;
    public bottomlistmonthlyvolume(Context context, Cursor c) {
    super(context, c);
    inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor topcursor) {
    // TODO Auto-generated method stub
    TextView tv1 = (TextView)view.findViewById(R.id.textView123);
    TextView tv2 = (TextView)view.findViewById(R.id.achievmentValue);
    TextView tv3 = (TextView)view.findViewById(R.id.shortfallValue);
    TextView et1 = (TextView)view.findViewById(R.id.actualvalue);
    TextView et2 = (TextView)view.findViewById(R.id.budgetvalue);

    tv1.setText(topcursor.getString(1));
    tv2.setText(topcursor.getString(7));
    tv3.setText(topcursor.getString(9));
    et1.setText(topcursor.getString(3));
    et2.setText(topcursor.getString(5));

    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {


    return inflater.inflate(R.layout.word_list_item, parent, false);


    }
}

有人可以建议如何实施它

1 个答案:

答案 0 :(得分:0)

我看到两种方式:
1.向SQLiteOpenHelper类(如果使用它)添加方法,该方法将直接从数据库返回包含数据的字符串。
2.在CursorAdapter类中,定义一个字符串成员,每次调用bindView时,将每个列表项数据连接到该字符串。

如果您发布更多代码,我可以更具体。

编辑: 所以从你的代码中你想要继续前进2:
假设您的片段中有一个名为bottomlistmonthlyvolume mAdapter;的引用 在适配器中,请参阅注释

public class bottomlistmonthlyvolume extends CursorAdapter {
    LayoutInflater inflater;
    StringBuilder shareData = new StringBuilder(); // container for shared data

    public bottomlistmonthlyvolume(Context context, Cursor c) {
    super(context, c);
    inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor topcursor) {
    // TODO Auto-generated method stub
    TextView tv1 = (TextView)view.findViewById(R.id.textView123);
    TextView tv2 = (TextView)view.findViewById(R.id.achievmentValue);
    TextView tv3 = (TextView)view.findViewById(R.id.shortfallValue);
    TextView et1 = (TextView)view.findViewById(R.id.actualvalue);
    TextView et2 = (TextView)view.findViewById(R.id.budgetvalue);

    // Assume we want to share all 'achievmentValue' with a title
    shareData.append("YOUR TITLE").append(topcursor.getString(7)).append("\n");

    tv1.setText(topcursor.getString(1));
    tv2.setText(topcursor.getString(7));
    tv3.setText(topcursor.getString(9));
    et1.setText(topcursor.getString(3));
    et2.setText(topcursor.getString(5));

    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {


    return inflater.inflate(R.layout.word_list_item, parent, false);


    }

    public String getShareData(){
        return shareData.toString();
    }
}

在操作栏中:

actionBar.addAction(new IntentAction(this, createShareIntent(), R.drawable.ic_title_share_default));

  private Intent createShareIntent() {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, mAdapter.getShareData());
        return Intent.createChooser(intent, "Share");
    }