如何制作条目数组列表?

时间:2015-06-02 04:52:52

标签: android arrays arraylist sharedpreferences

我有一个包含字符串的sharedPreference类。在每个字符串中都有一个十进制数字。我想将它们全部带到另一个活动的条目数组列表中。我怎样才能做到这一点? 感谢。

首先是Sharedpreferences类:

public class Prefs extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);

        //about 30 Strings
        String w1 = prefs.getString("w1", "");
        String w2 = prefs.getString("w2", "");
        String w3 = prefs.getString("w3", "");
        String w4 = prefs.getString("w4", "");
        String w5 = prefs.getString("w5", "");
        String w6 = prefs.getString("w6", "");

这是第二项活动的一部分

private LineData generateLineData() {

    //Introduce the Array list which we made as entries
    ArrayList<Entry> entries = new ArrayList<Entry>();


    LineDataSet set = new LineDataSet(entries, "data series");
    set.setColor(Color.rgb(240, 238, 70));
    set.setLineWidth(2.5f);
    set.setCircleColor(Color.rgb(240, 238, 70));
    set.setCircleSize(5f);
    set.setFillColor(Color.rgb(240, 238, 70));
    set.setDrawCubic(true);
    set.setDrawValues(true);
    set.setValueTextSize(10f);
    set.setValueTextColor(Color.rgb(240, 238, 70));

以下是原始源代码:

package com.xxmassdeveloper.mpchartexample;



public class CombinedChartActivity extends DemoBase {

private CombinedChart mChart;
private final int itemcount = 12;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_combined);

    mChart = (CombinedChart) findViewById(R.id.chart1);
    mChart.setDescription("");
    mChart.setBackgroundColor(Color.WHITE);
    mChart.setDrawGridBackground(false);
    mChart.setDrawBarShadow(false);

    // draw bars behind lines
    mChart.setDrawOrder(new DrawOrder[] {
            DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE,     DrawOrder.LINE, DrawOrder.SCATTER
    });

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setDrawGridLines(false);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setDrawGridLines(false);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTH_SIDED);

    CombinedData data = new CombinedData(mMonths);

    data.setData(generateLineData());
    data.setData(generateBarData());
//        data.setData(generateBubbleData());
//         data.setData(generateScatterData());
//         data.setData(generateCandleData());

    mChart.setData(data);
    mChart.invalidate();
}

private LineData generateLineData() {

    LineData d = new LineData();

    ArrayList<Entry> entries = new ArrayList<Entry>();

    for (int index = 0; index < itemcount; index++)
        entries.add(new Entry(getRandom(15, 10), index));

    LineDataSet set = new LineDataSet(entries, "Line DataSet");
    set.setColor(Color.rgb(240, 238, 70));
    set.setLineWidth(2.5f);
    set.setCircleColor(Color.rgb(240, 238, 70));
    set.setCircleSize(5f);
    set.setFillColor(Color.rgb(240, 238, 70));
    set.setDrawCubic(true);
    set.setDrawValues(true);
    set.setValueTextSize(10f);
    set.setValueTextColor(Color.rgb(240, 238, 70));

    set.setAxisDependency(YAxis.AxisDependency.LEFT);

    d.addDataSet(set);

    return d;
}

private BarData generateBarData() {

    BarData d = new BarData();

    ArrayList<BarEntry> entries = new ArrayList<BarEntry>();

    for (int index = 0; index < itemcount; index++)
        entries.add(new BarEntry(getRandom(15, 30), index));

    BarDataSet set = new BarDataSet(entries, "Bar DataSet");
    set.setColor(Color.rgb(60, 220, 78));
    set.setValueTextColor(Color.rgb(60, 220, 78));
    set.setValueTextSize(10f);
    d.addDataSet(set);

    set.setAxisDependency(YAxis.AxisDependency.LEFT);

    return d;
}

protected ScatterData generateScatterData() {

    ScatterData d = new ScatterData();

    ArrayList<Entry> entries = new ArrayList<Entry>();

    for (int index = 0; index < itemcount; index++)
        entries.add(new Entry(getRandom(20, 15), index));

    ScatterDataSet set = new ScatterDataSet(entries, "Scatter DataSet");
    set.setColor(Color.GREEN);
    set.setScatterShapeSize(7.5f);
    set.setDrawValues(false);
    set.setValueTextSize(10f);
    d.addDataSet(set);

    return d;
}

protected CandleData generateCandleData() {

    CandleData d = new CandleData();

    ArrayList<CandleEntry> entries = new ArrayList<CandleEntry>();

    for (int index = 0; index < itemcount; index++)
        entries.add(new CandleEntry(index, 20f, 10f, 13f, 17f));

    CandleDataSet set = new CandleDataSet(entries, "Candle DataSet");
    set.setColor(Color.rgb(80, 80, 80));
    set.setBodySpace(0.3f);
    set.setValueTextSize(10f);
    set.setDrawValues(false);
    d.addDataSet(set);

    return d;
}

protected BubbleData generateBubbleData() {

    BubbleData bd = new BubbleData();

    ArrayList<BubbleEntry> entries = new ArrayList<BubbleEntry>();

    for (int index = 0; index < itemcount; index++) {
        float rnd = getRandom(20, 30);
        entries.add(new BubbleEntry(index, rnd, rnd));
    }

    BubbleDataSet set = new BubbleDataSet(entries, "Bubble DataSet");
    set.setColors(ColorTemplate.VORDIPLOM_COLORS);
    set.setValueTextSize(10f);
    set.setValueTextColor(Color.WHITE);
    set.setHighlightCircleWidth(1.5f);
    set.setDrawValues(true);
    bd.addDataSet(set);

    return bd;
}

private float getRandom(float range, float startsfrom) {
    return (float) (Math.random() * range) + startsfrom;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.combined, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.actionToggleLineValues: {
            for (DataSet<?> set : mChart.getData().getDataSets()) {
                if (set instanceof LineDataSet)
                    set.setDrawValues(!set.isDrawValuesEnabled());
            }

            mChart.invalidate();
            break;
        }
        case R.id.actionToggleBarValues: {
            for (DataSet<?> set : mChart.getData().getDataSets()) {
                if (set instanceof BarDataSet)
                    set.setDrawValues(!set.isDrawValuesEnabled());
            }

            mChart.invalidate();
            break;
        }
    }
    return true;
}
}

1 个答案:

答案 0 :(得分:0)

如果你的sharedprefernce键在w1,w2 .... w30序列中,那么

活动#1

ArrayList<String> list=new ArryList<String>(); for(int i=1;i<=30;i++){ list.add(prefs.getString("w"+i, "")); } Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("list_data",list); startActivity(intent);

活动#2

ArrayList<String> entries = ArrayList<String>)getIntent().getExtras("list_data"); LineDataSet set = new LineDataSet(entries, "data series");