Ich arbeite an einer Android App. Ich möchte, dass die App zufällig einen Namen von json auswählt. Hier ist die json:Android, zufällig von externen JSON
{
"user": [
{
"id": "001",
"name": "Raj Amal",
"email": "[email protected]"
},
{
"id": "002",
"name": "Raj",
"email": "[email protected]"
}
]
}
Und hier ist mein Android-Code:
public class MainActivity extends Activity {
\t TextView uid;
\t TextView name1;
\t TextView email1;
\t Button Btngetdata;
\t
\t //URL to get JSON Array
\t private static String url = "http://weblink/json/index.php";
\t
\t //JSON Node Names
\t private static final String TAG_USER = "user";
\t private static final String TAG_ID = "id";
\t private static final String TAG_NAME = "name";
\t private static final String TAG_EMAIL = "email";
\t
\t JSONArray user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btngetdata = (Button)findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
\t \t \t
\t \t \t @Override
\t \t \t public void onClick(View view) {
\t \t new JSONParse().execute();
\t \t \t \t
\t \t \t }
\t \t });
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
\t private ProgressDialog pDialog;
\t @Override
protected void onPreExecute() {
super.onPreExecute();
uid = (TextView)findViewById(R.id.uid);
\t \t \t name1 = (TextView)findViewById(R.id.name);
\t \t \t email1 = (TextView)findViewById(R.id.email);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
\t }
\t
\t @Override
protected JSONObject doInBackground(String... args) {
\t \t JSONParser jParser = new JSONParser();
\t \t // Getting JSON from URL
\t \t JSONObject json = jParser.getJSONFromUrl(url);
\t \t return json;
\t }
\t @Override
protected void onPostExecute(JSONObject json) {
\t \t pDialog.dismiss();
\t \t try {
\t \t \t \t // Getting JSON Array
\t \t \t \t user = json.getJSONArray(TAG_USER);
\t \t \t \t JSONObject c = user.getJSONObject(0);
\t \t \t \t
\t \t \t \t // Storing JSON item in a Variable
\t \t \t \t String id = c.getString(TAG_ID);
\t \t \t \t String name = c.getString(TAG_NAME);
\t \t \t \t String email = c.getString(TAG_EMAIL);
\t \t \t \t
\t \t \t \t
\t \t \t \t //Set JSON Data in TextView
\t \t \t \t uid.setText(id);
\t \t \t \t name1.setText(name);
\t \t \t \t email1.setText(email);
\t \t \t
\t \t } catch (JSONException e) {
\t \t \t e.printStackTrace();
\t \t }
\t \t
\t }
}`
würde ich, wenn ich die Taste ein zufälliger Name presse Shows und Schleifen.
Bitte helfen Danke
eine Liste mit Namen erstellen und eine Zufallszahl verwenden Namen von der Liste –
Sie auswählen Vielen Dank für Ihre Antwort, ist das, was ich zu tun versuche ich brauche eine zufällige ID auswählen und den Namen mit der Show gleiche ID. Aber ich weiß wirklich nicht, wie man Vielen Dank – user3380879