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

Posted

技术标签:

【中文标题】使用拆分字符串自定义共享按钮中的意图或如何拆分字符串并替换奇数逗号【英文标题】:Customize intent in share button with splitting string or how to split string and replace odd commas 【发布时间】:2018-02-08 21:09:54 【问题描述】:

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

我有:TitleItemOne、DescriptionItemOne、TitleItemTwo、DescriptionItemTwo

我需要: TitleItemOne - 描述ItemOne TitleItemTwo - 描述ItemTwo

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

这就是代码(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);
    

【问题讨论】:

所以你有 t1,d1,t2,d2,t3... 在列表的一个索引中?并且您希望 t1-d1 在同一个列表条目中? @TonyKutzler 我想知道如何自定义它。使 t1-d1 t2-d2 ... 例如... 在另一个应用程序内的列表(如短信)的步骤上,它只进入 t1,d1,t2,d2 @TonyKutzler 我找到了我想要的东西。我可以拆分字符串并将所有 1,3,5 逗号替换为“-”。所以列表看起来像我想要的。但我不知道如何只替换奇数逗号 只需将 t1-d1 添加到列表中,然后添加 t2-... 等等 @TonyKutzler 我无法理解。我可以添加“-”,但它使 t1-,d1,它不能替换它。它只是添加它。 【参考方案1】:

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

字符串转html

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

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

对于使用"\n",您可以使用System.getProperty("line.separator"),它是依赖于操作系统的行分隔符

【讨论】:

你能根据我的代码给我一个例子吗? 我找到了我正在寻找的东西。我可以拆分字符串并将所有 1,3,5 逗号替换为“-”。所以列表看起来像我想要的那样。但我不知道如何只替换奇数逗号【参考方案2】:

改变

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

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

【讨论】:

谢谢托尼先生。我知道它必须是更简单的方法。就是这样。你是专业的) 我昨晚看到了,但是你的问题措辞很有趣,欢迎您。

以上是关于使用拆分字符串自定义共享按钮中的意图或如何拆分字符串并替换奇数逗号的主要内容,如果未能解决你的问题,请参考以下文章

如何在n子串中拆分字符串?

如何拆分字符串数组?

如何编写自定义函数将文档拆分为具有相同 ID 的多个文档

VB.Net/C# - 在自定义函数上拆分字符串

boost::algorithm - 拆分字符串会返回一个额外的标记

如何将此字符串拆分为 UDF