将stringbuilder从一个活动传递到另一个活动

时间:2013-07-11 12:12:38

标签: android android-intent

我正在尝试将带有城市名称复选框的列表视图作为项目从一个活动传递到另一个活动。我正在StringBuilder中成功检索已检查的城市。 问题在于我尝试将stringbuilder对象从一个活动转移到另一个活动。任何帮助都值得赞赏..

Java文件:

public class TailoredtwoActivity extends Activity implements OnItemClickListener, OnClickListener{

    StringBuilder builder;
    Button btn1;
    ListView mListView;
    String[] array = new String[] {"Ham", "Turkey", "Bread"};

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tailoredtwo);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);

        mListView = (ListView) findViewById(R.id.listViewcity);
        mListView.setAdapter(adapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        Button button = (Button) findViewById(R.id.btn_tailortwo_submit);
        button.setOnClickListener(this);
    }

    public void onClick(View view) {
        SparseBooleanArray positions = mListView.getCheckedItemPositions();
        builder = new StringBuilder();
        for(int index = 0; index < array.length; index++) {
            if(positions.get(index)==true)
            {
                builder.append(array[index]);
                builder.append(" ");
            }
        }
        Intent i1 = new Intent(this, TailoredthreeActivity.class);
        i1.putExtra(android.content.Intent.EXTRA_TEXT, builder);
        startActivity(i1);
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:1)

您可以传递StringBuilder包含的字符串,而不是传递整个StringBuilder。在另一个Activity检索String并使用

创建新的StringBuilder
StringBuilder stringBuilder = new StringBuilder(currentString);