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

Java 使用拆分字符串自定义共享按钮中的意图或如何拆分字符串并替换奇数逗号,java,android,listview,android-intent,share,Java,Android,Listview,Android Intent,Share,描述:我想使用共享按钮。使用“共享”按钮,用户将列表作为消息发送。在列表中,每个项目都有标题+说明 问题是:系统从一个列表中获取所有项目,并使用逗号将其依次放入行中 我有:第二个,第二个,第二个 我需要: TitleItemOne-描述素 标题项2-描述项2 或: 也许用“-”替换所有的逗号“,”更容易些,所以它将成为我所寻找的样式 这就是代码(Sharebutton方法中需要的代码) /** *显示已输入并存储在应用程序中的列表列表。 */ 公共类CatalogActivity扩展了AppCo

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

我有:第二个,第二个,第二个

我需要:
TitleItemOne-描述素
标题项2-描述项2

或: 也许用“-”替换所有的逗号“,”更容易些,所以它将成为我所寻找的样式

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

/**
*显示已输入并存储在应用程序中的列表列表。
*/
公共类CatalogActivity扩展了AppCompatActivity实现
LoaderManager.LoaderCallbacks{
私有静态最终字符串TAG=“myLogs”;
/**pet数据加载器的标识符*/
私有静态最终整数列表\u加载程序=0;
/**ListView的适配器*/
ListCursorAdapter-mCursorAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_目录);
Log.v(标签“创建时的目录活动”);
//设置FAB以打开编辑器活动
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(CatalogActivity.this、EditorActivity.class);
星触觉(意向);
}
});
//查找将用列表数据填充的ListView
ListView ListView=(ListView)findViewById(R.id.list);
//在ListView上查找并设置空视图,使其仅在列表包含0项时显示。
视图emptyView=findViewById(R.id.empty\u视图);
setEmptyView(emptyView);
//设置适配器以为光标中的每行列表数据创建列表项。
//还没有项目数据(直到加载程序完成),所以为光标传入null。
mCursorAdapter=新的ListCursorAdapter(此为空);
setAdapter(mCursorAdapter);
//设置项目单击侦听器
ListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id){
ShoppingListBdHelper=新的ShoppingListBdHelper(view.getContext());
if(helper.setCompleted(id)){
mCursorAdapter.setCompleted(视图);
}
}
});
//启动装载机
getSupportLoaderManager().initLoader(列表加载器,null,this);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//从res/menu/menu_catalog.xml文件中展开菜单选项。
//这会将菜单项添加到应用程序栏。
getMenuInflater().充气(R.menu.menu\u目录,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//用户单击了应用程序栏溢出菜单中的菜单选项
开关(item.getItemId()){
//对点击“插入虚拟数据”菜单选项作出响应
案例R.id.action\u share\u按钮:
shareButton(mCursorAdapter.getCursor());
返回true;
//对单击“删除所有条目”菜单选项作出响应
案例R.id.action\u删除\u所有\u条目:
deleteAllItems();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
/**
*共享按钮
*/
私有void共享按钮(光标){
Log.v(标签“---我们在共享按钮方法----”;
列表测试;
测试=新的ArrayList();
cursor.moveToFirst();
而(!cursor.isAfterLast()){
Log.d(标记,“字段:”+cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_NAME));
test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_NAME));//添加项
test.add(cursor.getString(cursor.getColumnIndex(ListContract.ListEntry.COLUMN_ITEM_DESCRIPTION));//添加项
cursor.moveToNext();
}
cursor.moveToFirst();
Log.v(标记“”--我们的列表包括:“+test.toString());
Intent myIntent=新Intent();
myIntent.setAction(Intent.ACTION\u SEND);
myIntent.PutstringArrayListXTRA(“测试”,(ArrayList)测试);
myIntent.putExtra(android.content.Intent.EXTRA_TEXT,test.toString());
Log.v(标记“---INTENT附加值是:“+myIntent.getExtras());
myIntent.setType(“文本/普通”);
startActivity(Intent.createChooser(myIntent,“共享使用”);
}
/**
*方法删除数据库中的所有列表。
*/
私有void deleteAllItems(){
日志v(标签“Саааааааааааааааааа;
long rowsDeleted=getContentResolver().delete(ListContract.ListEntry.CONTENT_URI,null,null);
Log.v(“CatalogActivity”,rowsDeleted+“从列表数据库中删除的行”);
}
@凌驾
公共加载器onCreateLoader(inti,Bundle){
Log.v(标记“装载机光标”);
//定义一个投影,指定我们关心的表中的列。
字符串[]投影={
ListContract.ListEntry.\u ID,
ListContract.ListEntry.COLUMN\u项目名称,
ListContract.ListEntry.COLUMN\u项目\u说明,
ListContract.ListEntry.COLUMN\u项目\u已完成
};
//此加载程序将执行ContentProvid
/**
 * 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.fromHtml("<h2>Title</h2><br><p>Description here</p>"));