2015-06-02 103 views
0

我有一個包含字符串的sharedPreference類。在每個字符串中都有一個十進制數字。我想將它們全部帶到條目另一個活動中的數組列表。我怎樣才能做到這一點? 謝謝。如何創建一個Entry Array List?

這裏是第一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; 
} 
} 
+0

Wharts的問題,你所面對? – Pavya

+0

@ user3676184需要知道如何從字符串創建條目數組列表,以及如何將其引入第二個活動,然後將其用作「條目」! – Civiato

+1

究竟是什麼'Entry'? –

回答

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

+0

它不接受條目爲String或Double。它應該由Entry創建,如原始源代碼中所示。 – Civiato

相關問題