Android:如何使用Intent.createChooser()共享listview数据

Android:如何使用Intent.createChooser()共享listview数据,android,android-intent,android-actionbar,Android,Android Intent,Android Actionbar,您好,在我的应用程序中,我有一个带有listview的片段和一个微调器,我正在使用cursoradapter将数据从我的数据库获取到listview,我想通过电子邮件共享此listview数据,我正在我的操作栏上实现shareintent操作,但我想知道如何将listview数据获取到intent选择器中 下面是我的代码: actionBar.addAction(new IntentAction(this, createShareIntent(), R.drawable.ic_title_sh

您好,在我的应用程序中,我有一个带有listview的片段和一个微调器,我正在使用cursoradapter将数据从我的数据库获取到listview,我想通过电子邮件共享此listview数据,我正在我的操作栏上实现shareintent操作,但我想知道如何将listview数据获取到intent选择器中

下面是我的代码:

 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");
    }
下面是我的游标适配器

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.向
SQLiteOpenHelper
类(如果您使用)添加一个方法,该方法直接从数据库返回包含数据的字符串。
2.在
CursorAdapter
类中定义一个字符串成员,每次调用
bindView
时,将每个列表项数据连接到该字符串。

如果你发布更多的代码,我可以更具体

编辑: 因此,从您的代码中,您希望走第二条道路:
假设您的片段中有一个名为
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");
    }

你能把你的游标适配器代码吗?嗨,我用游标适配器代码更新了我的问题。请检查它。当我第一次单击sharebutton时,它不工作,当我更改活动并返回sharebutton时,它工作。。如果不看代码,很难说发生了什么。调试您的代码并查看发生什么情况。我无法找到第一次没有加载数据的原因。请尝试将ActionBar创建放在onCreateView()中