在我的代码中,我从php页面获取了JSON格式的数据,然后将它们放在SQLite创建的本地数据库中,我希望在ListView中显示结果。
MainActivity.java
package com.example.ismail.test;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONObject;
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 {
SQLiteDatabase articlesDB;
ArrayList<HashMap<String, String>> pharmatList = new ArrayList<HashMap<String, String>>();
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> adresses = new ArrayList<String>();
ArrayList<String> tels = new ArrayList<String>();
ListAdapter adapter;
ArrayList<String> urls = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);
adapter = new SimpleAdapter(
MainActivity.this, pharmatList,
R.layout.list_item, new String[]{"title", "adress", "tel"}, new int[]{R.id.name,
R.id.adress, R.id.tel});
listView.setAdapter(adapter);
/* listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), ArticleActivity.class);
// i.putExtra("tel", urls.get(position));
// i.putExtra("content", content.get(position));
startActivity(i);
}
});*/
articlesDB = this.openOrCreateDatabase("pharmacie", MODE_PRIVATE, null);
articlesDB.execSQL("CREATE TABLE IF NOT EXISTS pharmacie ( pharmacieID INTEGER PRIMARY KEY, title VARCHAR, address VARCHAR, tel VARCHAR)");
updateListView();
DownloadTask task = new DownloadTask();
try {
task.execute("http://www.pfefes.tk/Pharmacie/pharmacie%20de%20garde/Agdal.php");
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateListView() {
try {
Cursor c = articlesDB.rawQuery("SELECT * FROM pharmacie ORDER BY pharmacieID DESC", null);
int telIndex = c.getColumnIndex("tel");
int adressIndex = c.getColumnIndex("address");
int titleIndex = c.getColumnIndex("title");
titles.clear();
adresses.clear();
tels.clear();
if (c != null && c.moveToFirst()) {
do {
titles.add(c.getString(titleIndex));
adresses.add(c.getString(adressIndex));
tels.add(c.getString(telIndex));
} while (c.moveToNext());
}
c.close();
// ((ListAdapter) .getAdapter()).notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
String pharmacieInfo = "";
while (data != -1) {
char current = (char) data;
pharmacieInfo += current;
data = reader.read();
}
JSONObject jsonObject = new JSONObject(pharmacieInfo);
JSONArray parentArray = jsonObject.getJSONArray("pharma");
articlesDB.execSQL("DELETE FROM pharmacie");
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
String pharmacieTitle = finalObject.getString("name");
String pharmacieAdress = finalObject.getString("adress");
String pharmacieTel = finalObject.getString("tel");
int j = 1;
HashMap<String, String> pharmaciee = new HashMap<String, String>();
pharmaciee.put("name", pharmacieTitle);
pharmaciee.put("adrerss", pharmacieAdress);
pharmaciee.put("tel", pharmacieTel);
pharmatList.add(pharmaciee);
String sql = "INSERT INTO pharmacie (pharmacieID, title, address, tel) VALUES (?, ?, ?, ?)";
SQLiteStatement statement = articlesDB.compileStatement(sql);
String strI = "" + j;
int k = 0;
statement.bindString(1, strI);
statement.bindString(2, pharmatList.get(k).get("name"));
statement.bindString(3, pharmatList.get(k).get("adrerss"));
statement.bindString(4, pharmatList.get(k).get("tel"));
k++;
statement.execute();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
updateListView();
}
}
}
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ismail.test.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/adress"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:0)
您应在向adapter.notifyDataSetChanged()
添加元素后致电pharmatList
。