使用拆分字符串或如何拆分字符串并替换奇数逗号来自定义共享按钮中的意图

Customize intent in share button with splitting string or how to split string and replace odd commas

描述:我想使用分享按钮。使用共享按钮用户发送列表作为消息。在列表中,每个项目都有标题+描述 问题:系统从列表中获取所有项目,并使用逗号将其一个接一个地放在一行中。

我有:TitleItemOne、DescriptionItemOne、TitleItemTwo、DescriptionItemTwo

我需要:
TitleItemOne - DescriptionItemOne
TitleItemTwo - DescriptionItemTwo

或者: 也许将所有 ODD 逗号“,”替换为“-”会更容易,所以它会是我正在寻找的样式。

这就是代码(Sharebutton 方法中需要的代码)

/**
 * Displays list of list that were entered and stored in the app.
 */
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);
    }

    @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_share_button:
                shareButton(mCursorAdapter.getCursor());
                return true;
            // Respond to a click on the "Delete all entries" menu option
            case R.id.action_delete_all_entries:
                deleteAllItems();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Share button
     */
    private void shareButton(Cursor cursor) {

        Log.v(TAG, "--- WE ARE IN SHARE BUTTON METHOD ---");

        List<String> test;
        test = new ArrayList<String>();
        cursor.moveToFirst();

        while(!cursor.isAfterLast()) {
            Log.d(TAG, "field: " + cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_NAME)));

            test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_NAME))); //add the item
            test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_DESCRIPTION))); //add the item
            cursor.moveToNext();
        }

        cursor.moveToFirst();

        Log.v(TAG, "--- OUR LIST INCLUDES: " + test.toString());

        Intent myIntent = new Intent();
        myIntent.setAction(Intent.ACTION_SEND);
        myIntent.putStringArrayListExtra("test", (ArrayList<String>) test);
        myIntent.putExtra(android.content.Intent.EXTRA_TEXT, test.toString());

        Log.v(TAG, "--- INTENT EXTRAS ARE: " + myIntent.getExtras());

        myIntent.setType("text/plain");
        startActivity(Intent.createChooser(myIntent, "Share using"));
    }


    /**
     * 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");
    }

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

您可以将 字符串 格式化为 html 或使用 "\n"

字符串到 html :

您可以使用 Html.fromHtml() 在您的字符串中使用 HTML 标签:

Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

要使用 "\n",您可以使用 System.getProperty("line.separator"),它是 OS 依赖行分隔符

改变

test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_NAME))); //add the item test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_DESCRIPTION)));

test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_NAME)) + "-" + (cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_DESCRIPTION)));

注意: 我可能没有正确关闭所有括号,但你明白了