platform.json
{
"platform": [{
"name": "Windows"
},
{
"name": "Linux"
},
{
"name": "MacOs"
}
]
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.samuray.myapplication.MainActivity">
<ListView
android:id="@+id/lvMain"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
res/layout/item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFF"
android:gravity="left">
<TextView
android:layout_height="25dp"
android:layout_width="wrap_content"
android:text="+15"
android:textColor="#000"
android:textSize="19sp"
android:id="@+id/name_item"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
MainActivity.java
package com.example.samuray.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ParseTask().execute();
}
private class ParseTask extends AsyncTask<Void, Void, String> {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resultJson = "";
@Override
protected String doInBackground(Void... params) {
try {
String $url_json = "http://www.anar8.ru/test/1/platform.json";
URL url = new URL($url_json);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
Log.d("FOR_LOG", resultJson);
} catch (Exception e) {
e.printStackTrace();
}
return resultJson;
}
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
final ListView lView = (ListView) findViewById(R.id.lvMain);
String[] from = {"name_item"};
int[] to = {R.id.name_item};
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hashmap;
try {
JSONObject json = new JSONObject(strJson);
JSONArray jArray = json.getJSONArray("platform");
for (int i = 0; i < jArray.length(); i++) {
JSONObject friend = jArray.getJSONObject(i);
String nameOS = friend.getString("name");
Log.d("FOR_LOG", nameOS);
hashmap = new HashMap<String, String>();
hashmap.put("name_item", "" + nameOS);
arrayList.add(hashmap);
}
final SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, arrayList, R.layout.item, from, to);
lView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
суббота, 26 декабря 2020 г.
How to display json data in listview . Android
Подписаться на:
Комментарии к сообщению (Atom)
Спасибо, мне нужен был такой пример.
ОтветитьУдалить