采用Android dynamicaly装入多个项目的列表视图需要帮助 [英] use android dynamicaly load more items to the listview need help
问题描述
包fshizzle.com;
进口java.io.IOException异常;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口android.app.ListActivity;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.SimpleAdapter.ViewBinder;
进口android.widget.Toast;
公共类迎宾扩展ListActivity {
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
意图发送= getIntent();
字符串登录= sender.getStringExtra(extra_login);
的setContentView(R.layout.listplaceholder);
ArrayList的< HashMap的<字符串,对象>> mylist中=新的ArrayList< HashMap的<字符串,对象>>();
JSONObject的JSON = JSONfunctions.getJSONfromURL(var_global.URL +JSON /列表视图/+登录+JSON);
尝试{
JSONArray地震= json.getJSONArray(相关信息);
的for(int i = 0; I< earthquakes.length();我++){
HashMap的<字符串,对象>图=新的HashMap<字符串,对象>();
JSONObject的E = earthquakes.getJSONObject(我);
map.put(ID,将String.valueOf(I));
map.put(名,相关信息名称:+ e.getString(用户));
map.put(时代,年龄:+ e.getString(时代));
URL pictureURL =新的URL(var_global.URL +上传/大拇指/+ e.getString(图像));
点阵位图= BitmapFactory.de codeStream(pictureURL.openStream());
map.put(IMG,位图);
//http://www.imagemagick.org/Usage/thumbnails/
mylist.add(图)
}
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
SimpleAdapter适配器=新SimpleAdapter(这一点,mylist中,R.layout.json2,新的String [] {IMG,姓名,年龄},
新的INT [] {R.id.item_img,R.id.item_title,R.id.item_subtitle});
adapter.setViewBinder(新MyViewBinder());
setListAdapter(适配器);
最终的ListView LV = getListView();
lv.setTextFilterEnabled(真正的);
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
@燮pressWarnings(未登记)
HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)lv.getItemAtPosition(位置);
Toast.makeText(welcome.this,ID+ o.get(ID)+'被点击了。,Toast.LENGTH_SHORT).show();
}
});
}
公共类MyViewBinder实现ViewBinder {
公共布尔setViewValue(查看视图,对象数据,字符串textRe presentation){
如果((视图的instanceof ImageView的)及(数据的instanceof位图)){
ImageView的IV =(ImageView的)观点;
位图BM =(位图)数据;
iv.setImageBitmap(BM);
返回true;
}
返回false;
}
}
}
这解析JSON文件,并创建图像的网络列表视图,我想利用这一code,但我不能<一个href="http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/" rel="nofollow">http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/
更有经验的人能告诉我怎么粘贴呢?谢谢 我无法显示我的JSON文件具有code中的相关信息在网址
我也试试这个code 的http:// benjii .ME / 2010/08 /无尽的滚动,列表视图功能于安卓/ 的,但我这个错误的方法setOnScrollListener(welcome.EndlessScrollListener)是未定义的类型SimpleAdapter以
adapter.setOnScrollListener(新EndlessScrollListener());
setListAdapter(适配器);
code。
通过这个code:
this.getListView()setOnScrollListener(新EndlessScrollListener())。
没有错误,但一切都显示相同的时间。 ????
什么LoadGigsTask()函数?我使用// LoadGigsTask是不确定的教程,也许是因为它,不是吗?
这是确定在code与
((AbsListView)this.view).setOnScrollListener(新EndlessScrollListener());
但是,在这条线仿真器崩溃了...
Finaly我用这个我现在要查找更新的列表视图......
this.getListView()。setOnScrollListener(新OnScrollListener(){
//这里没用,跳过!
公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){}
// dumdumdum
公共无效onScroll(AbsListView观点,诠释firstVisibleItem,
INT visibleItemCount,诠释totalItemCount){
//什么是底部ITEN是可见
INT lastInScreen = firstVisibleItem + visibleItemCount;
//是底部项目可见的放大器;没有加载更多了吗?装载更多 !
如果((lastInScreen == totalItemCount)及&安培;!(loadingMore)){
Toast.makeText(getApplicationContext(),行结束清单,Toast.LENGTH_LONG).show();
//线程的线程=新主题(空,loadMoreListItems);
//thread.start();
}
}
});
欲了解更多的相关信息在这里dynamicaly加载列表视图我的code使用;)
this.getListView()。setOnScrollListener(新OnScrollListener(){
//这里没用,跳过!
公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){}
// dumdumdum
公共无效onScroll(AbsListView观点,诠释firstVisibleItem,
INT visibleItemCount,诠释totalItemCount){
//什么是底部ITEN是可见
INT lastInScreen = firstVisibleItem + visibleItemCount;
//是底部项目可见的放大器;没有加载更多了吗?装载更多 !
如果((lastInScreen == totalItemCount)及及(loadingMore)){
Adapter.notifyDataSetChanged();
}
如果((lastInScreen == totalItemCount)及&安培;!(loadingMore)){
我++;
// envoie拉页阿列尔chercher上
字符串第3页= GETPAGE(var_global.URL +?listview.php登录=+登录+&放大器;网页=+ I);
Log.i(LOG_TAG,第3页);
JSONObject的JSON = JSONfunctions.getJSONfromURL(var_global.URL +JSON /列表视图/+登录+JSON);
尝试{
JSONArray地震= json.getJSONArray(相关信息);
的for(int i = 0; I&LT; earthquakes.length();我++){
HashMap的&LT;字符串,对象&gt;图=新的HashMap&LT;字符串,对象&gt;();
JSONObject的E = earthquakes.getJSONObject(我);
// Si用户==一个endlist
如果(e.getString(用户)。等于(endlistfshizzle)){
loadingMore = TRUE;
}
map.put(ID,将String.valueOf(I));
map.put(名,相关信息名称:+ e.getString(用户));
map.put(时代,年龄:+ e.getString(时代));
URL pictureURL =新的URL(var_global.URL +上传/大拇指/+ e.getString(图像));
点阵位图= BitmapFactory.de codeStream(pictureURL.openStream());
map.put(IMG,位图);
//http://www.imagemagick.org/Usage/thumbnails/
mylist.add(图)
}
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
Adapter.notifyDataSetChanged();
}
}
});
良好的编程可言!
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;
}
}
}
this parse a json file and create a listview with image web, I would like use this code but I can not http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/
someone more experienced can tell me how to paste all this ? thank you I can not to display The infos of my json files With the Code In The url
also i try this code http://benjii.me/2010/08/endless-scrolling-listview-in-android/ but i this error "The method setOnScrollListener(welcome.EndlessScrollListener) is undefined for the type SimpleAdapter" with
adapter.setOnScrollListener(new EndlessScrollListener());
setListAdapter(adapter);
code.
With this code:
this.getListView().setOnScrollListener(new EndlessScrollListener());
no error but all is display same time. ????
What the LoadGigsTask() function ? i use // LoadGigsTask is undefined in a tutorial maybe is it because of that, no ?
it's ok in code with
((AbsListView) this.view).setOnScrollListener(new EndlessScrollListener());
But in emulator crash in this line ...
Finaly i use this i search for update the listview now ...
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();
}
}
});
For more infos here my code use for dynamicaly load 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();
}
}
});
Good programming at all !!
这篇关于采用Android dynamicaly装入多个项目的列表视图需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!