I was trying to make a graph based on the two array values, i.e conc and optical density. I have created two activities for saving those arrays and pass the array list using bundle and intent to the third activity using string.
Whenever I try to run the code, my app crashes.
GraphView gpView;
String co;
String od;
Float x,y;
int l1,l2;
DataPoint [] dp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Bundle bundle1 = getIntent().getExtras();
ArrayList<String> array1 = (ArrayList<String>) bundle1.getStringArrayList("Conc");
Bundle bundle2 = getIntent().getExtras();
ArrayList<String> array2 = (ArrayList<String>) bundle2.getStringArrayList("Od");
gpView= findViewById(R.id.graph);
LineGraphSeries <DataPoint> series = new LineGraphSeries<>();
l1 = array1.size();
l2 = array2.size();
if((array1.contains(true))&&(array2.contains(true)))
{
for (int c1 = 0; c1<l1; c1++)
{
for(int c2=0; c2<l2; c2++ ) {
co = array1.get(c1);
od = array2.get(c2);
x = Float.parseFloat(co);
y = Float.parseFloat(od);
dp[c1] = new DataPoint(x,y);
}
}
gpView.addSeries(series);
}
}
Alternately I tried to run a simple input method and it worked using this
private DataPoint[] getDataPoint ()
{
dp = new DataPoint[]
{
new DataPoint(10, 0.2),
new DataPoint(20, 0.38),
new DataPoint(30, 0.45),
new DataPoint(40, 0.69),
new DataPoint(50, 0.81),
};
return (dp);
}
I think you haven't initialized your dp array. Initialize your array
dp = new DataPoint[l1];
before if statement if((array1.contains(true))&&(array2.contains(true)))
Edit:
May be you are having NullPointerException. Check array1 and array2 is not null
if(array1 == null){
Toast.makeText(this, "array1 is null", Toast.LENGTH_LONG).show();
}
if(array2 == null){
Toast.makeText(this, "array2 is null", Toast.LENGTH_LONG).show();
}
if(array1 != null){
l1 = array1.size();
}
if(array2 != null){
l2 = array2.size();
}
if(l1 > 0){
dp = new DataPoint[l1];
for (int c1 = 0; c1<l1; c1++)
{
for(int c2=0; c2<l2; c2++ ) {
co = array1.get(c1);
od = array2.get(c2);
x = Float.parseFloat(co);
y = Float.parseFloat(od);
dp[c1] = new DataPoint(x,y);
}
}
gpView.addSeries(series);
}
if array1 and array2 is null then make sure you are passing both arraylist via Intent.
Related
I have a data that i need to pass as Double[],
my data is in double[], and i need to pass as Double[] because i need
to change it into List<Double>
Here is my code
ListFragment
#Override
public void OnEditItem(int position) {
Model selectedItem = mModel.get(position);
String selectedKey = selectedItem.getKey();
String selectedImage = selectedItem.getImagesUri();
String selecteDescriptions = selectedItem.getImageDescription();
String selectedImagesName = selectedItem.getImageNames();
List<Double> selectedataSet = selectedItem.getDataSet();
double[] zeroSet = new double[selectedataSet.size()];
for(int i=0; i<zeroSet.length; i++){
zeroSet[i] = selectedataSet.get(i);
}
Log.d(TAG, "thisList: " + datacel);
EditFragment editFragment = new EditFragment();
FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager()
.beginTransaction();
final Bundle bundle = new Bundle();
bundle.putString("Label", selectedKey);
bundle.putString("Image", selectedImage);
bundle.putString("Descriptions", selecteDescriptions);
bundle.putString("ImagesName", selectedImagesName);
And this is the code that receive the double[]
EditFragment
private void updateView(View view) {
if(getArguments()!=null){
String labelId = getArguments().getString("Label");
String imagesBundle = getArguments().getString("Image");
String descriptionsBundle =getArguments().getString("Descriptions");
String imagesNameBundle =getArguments().getString("ImagesName");
double[] dataSet = getArguments().getDoubleArray("DataSet");
//this i where i need to change into Double[];
Log.d(TAG, "thisLIst: " + dataSet);
etName.setText(String.valueOf(imagesNameBundle));
etDescribe.setText(descriptionsBundle);
Picasso.get().load(imagesBundle).into(ImageInput);
simpleActivity.setDataToListView(value, listViewDataSet,context); // this value need Double[];
Edited
i think my error starting from here,
double[] zeroSet = new
double[selectedataSet.size()];
for(int i=0; i<zeroSet.length; i++){
zeroSet[i] = selectedataSet.get(i);
}
because the data i got just like this,
how can i solve this?
It's not complicated at all. You just have to copy the array elements yourself, like
double[] dataSet = getArguments().getDoubleArray("DataSet");
Double[] boxedDataSet = new Double[dataSet.length];
for ( int i = 0; i < dataSet.length; i++ ) {
boxedDataSet[i] = dataSet[i]; // Invokes auto-boxing
// Equivalent, more explicit alternative:
// boxedDataSet[i] = Double.valueOf(dataSet[i]);
}
Try something like this:
import java.util.Arrays;
class BoxArrayExample {
public static void main(String[] args) {
double[] arr = new double[] {1.0, 2.0, 3.0};
System.out.println(String.format("Unboxed array: %s", Arrays.toString(arr)));
Double[] boxedArr = Arrays.stream(arr)
.boxed()
.toArray(Double[]::new);
System.out.println(String.format("Boxed array: %s", Arrays.toString(boxedArr)));
}
}
Output:
Unboxed array: [1.0, 2.0, 3.0]
Boxed array: [1.0, 2.0, 3.0]
int size = mcq.size();
String arr[] = null;
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr[i] = tv1.getText().toString();
// Log.e("Array",arr[i]);
} else if (op2.isPressed()) {
arr[i] = tv2.getText().toString();
//Log.e("Array",arr[i]);
} else if (op3.isPressed()) {
arr[i] = tv3.getText().toString();
// Log.e("Array",arr[i]);
} else if (op4.isPressed()) {
arr[i] = tv4.getText().toString();
//Log.e("Array",arr[i]);
}
I am trying to store the data in an array when the button is pressed,but it always shows null.And when the for loop is over I want to display my array.
here , Arrays in Java have a size so u cannot do like this. Instead of this
use list,
ArrayList<String> arr = new ArrayList<String>();
Inorder to do using String array :
String[] arr = new String[SIZEDEFNE HERE];
For ur answer :
ArrayList<String> arr = new ArrayList<String>();
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr.add(tv1.getText().toString());
} else if (op2.isPressed()) {
arr.add(tv2.getText().toString());
} else if (op3.isPressed()) {
arr.add(tv3.getText().toString());
} else if (op4.isPressed()) {
arr.add(tv4.getText().toString());
}
Retrive value using
String s = arr.get(0);
This is because your string array is null. Declare it with the size as
String[] arr = new String[size];
Try Array List. use the following code in your main java
final ArrayList<String> list = new ArrayList<String>();
Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
list.add(editText.getText().toString());
}
});
To avoid duplication in arraylist .You can use arraylist for faster then String array
ArrayList<String> list = new ArrayList<String>();
list.add("Krishna");
list.add("Krishna");
list.add("Kishan");
list.add("Krishn");
list.add("Aryan");
list.add("Harm");
System.out.println("List"+list);
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);
i have a graph view to implement an analysis graph
All the x-axis and y-axis data is get from the sqlite db data to show the output
x-axis is showing date
y-axis is showing weight
but I have no idea to implement them out, I am stuck,
below is my code but its wrong and I haven't completed yet, can someone help me to solve and build
DBHelperNote connect = new DBHelperNote(getActivity());
SQLiteDatabase db = connect.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM weight;",null);
String[] weight = new String[cursor.getCount()];
int i = 0;
while(cursor.moveToNext()){
d = cursor.getString(cursor.getColumnIndex("weightnum"));
weight[i] = d;
i++;
}
String[] date = new String[cursor.getCount()];
int r = 0;
while(cursor.moveToNext()){
e = cursor.getString(cursor.getColumnIndex("date"));
date[r] = e;
r++;
}
GraphView line_graph = (GraphView) contentView.findViewById(R.id.graph);
LineGraphSeries<DataPoint> line_series =
new LineGraphSeries<DataPoint>(new DataPoint[] {
>> here "while" getting error
while ( a!=weight.length) {
new DataPoint(Integer.parseInt(weight[i]), Integer.parseInt(date[i]));
i++;
}
}
);
line_graph.addSeries(line_series);
line_series.setDrawDataPoints(true);
line_series.setDataPointsRadius(10);
line_series.setOnDataPointTapListener(new OnDataPointTapListener() {
#Override
public void onTap(Series series, DataPointInterface dataPoint) {
Toast.makeText(getActivity(), "Series: On Data Point clicked: " + dataPoint, Toast.LENGTH_SHORT).show();
}
});
Hmn, it might be an error with the data type in their example they use a double instead of an int.
http://www.android-graphview.org/documentation/category/live-chart
private DataPoint[] generateData() {
int count = 30;
DataPoint[] values = new DataPoint[count];
for (int i=0; i<count; i++) {
double x = i;
double f = mRand.nextDouble()*0.15+0.3;
double y = Math.sin(i*f+2) + mRand.nextDouble()*0.3;
DataPoint v = new DataPoint(x, y);
values[i] = v;
}
return values;
I am creating an app, in which I create some EditTexts dynamically with ID number.
I want to pass the information from the EditTexts, so I tried to create a EditText array with these and then use the .getText().toString() to save them in a String Array, which I want pass to the next activity.
It seems like it won't create the "editArray[]" in second code part correctly.
Thanks in advance.
Here's my code (EnterNames.java) - Creation of EditTexts -> Succesful
protected void NumberOfEditText()
{
View VertLayout = (LinearLayout) findViewById(R.id.VertLayout);
String SpinValue = getIntent().getExtras().getString("SpinValue");
int intSpinValue = Integer.valueOf(SpinValue);
editTextCount = intSpinValue;
EditText[] editTextArray = new EditText[editTextCount];
for (int i = 0; i < editTextCount; i++)
{
String Name = "Name " + (i+1);
editTextArray[i] = new EditText(this);
editTextArray[i].setId(i+1000);
editTextArray[i].setText(Name);
editTextArray[i].setTextSize(20);
editTextArray[i].setFilters( new InputFilter[] { new InputFilter.LengthFilter(15) } );
editTextArray[i].setSelectAllOnFocus(true);
editTextArray[i].setSingleLine(true);
editTextArray[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) VertLayout).addView(editTextArray[i]);
}
}
Second code (EnterNames.java) - Passing data to next activity -> Failure.
By testing I think the problem is the for-loops (the editArray returns null)
public void Go(View view)
{
setContentView(R.layout.activity_enter_names);
String NameArray [] = new String [editTextCount];
EditText editArray [] = new EditText [editTextCount];
Intent intent = new Intent(this, RandomGeneration.class);
for (int i = 0; i < editTextCount; i++)
{
editArray[i] = (EditText) findViewById(i+1000);
}
for (int i = 0; i < editTextCount; i++)
{
NameArray[i] = editArray[i].getText().toString();
}
Bundle extras = new Bundle();
extras.putInt("NumberofNames", editTextCount);
extras.putStringArray("NameArray", NameArray);
intent.putExtras(extras);
startActivity(intent);
}
According to Activity.findViewById() documentation, this method will search for views in your XML, not in views added in Java.
The simple way to do what you want:
class EnterNames extends Activity {
private int editTextCount;
private EditText[] editTextArray;
protected void NumberOfEditText() {
LinearLayout vertLayout = (LinearLayout) findViewById(R.id.VertLayout);
editTextCount = Integer.valueOf(getIntent().getExtras().getString("SpinValue"));
editTextArray = new EditText[editTextCount];
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (int i = 0; i < editTextCount; i++) {
String Name = "Name " + (i+1);
editTextArray[i] = new EditText(this);
editTextArray[i].setText(Name);
editTextArray[i].setTextSize(20);
editTextArray[i].setFilters( new InputFilter[] { new InputFilter.LengthFilter(15) } );
editTextArray[i].setSelectAllOnFocus(true);
editTextArray[i].setSingleLine(true);
editTextArray[i].setLayoutParams(params);
vertLayout.addView(editTextArray[i]);
}
}
[…]
public void Go(View view)
{
// This will delete views added to your LinearLayout, don't use it!
//setContentView(R.layout.activity_enter_names);
String nameArray[] = new String[editTextCount];
for (int i = 0; i < editTextCount; i++) {
nameArray[i] = editArray[i].getText().toString();
}
Bundle extras = new Bundle();
extras.putInt("NumberofNames", editTextCount);
extras.putStringArray("NameArray", nameArray);
Intent intent = new Intent(this, RandomGeneration.class);
intent.putExtras(extras);
startActivity(intent);
}
}
Why you are calling setContentView(R.layout.activity_enter_names); in public void Go(View view)? You overide Activities layout in which you previously has added EditText views and all your findById() returns null.
Your code should work if you remove setContentView(R.layout.activity_enter_names); from Go function.
setContentView(R.layout.activity_enter_names); should be called single time in Activities onCreate function.
Hi can some one suggest me a sample example of how i can sort the textviews based on the numbers in textviews. I am able to get the text from the TextViews need to sort and place the lowest number first.
Thank you.
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
//here I want to assign sorted numberes to the TextViews
}
}
So I have followed Jeffrey's advice. Here is the code which still doesn't work properly. What could be wrong?
Created an array of TextViews:
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
tvs[2] = textView43;
tvs[3] = textView53;
tvs[4] = textView63;
tvs[5] = textView73;
tvs[6] = textView83;
Sorted the array and assinged new values to the TextViews:
Arrays.sort(tvs, new TVTextComparator());
textView23.setText(tvs[0].getText().toString());
textView33.setText(tvs[1].getText().toString());
textView43.setText(tvs[2].getText().toString());
textView53.setText(tvs[3].getText().toString());
textView63.setText(tvs[4].getText().toString());
textView73.setText(tvs[5].getText().toString());
textView83.setText(tvs[6].getText().toString());
And here is the Comporator class:
public class TVTextComparator implements Comparator<TextView> {
public int compare(TextView lhs, TextView rhs) {
Integer oneInt = Integer.parseInt(lhs.getText().toString());
Integer twoInt = Integer.parseInt(rhs.getText().toString());
return oneInt.compareTo(twoInt);
}
}
to sort your textViews, first put them in an array,
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
// and so on
note that if you have handle to the parent container, you could easily build the array by using ViewGroup.getChildCount() and getChildAt().
now write a comparator for a text view,
class TVTextComparator implements Comparator<TextView> {
#Override
public int compare(TextView lhs, TextView rhs) {
return lhs.getText().toString().compareTo(rhs.getText().toString());
// should check for nulls here, this is NOT a robust impl of compare()
}
}
now use the comparator to sort the array,
Arrays.sort(tvs, 0, tvs.length, new TVTextComparator());
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
textView23.setText(intValues[0]);
textView33.setText(intValues[1]);
textView43.setText(intValues[2]);
textView53.setText(intValues[3]);
textView63.setText(intValues[4]);
textView73.setText(intValues[5]);
textView83.setText(intValues[6]);
}