java - 使用分割字符串自定义共享按钮中的 Intent 或如何分割字符串并替换奇数逗号

标签 java android listview android-intent share

描述:我想使用分享按钮。通过共享按钮,用户可以将列表作为消息发送。列表中的每个项目都有标题 + 说明 问题:系统从列表中获取所有项目,并使用逗号将其逐个放入行中。

我有:TitleItemOne、DescriptionItemOne、TitleItemTwo、DescriptionItemTwo

我需要:
TitleItemOne - DescriptionItemOne
标题项目二 - 描述项目二

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

这就是代码(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”),它是操作系统相关的行分隔符

关于java - 使用分割字符串自定义共享按钮中的 Intent 或如何分割字符串并替换奇数逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45972507/

相关文章:

java - Android 预览中的空白屏幕

android - 无法在 Android 的 Volley 请求监听器中更新 ListView 适配器

java - Android:jar 库与项目库相比不起作用

android - 暂停和恢复基于 RxJava 2.X 中的 bool 门的可观察对象?

java - 这背后的推理和范围(我认为与范围有关)

java - 如何在 OpenCV 中显示 Mat 变量中存在的所有值?

java - ListView未填充数据

android - 在 ScrollView 中使用 Listview

java - IntelliJ 和 Maven - 无法创建 Maven 项目

java - spring beanpostprocessor 不适用于组件扫描?