Can I use two spinners to retrieve data from a jav

2019-09-07 10:43发布

问题:

What I want to achieve is to use to spinners to select from town and then to town and then to retrieve the distance and time of travel that i have defined in my dist.js file. It works great when i use html in webview. but I don't want to use webview. Therefore how can i get the two spinners to calculate this?


Okay have tried but still nothing works. here is my code so far. Can you tell what i am doing wrong?

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import org.json.JSONArray;

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

public class MainActivity  extends Activity{ 

Spinner s1; 
Spinner s2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 


s1  = (Spinner) findViewById(R.id.spinner1);
s2  = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.camp_sites, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
InputStream is = context.getResources().openRawResource(R.raw.dist); 
Writer writer = new StringWriter(is.available()); 
char[] buffer = new char[1024]; 
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); {
try { 
    int n; 
    while ((n = reader.read(buffer)) != -1) { 
        writer.write(buffer, 0, n); 
    } 
} finally { 
    reader.close(); 
} 
String jsonString = writer.toString(); 

JSONArray json = new JSONArray(jsonString);}
}


public void onNothingSelected(AdapterView<?> parent) {

   }
}

Thank you for helping me on this problem!!!

回答1:

Load the js file, get an array of the town names, and put them in an ArrayAdapter. Then pass the adapter to your spinner:

spinner.setAdapter(adapter);

Do this once for each of your two spinners.

More on spinners: http://developer.android.com/guide/topics/ui/controls/spinner.html

Update

To load the js file (I assume it contains a JSON array), you can put it in resources/raw and do this:

// open the input stream
InputStream is = context.getResources().openRawResource(R.raw.dist);

// read into a buffer
// NOTE: This is ugly, and there may be a better way, but it works.
Writer writer = new StringWriter(is.available());
char[] buffer = new char[1024];
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
try {
    int n;
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
} finally {
    reader.close();
}
String jsonString = writer.toString();

// load into a JSONArray
// if your file contains a single object, use JSONObject instead
JSONArray json = new JSONArray(jsonString);

If your json file is really big, there is a way to parse a stream into JSON without reading the whole thing first, but it's more complicated. I recommend Jackson for that, and really for all your JSON needs in general. Gson is another option.