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

问题描述:

描述:我想使用分享按钮。使用分享按钮用户发送列表作为消息。在列表中,每个项目都有标题+描述 问题:系统从列表中获取所有项目,并使用逗号依次将其放入行中。使用分割字符串自定义分享按钮的意图或如何拆分字符串并替换奇怪的逗号

我:TitleItemOne,DescriptionItemOne,TitleItemTwo,DescriptionItemTwo

我需要:
TitleItemOne - DescriptionItemOne
TitleItemTwo - DescriptionItemTwo

或者: 也许更容易更换所有奇数逗号 “”与“ - ”所以它会这样我正在寻找的风格。

这是代码(在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); 
    } 
} 
+0

所以你有t1,d1,t2,d2,t3 ...在列表的一个索引中?并且你想在同一个列表条目中使用t1-d1? –

+0

@TonyKutzler我想知道我可以如何定制它。使t1-d1 t2-d2 ...例如...当在另一个应用程序,如文本消息的列表中,它只走过t1,d1,t2,d2 –

+0

@TonyKutzler我发现了一些我在找的东西。我可以拆分字符串并用“ - ”替换所有1,3,5个逗号。所以列表会看起来像我想要的。但我不知道如何替换只有ODD逗号 –

变化

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

注: 我可能没有全部括号正确关闭,但你有想法

+0

谢谢托尼先生。我知道它必须是更简单的方法。就是这样。你是亲) –

+0

我昨晚看到了这个,但是你的问题措辞有趣,欢迎你。 –

可以格式化你的HTML或使用"\n"

字符串HTML:

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

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

使用"\n"可以使用System.getProperty("line.separator")至极取决于操作系统行分隔符

+0

你能否给我举例说明我的代码? –

+0

我找到了我在找的东西。我可以拆分字符串并用“ - ”替换所有1,3,5个逗号。所以列表会看起来像我想要的。但我不知道如何只替换奇数的逗号 –