当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to convert String Values to Integer Array List - Android

问题描述:

I want to input 3 data From 3 EditText fields and it will want to display as a Bar-Chart.

Here is my code section. Give me a solution for these errors..

I tried to get this Bar Chart using this code. But it is not working. i Comment The //ERROR POINT

 protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.getfromdb);

final String[] mMonth = new String[] {

"Jan", "Feb" , "Mar", "Apr", "May", "Jun",

"Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"

};

final EditText t1 =(EditText)findViewById(R.id.textView1);

final EditText t2 =(EditText)findViewById(R.id.textView2);

final EditText t3 =(EditText)findViewById(R.id.textView3);

Button btnChart = (Button) findViewById(R.id.btnView);

int x1 = Integer.parseInt(t1.getText().toString());

int x2 = Integer.parseInt(t2.getText().toString());

int x3 = Integer.parseInt(t3.getText().toString());

btnChart.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

openChart();

}

private void openChart() {

int x1 = Integer.parseInt(t1.getText().toString());

int x2 = Integer.parseInt(t2.getText().toString());

int x3 = Integer.parseInt(t3.getText().toString());

int[] x = { 0,1,2 };

int [] income = {x1 ,x2, x3}; //ERROR POINT

XYSeries incomeSeries = new XYSeries("Pulse Rate");

XYSeries expenseSeries = new XYSeries("");

for(int i=0;i<x.length;i++){

incomeSeries.add(i,income[i]);

}

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

dataset.addSeries(incomeSeries);

dataset.addSeries(expenseSeries);

XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();

incomeRenderer.setColor(Color.rgb(130, 130, 230));

incomeRenderer.setFillPoints(true);

incomeRenderer.setLineWidth(2);

incomeRenderer.setDisplayChartValues(true);

XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();

expenseRenderer.setColor(Color.rgb(220, 80, 80));

expenseRenderer.setFillPoints(true);

expenseRenderer.setLineWidth(2);

expenseRenderer.setDisplayChartValues(true);

XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

multiRenderer.setXLabels(0);

multiRenderer.setChartTitle("Pulse Chart");

multiRenderer.setXTitle("Pulse rate");

multiRenderer.setYTitle("Days");

multiRenderer.setZoomButtonsVisible(true);

for(int i=0; i< x.length;i++){

multiRenderer.addXTextLabel(i, mMonth[i]);

}

multiRenderer.addSeriesRenderer(incomeRenderer);

multiRenderer.addSeriesRenderer(expenseRenderer);

Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);

startActivity(intent);

}

});

}

private void setListAdapter(SimpleCursorAdapter adapter) {

// TODO Auto-generated method stub

}

I Comment the //ERROR POINT in code segment.

When i give the values this point as a

 int [] income = {60 ,40, 50}; //It should Be working

Then it Should be Working.. But i want to put these values Using EditText boxes.

网友答案:

try to this line

int x1 = Integer.parseInt(t1.getText().toString());
网友答案:

try to this repalcing

    int x1 = Integer.parseInt(t1.getText().toString()).getIntValue();
网友答案:

As mentioned in the comments, take an Integer array of income -: Integer[] income = {x1,x2,x3};

Than you can use Arrays.asList(income)

网友答案:

Change your openChart method as follows:

private void openChart() {
    int x1 = Integer.parseInt(t1.getText().toString());
    int x2 = Integer.parseInt(t2.getText().toString());
    int x3 = Integer.parseInt(t3.getText().toString());

    int[] income = {x1 ,x2, x3};

}
网友答案:

if you want to convert from Integer to int then you should write code like

int x1 = Integer.parseInt(t1.getText().toString()).getIntValue();

then use the int value somewhere

网友答案:
final EditText t1 =(EditText)findViewById(R.id.textView1);
final EditText t2 =(EditText)findViewById(R.id.textView2);
final EditText t3 =(EditText)findViewById(R.id.textView3);

ArrayList<Integer> integersList = new ArrayList<Integer>();
integersList.add(Integer.parseInt(t1.getText().toString()));
integersList.add(Integer.parseInt(t2.getText().toString()));
integersList.add(Integer.parseInt(t3.getText().toString()));
网友答案:

Just replace your this code

int x1 = Integer.parseInt(t1.getText().toString());
 int x2 = Integer.parseInt(t2.getText().toString());
 int x3 = Integer.parseInt(t3.getText().toString());

form this

                int x1 = Integer.parseInt(t1.getText().toString()).getIntValue();;
                int x2 = Integer.parseInt(t2.getText().toString()).getIntValue();;
                int x3 = Integer.parseInt(t3.getText().toString()).getIntValue();;
分享给朋友:
您可能感兴趣的文章:
随机阅读: