【问题标题】:Spinner to String to Intent to Array . Why won't this Intent work?Spinner 到 String 到 Intent 到 Array 。为什么这个 Intent 不起作用?
【发布时间】:2013-01-18 03:33:31
【问题描述】:

为什么 Intent 不能正常工作? MainActivity 是一个图表,它最初绘制了数组 (series1Numbers) 中的一条线。我有另一个带有微调器的类 (Main2Activity),我想填充 MainActivity 中的 (series1numbers) 数组,从而生成我的图表。目前(series1numbers)的图形线是空白的,这很有意义,因为我无法弄清楚如何用(Main2Activity)中的单个数字填充它。我已经搜索和搜索,但无法将这个问题联系起来。

主Activity.java

package com.example;
import java.util.Arrays;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;

/**
* The simplest possible example of using AndroidPlot to plot some data.
*/
public class MainActivity extends Activity
{

private XYPlot mySimpleXYPlot;

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intename = getIntent();
String name = (String) intename.getSerializableExtra("series1Numbers");



// initialize our XYPlot reference:
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

// Create a couple arrays of y-values to plot:
Number[] series1Numbers ={name};
Number[] series2Numbers = {-4, -4, -5, -4, -4, -4, -3, -7, -4, -2, -4, -5, -5, -5};

// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers), 
// SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, 
// Y_VALS_ONLY means use the element index as the x value
"Series1");                             
// Set the display title of the series

// same as above
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");

// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
        Color.rgb(0, 200, 0),                   // line color
        Color.rgb(0, 100, 0),                   // point color
        null);                                  // fill color (none)

// add a new series' to the xyplot:
mySimpleXYPlot.addSeries(series1, series1Format);

// same as above:
mySimpleXYPlot.addSeries(series2,
        new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), null));


// reduce the number of range labels
mySimpleXYPlot.setTicksPerRangeLabel(3);

// by default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();

}

Main2Activity.java

package com.example;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Main2Activity extends Activity implements OnItemSelectedListener {

//TextView series1Numbers ;
Spinner spin;
String[] series1Numbers = { "-1", "-2", "-3", "-4", "-5",
   "-6", "-7", "-8" , "-9" , "-10" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

//series1Numbers  = (TextView) findViewById(R.id.series1Numbers );

Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);

ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, series1Numbers);

spin.setAdapter(aa);
}

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
// series1Numbers .setText(items[position]);
    if (position == 1){
      Intent intentObj = new Intent(Main2Activity.this, MainActivity.class);
      intentObj.putExtra("series1Numbers", series1Numbers);
      startActivity(intentObj);

    }
}

【问题讨论】:

    标签: android arrays string android-intent


    【解决方案1】:
    Bundle b=new Bundle();
    b.putStringArray(key, new String[]{value1, value2});
    Intent i=new Intent(context, Class);
    i.putExtras(b);
    

    为了阅读:

    Bundle b=this.getIntent().getExtras();
    String[] array=b.getStringArray(key);
    

    希望这会对您有所帮助。 :)

    更多信息,请参考this

    【讨论】:

    • 我不明白如何实现这一点。我是否将第一部分插入到微调器的 onselect 中?
    • 是的,在微调器的onItemSelected() 中包含第一部分,并在捆绑中传递字符串数组。(在您的情况下为 series1Numbers)。
    猜你喜欢
    • 2011-04-28
    • 1970-01-01
    • 2019-06-07
    • 2017-05-23
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2016-11-23
    相关资源
    最近更新 更多