Java 如何在Android中集成Pull到RefreshListview 公共类AndroidJSONParsingActivity扩展了ListActivity{ //发出请求的url 专用静态字符串url=”http://api.androidhive.info/contacts/"; //JSON节点名称 专用静态最终字符串标记_CONTACTS=“CONTACTS”; 私有静态最终字符串标记\u ID=“ID”; 私有静态最终字符串标记_NAME=“NAME”; 私有静态最终字符串标记\u EMAIL=“EMAIL”; 私有静态最终字符串标记_ADDRESS=“ADDRESS”; 私有静态最终字符串标记_GENDER=“GENDER”; 专用静态最终字符串标记_PHONE=“PHONE”; 专用静态最终字符串标记\u PHONE\u MOBILE=“MOBILE”; 专用静态最终字符串标记\u PHONE\u HOME=“HOME”; 专用静态最终字符串标记\u PHONE\u OFFICE=“OFFICE”; 静态最终int菜单\手动\刷新=0; 静态最终整数菜单\禁用\滚动=1; 静态最终整数菜单\设置\模式=2; 私有链接列表; //私有PullToRefreshView mPullRefreshListView; //联系JSONArray JSONArray联系人=null; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //ListView的Hashmap ArrayList contactList=新建ArrayList(); //创建JSON解析器实例 JSONParser jParser=新的JSONParser(); //从URL获取JSON字符串 JSONObject json=jParser.getJSONFromUrl(url); 试一试{ //获取联系人数组 contacts=json.getJSONArray(TAG_contacts); //通过所有触点循环 对于(int i=0;ivalue 地图放置(标签标识,标识); 地图放置(标签名称、名称); 地图放置(标签、电子邮件、电子邮件); 地图放置(标签、手机、手机); //将哈希列表添加到ArrayList 联系人列表。添加(地图); } }捕获(JSONException e){ e、 printStackTrace(); } } }
这是我的解析代码,我现在可以得到字符串值,我已经在我的项目中加入了JsonParser类Java 如何在Android中集成Pull到RefreshListview 公共类AndroidJSONParsingActivity扩展了ListActivity{ //发出请求的url 专用静态字符串url=”http://api.androidhive.info/contacts/"; //JSON节点名称 专用静态最终字符串标记_CONTACTS=“CONTACTS”; 私有静态最终字符串标记\u ID=“ID”; 私有静态最终字符串标记_NAME=“NAME”; 私有静态最终字符串标记\u EMAIL=“EMAIL”; 私有静态最终字符串标记_ADDRESS=“ADDRESS”; 私有静态最终字符串标记_GENDER=“GENDER”; 专用静态最终字符串标记_PHONE=“PHONE”; 专用静态最终字符串标记\u PHONE\u MOBILE=“MOBILE”; 专用静态最终字符串标记\u PHONE\u HOME=“HOME”; 专用静态最终字符串标记\u PHONE\u OFFICE=“OFFICE”; 静态最终int菜单\手动\刷新=0; 静态最终整数菜单\禁用\滚动=1; 静态最终整数菜单\设置\模式=2; 私有链接列表; //私有PullToRefreshView mPullRefreshListView; //联系JSONArray JSONArray联系人=null; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //ListView的Hashmap ArrayList contactList=新建ArrayList(); //创建JSON解析器实例 JSONParser jParser=新的JSONParser(); //从URL获取JSON字符串 JSONObject json=jParser.getJSONFromUrl(url); 试一试{ //获取联系人数组 contacts=json.getJSONArray(TAG_contacts); //通过所有触点循环 对于(int i=0;ivalue 地图放置(标签标识,标识); 地图放置(标签名称、名称); 地图放置(标签、电子邮件、电子邮件); 地图放置(标签、手机、手机); //将哈希列表添加到ArrayList 联系人列表。添加(地图); } }捕获(JSONException e){ e、 printStackTrace(); } } },java,android,Java,Android,这是我的解析代码,我现在可以得到字符串值,我已经在我的项目中加入了JsonParser类 public class AndroidJSONParsingActivity extends ListActivity { // url to make request private static String url = "http://api.androidhive.info/contacts/"; // JSON Node names private stati
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;
private LinkedList<String> mListItems;
//private PullToRefreshView mPullRefreshListView;
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_EMAIL, email);
map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
公共类PullToRefreshListActivity扩展活动{
静态最终int菜单\手动\刷新=0;
静态最终整数菜单\禁用\滚动=1;
静态最终整数菜单\设置\模式=2;
私有PullToRefreshView mPullRefreshListView;
私人ArrayaAdapter mAdapter;
专用静态字符串url=”http://api.androidhive.info/contacts/";
/**在首次创建活动时调用*/
私有链接列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.pull\u to\u refresh\u listview);
mPullRefreshListView=(PullToRefreshView)findViewById(R.id.pull\u refresh\u list);
//设置要在刷新列表时调用的侦听器。
mPullRefreshListView.setOnRefreshListener(新的OnRefreshListener(){
@凌驾
公共void onRefresh(){
mPullRefreshListView.setLastUpdatedLabel(DateUtils
.formatDateTime(getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT\u SHOW\u TIME
|DateUtils.FORMAT\u SHOW\u DATE
|DateUtils.FORMAT_ABBREV_ALL));
//请务必刷新此处的列表。
新建GetDataTask().execute();
}
});
ListView actualListView=mPullRefreshListView.getRefreshableView();
mListItems=新链接列表();
addAll(Arrays.asList(mStrings));
mAdapter=新阵列适配器(此,
android.R.layout.simple_list_item_1,mListItems);
//您也可以只使用setListAdapter(mAdapter)
actualListView.setAdapter(mAdapter);
}
私有类GetDataTask扩展了AsyncTask{
@凌驾
受保护字符串[]doInBackground(Void…params){
//模拟后台作业。
试一试{
《睡眠》(2000年);
}捕捉(中断异常e){
}
返回mstring;
}
@凌驾
受保护的void onPostExecute(字符串[]结果){
多学院
.addFirst(“刷新后添加..新版本即将推出J-elly Bean”);
mAdapter.notifyDataSetChanged();
//刷新列表后,调用onRefreshComplete。
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(结果);
}
}
私有字符串[]mStrings={“A-stro”、“B-ender”、“C-upcake”、“E-clair”,
“F-royo”、“G-ingerbread”、“H-oneycomb”、“I-ce奶油三明治”,
“来自iamvijayakumar.blogspot.com的Android版本”};
}
这是我所做的PullToRefreshListview的虚拟示例。现在我想集成,这样我就可以在ListView中显示数据,并使用Pull来刷新。你能帮我整合一下吗?我将在哪里放置用于解析的代码以及所有??请帮帮我。在这个虚拟示例中,您使用下面用于使用Hashmap列表设置数据的命令来创建简单的替换适配器。标签ID等用于
public class PullToRefreshListActivity extends Activity {
static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;
private PullToRefreshView mPullRefreshListView;
private ArrayAdapter<String> mAdapter;
private static String url = "http://api.androidhive.info/contacts/";
/** Called when the activity is first created. */
private LinkedList<String> mListItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh_listview);
mPullRefreshListView = (PullToRefreshView) findViewById(R.id.pull_refresh_list);
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
mPullRefreshListView.setLastUpdatedLabel(DateUtils
.formatDateTime(getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL));
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
ListView actualListView = mPullRefreshListView.getRefreshableView();
mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListItems);
// You can also just use setListAdapter(mAdapter)
actualListView.setAdapter(mAdapter);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems
.addFirst("Added after refresh..New Version Coming soon J-elly Bean");
mAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings = { "A-stro", "B-ender", "C-upcake", "E-clair",
"F-royo", "G-ingerbread", "H-oneycomb", "I-ce Cream Sandwich ",
"Android Versions From iamvijayakumar.blogspot.com" };
mAdapter= new SimpleAdapter(this, mylist, R.layout.row,
new String[] {TAG_ID, TAG_NAME, TAG_EMAIL,TAG_PHONE_MOBILE}, new int[]
{R.id.tvtagid, R.id.tvtagname, R.id.tvtagemail,R.id.tvtagphone});