我正在寻找在Android中实现类似Twitter数据Feed的任何建议/方法(每个项目都是可点击的,并在点击时打开一个对话框/新活动屏幕)。 数据从服务器获取,在java中实现。有没有共同的模式/方式来做到这一点。 我们已经实现了服务器端代码,以JSON格式提供数据。 任何建议/例子/参考?
答案 0 :(得分:0)
尝试以下示例:
<强> MainActivity.java:强>
package com.androidbegin.jsonparsetutorial;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String RANK = "rank";
static String COUNTRY = "country";
static String POPULATION = "population";
static String FLAG = "flag";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("worldpopulation");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("rank", jsonobject.getString("rank"));
map.put("country", jsonobject.getString("country"));
map.put("population", jsonobject.getString("population"));
map.put("flag", jsonobject.getString("flag"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
<强> JSON:强>
{
"worldpopulation": [
{
"rank": 1,
"country": "China",
"population": "1,354,040,000",
"flag": "http://www.androidbegin.com/tutorial/flag/china.png"
},
{
"rank": 2,
"country": "India",
"population": "1,210,193,422",
"flag": "http://www.androidbegin.com/tutorial/flag/india.png"
},
{
"rank": 3,
"country": "United States",
"population": "315,761,000",
"flag": "http://www.androidbegin.com/tutorial/flag/unitedstates.png"
},
{
"rank": 4,
"country": "Indonesia",
"population": "237,641,326",
"flag": "http://www.androidbegin.com/tutorial/flag/indonesia.png"
},
{
"rank": 5,
"country": "Brazil",
"population": "193,946,886",
"flag": "http://www.androidbegin.com/tutorial/flag/brazil.png"
},
{
"rank": 6,
"country": "Pakistan",
"population": "182,912,000",
"flag": "http://www.androidbegin.com/tutorial/flag/pakistan.png"
},
{
"rank": 7,
"country": "Nigeria",
"population": "170,901,000",
"flag": "http://www.androidbegin.com/tutorial/flag/nigeria.png"
},
{
"rank": 8,
"country": "Bangladesh",
"population": "152,518,015",
"flag": "http://www.androidbegin.com/tutorial/flag/bangladesh.png"
},
{
"rank": 9,
"country": "Russia",
"population": "143,369,806",
"flag": "http://www.androidbegin.com/tutorial/flag/russia.png"
},
{
"rank": 10,
"country": "Japan",
"population": "127,360,000",
"flag": "http://www.androidbegin.com/tutorial/flag/japan.png"
}
]
}
请查看以下链接了解详情: