use*_*866 1 android listview load dynamic
package fshizzle.com;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;
public class welcome extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent sender=getIntent();
String login = sender.getStringExtra("extra_login");
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
JSONObject json = JSONfunctions.getJSONfromURL(var_global.URL+"json/listview/"+login+".json");
try{
JSONArray earthquakes = json.getJSONArray("infos");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "infos name:" + e.getString("user"));
map.put("age", "age: " + e.getString("age"));
URL pictureURL = new URL(var_global.URL+"upload/thumbs/"+e.getString("image"));
Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
map.put("img",bitmap);
//http://www.imagemagick.org/Usage/thumbnails/
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleAdapter adapter = new SimpleAdapter(this, mylist, R.layout.json2, new String[] { "img", "name", "age" },
new int[] { R.id.item_img, R.id.item_title, R.id.item_subtitle });
adapter.setViewBinder(new MyViewBinder());
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(welcome.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
public class MyViewBinder implements ViewBinder {
public boolean setViewValue(View view, Object data,String textRepresentation) {
if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这解析了一个json文件并创建了一个带有图像web的listview,我想使用这段代码,但我不能http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview -never结束的列表/
有经验的人可以告诉我如何粘贴这一切?谢谢我无法使用代码在网址中显示我的json文件的信息
我也尝试这个代码http://benjii.me/2010/08/endless-scrolling-listview-in-android/但是这个错误"方法setOnScrollListener(welcome.EndlessScrollListener)是未定义的类型SimpleAdapter"与
adapter.setOnScrollListener(new EndlessScrollListener());
setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
码.
使用此代码:
this.getListView().setOnScrollListener(new EndlessScrollListener());
Run Code Online (Sandbox Code Playgroud)
没有错误,但所有都显示相同的时间.????
LoadGigsTask()函数是什么?我使用// LoadGigsTask在教程中未定义也许是因为它,不是吗?
它的代码没问题
((AbsListView) this.view).setOnScrollListener(new EndlessScrollListener());
但是在这行的模拟器崩溃中......
最后我用这个我现在搜索更新列表视图...
this.getListView().setOnScrollListener(new OnScrollListener(){
//useless here, skip!
public void onScrollStateChanged(AbsListView view, int scrollState) {}
//dumdumdum
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && !(loadingMore)){
Toast.makeText(getApplicationContext(), "OK END LIST.", Toast.LENGTH_LONG).show();
//Thread thread = new Thread(null, loadMoreListItems);
//thread.start();
}
}
});
Run Code Online (Sandbox Code Playgroud)
有关更多信息,我的代码用于动态加载listview;)
this.getListView().setOnScrollListener(new OnScrollListener(){
//useless here, skip!
public void onScrollStateChanged(AbsListView view, int scrollState) {}
//dumdumdum
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && (loadingMore)){
Adapter.notifyDataSetChanged();
}
if((lastInScreen == totalItemCount) && !(loadingMore)){
i++;
//on envoie la page a aller chercher
String page3 = getPage(var_global.URL+"listview.php?login="+login+"&page="+i);
Log.i(LOG_TAG, page3);
JSONObject json = JSONfunctions.getJSONfromURL(var_global.URL+"json/listview/"+login+".json");
try{
JSONArray earthquakes = json.getJSONArray("infos");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject e = earthquakes.getJSONObject(i);
//si user == a endlist
if (e.getString("user").equals("endlistfshizzle")) {
loadingMore = true;
}
map.put("id", String.valueOf(i));
map.put("name", "infos name:" + e.getString("user"));
map.put("age", "age: " + e.getString("age"));
URL pictureURL = new URL(var_global.URL+"upload/thumbs/"+e.getString("image"));
Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
map.put("img",bitmap);
//http://www.imagemagick.org/Usage/thumbnails/
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Adapter.notifyDataSetChanged();
}
}
});
Run Code Online (Sandbox Code Playgroud)
好编程!
| 归档时间: |
|
| 查看次数: |
11689 次 |
| 最近记录: |