如何将光标列表添加到共享按钮中的意图

时间:2017-08-18 02:50:43

标签: java android android-intent

我有一个应用程序,我使用内容提供程序来处理数据库。我有一个列表,它被夸大并显示给用户。我需要将该列表保存到intent中,并使用分享按钮发送它,例如I found here

以下是我的问题 -

1)我是否需要将光标转换为列表或是否需要创建第二个列表并使用光标对其进行充气或是否有其他变体? 2)如何自定义收到的邮件的外观?

这是我的代码 -

public class CatalogActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String TAG = "myLogs";

    /** Identifier for the pet data loader */
    private static final int LIST_LOADER = 0;

    /** Adapter for the ListView */
    ListCursorAdapter mCursorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalog);

        Log.v(TAG, "Зашли в catalog activity oncreate");

        // Setup FAB to open EditorActivity
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
                startActivity(intent);
            }
        });

        // Find the ListView which will be populated with the list data
        ListView listListView = (ListView) findViewById(R.id.list);

        // Find and set empty view on the ListView, so that it only shows when the list has 0 items.
        View emptyView = findViewById(R.id.empty_view);
        listListView.setEmptyView(emptyView);

        // Setup an Adapter to create a list item for each row of list data in the Cursor.
        // There is no items data yet (until the loader finishes) so pass in null for the Cursor.
        mCursorAdapter = new ListCursorAdapter(this, null);
        listListView.setAdapter(mCursorAdapter);

        // Setup the item click listener
        listListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                ShoppingListBdHelper helper = new ShoppingListBdHelper(view.getContext());
                if (helper.setCompleted(id)) {
                    mCursorAdapter.setCompleted(view);
                }
            }
        });

        // Kick off the loader
        getSupportLoaderManager().initLoader(LIST_LOADER, null, this);
    }

    /**
     * Helper method to insert hardcoded item data into the database. For debugging purposes only.
     */
    private void insertPet() {

        Log.v(TAG, "Создали insertpet");

        Intent myIntent = new Intent(Intent.ACTION_SEND);

        String shareBody = "Body here";
        String shareSub = "Sub here";
        myIntent.putExtra(Intent.EXTRA_TEXT, shareSub);
        myIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
        myIntent.setType("text/plain");
        startActivity(Intent.createChooser(myIntent, "Share using"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_catalog.xml file.
        // This adds menu items to the app bar.
        getMenuInflater().inflate(R.menu.menu_catalog, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {
            // Respond to a click on the "Insert dummy data" menu option
            case R.id.action_insert_dummy_data:
                insertPet();
                return true;
            // Respond to a click on the "Delete all entries" menu option
            case R.id.action_delete_all_entries:
                deleteAllItems();
                return true;
            case R.id.menu_item_share:
                shareIntent();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * Helper method to delete all list in the database.
     */
    private void deleteAllItems() {

        Log.v(TAG, "Сработал метод удаления всех данных");
        long rowsDeleted = getContentResolver().delete(ListContract.ListEntry.CONTENT_URI, null, null);
        Log.v("CatalogActivity", rowsDeleted + " rows deleted from list database");
    }

    private void shareIntent(){
        ArrayList<String> list = new ArrayList<String>();

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putStringArrayListExtra("test", (ArrayList<String>) list);
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        Log.v(TAG, "Начал работать loader cursor");
        // Define a projection that specifies the columns from the table we care about.
        String[] projection = {
                ListContract.ListEntry._ID,
                ListContract.ListEntry.COLUMN_ITEM_NAME,
                ListContract.ListEntry.COLUMN_ITEM_DESCRIPTION,
                ListContract.ListEntry.COLUMN_ITEM_COMPLETED
        };

        // This loader will execute the ContentProvider's query method on a background thread
        return new CursorLoader(this,   // Parent activity context
                ListContract.ListEntry.CONTENT_URI,   // Provider content URI to query
                projection,             // Columns to include in the resulting Cursor
                null,                   // No selection clause
                null,                   // No selection arguments
                null);                  // Default sort order

    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Update {@link ListCursorAdapter} with this new cursor containing updated pet data
        mCursorAdapter.swapCursor(data);
        Log.v(TAG, "Cursor adapter загрузился");
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Callback called when the data needs to be deleted
        mCursorAdapter.swapCursor(null);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用ArrayList代替StringBuilder,只需将ArrayList转换为StringBuilder并传递builder.toString(),我认为这对您有用。