Java &引用;returnRes不能解析为变量;android中的错误
我已经在一个虚拟项目中测试了无止境滚动函数的功能,它工作得很好,但是如果我在我的原始项目中粘贴相同的代码,在变量“loadMoreListItems”上得到“loadMoreListItems不能解析为变量”,在变量上得到“returnRes不能解析为变量”,即使我检查了android.R导入和布局名称,一切都是正确的,不知道哪里出错了Java &引用;returnRes不能解析为变量;android中的错误,java,android,listview,scroll,Java,Android,Listview,Scroll,我已经在一个虚拟项目中测试了无止境滚动函数的功能,它工作得很好,但是如果我在我的原始项目中粘贴相同的代码,在变量“loadMoreListItems”上得到“loadMoreListItems不能解析为变量”,在变量上得到“returnRes不能解析为变量”,即使我检查了android.R导入和布局名称,一切都是正确的,不知道哪里出错了 public class Home extends ListActivity { ArrayList<HashMap<String, String
public class Home extends ListActivity {
ArrayList<HashMap<String, String>> songsList;
ListView list;
LazyAdapter adapter;
JSONArray posts;
LinearLayout line1,line2;
ImageView menu;
boolean loadingMore = false;
ArrayList<String> songsList1;
LayoutInflater inflater;
static int jsonpage = 0;
JSONParser jParser;
JSONObject json;
TextView loadtext;
// All static variables
static final String URL = "http://www.exm.com/?json=get_recent_posts";
static final String KEY_POSTS = "posts";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(android.R.id.list);
// get the LayoutInflater for inflating the customomView
// this will be used in the custom adapter
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
songsList = new ArrayList<HashMap<String, String>>();
_loaddata();
LayoutInflater li = LayoutInflater.from(getBaseContext());
View footerView = li.inflate(com.exm.com.R.layout.listfooter, null);
loadtext = (TextView) footerView.findViewById(R.id.empty);
loadtext.setEnabled(false);
loadtext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "poda",
Toast.LENGTH_LONG).show();
}
});
this.getListView().addFooterView(footerView);
// Getting adapter by passing json data ArrayList
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(
getApplicationContext(),
"Click ListItem Number "
+ songsList.get(position).get("title"),
Toast.LENGTH_LONG).show();
}
});
this.getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// int first = view.getFirstVisiblePosition();
// int count = view.getChildCount();
//
// if (scrollState == SCROLL_STATE_IDLE
// || (first + count > adapter.getCount())) {
// list.invalidateViews();
// }
if (scrollState == SCROLL_STATE_IDLE) {
_loaddata();
adapter.notifyDataSetChanged();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
@SuppressWarnings("unused")
private void _loaddata() {
try {
// getting JSON string from URL
jParser = new JSONParser();
jsonpage = jsonpage + 1;
json = jParser
.getJSONFromUrl("http://india.exm.net/ads/page/"
+ jsonpage + "/?json=get_recent_posts");
posts = json.getJSONArray(KEY_POSTS);
if (posts.length() > 0) {
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
// to remove all <P> </p> and <br /> and replace with ""
content = content.replace("<br />", "");
content = content.replace("<p>", "");
content = content.replace("</p>", "");
// authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);
String url = null;
String slug = null;
try {
JSONArray atta = c.getJSONArray("attachments");
for (int j = 0; j < atta.length(); j++) {
JSONObject d = atta.getJSONObject(j);
slug = d.getString(KEY_SLUG);
JSONObject images = d.getJSONObject(KEY_IMAGES);
JSONObject thumbnail = images
.getJSONObject(KEY_THUMB_URL);
url = thumbnail.getString(KEY_URL);
}
} catch (Exception e) {
e.printStackTrace();
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL,
url);
// adding HashList to ArrayList
songsList.add(map);
}
} else
loadtext.setText("Data ivlo thaan irukku k va summa look-u vidatha");
} catch (JSONException e) {
e.printStackTrace();
}
// Runnable to load the items
Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
// Set flag so we cant load new items 2 at the same time
loadingMore = true;
// Reset the array that holds the new items
songsList1 = new ArrayList<String>();
// Simulate a delay, delete this on a production environment!
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
runOnUiThread(returnRes);
}
};
// Since we cant update our UI from a thread this Runnable takes care of
// that!
private Runnable returnRes = new Runnable() {
@Override
public void run() {
// Loop thru the new items and add them to the adapter
if (songsList1 != null && songsList1.size() > 0) {
for (int i = 0; i < songsList1.size(); i++)
adapter.add(songsList1.get(i));
}
// Update the Application title
setTitle("Neverending List with "
+ String.valueOf(adapter.getCount()) + " items");
// Tell to the adapter that changes have been made, this will cause
// the list to refresh
adapter.notifyDataSetChanged();
// Done loading more.
loadingMore = false;
}
};
公共类主页扩展ListActivity{
ArrayList songsList;
列表视图列表;
懒散适配器;
JSONArray岗位;
线性布局线1、线2;
图像视图菜单;
boolean loadingMore=false;
ArrayList songsList1;
充气机;
静态int jsonpage=0;
JSONParser-jParser;
JSONObject json;
文本视图加载文本;
//所有静态变量
静态最终字符串URL=”http://www.exm.com/?json=get_recent_posts";
静态最终字符串键\u POSTS=“POSTS”;
静态最终字符串键\u ID=“ID”;
静态最终字符串键\u TITLE=“TITLE”;
静态最终字符串键\u DATE=“DATE”;
静态最终字符串键\u CONTENT=“CONTENT”;
静态最终字符串键\u AUTHOR=“AUTHOR”;
静态最终字符串键\u NAME=“NAME”;
静态最终字符串键\u ATTACHMENTS=“ATTACHMENTS”;
静态最终字符串键\u SLUG=“SLUG”;
静态最终字符串键\u THUMB\u URL=“缩略图”;
静态最终字符串键\u IMAGES=“IMAGES”;
静态最终字符串键\u URL=“URL”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(android.R.id.list);
//获取用于对customomView进行充气的LayoutFlater
//这将在自定义适配器中使用
充气器=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u SERVICE);
songsList=newarraylist();
_loaddata();
LayoutInflater li=LayoutInflater.from(getBaseContext());
视图页脚视图=li.inflate(com.exm.com.R.layout.listfooter,null);
loadtext=(TextView)footerView.findViewById(R.id.empty);
loadtext.setEnabled(false);
loadtext.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),“poda”,
Toast.LENGTH_LONG).show();
}
});
this.getListView().addFooterView(footerView);
//通过传递json数据ArrayList获取适配器
adapter=新的LazyAdapter(此,songsList);
list.setAdapter(适配器);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(
getApplicationContext(),
“单击列表项目编号”
+歌曲列表。获取(位置)。获取(“标题”),
Toast.LENGTH_LONG).show();
}
});
this.getListView().setOnScrollListener(新的OnScrollListener()){
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
//int first=view.getFirstVisiblePosition();
//int count=view.getChildCount();
//
//如果(滚动状态==滚动状态空闲
//| |(first+count>adapter.getCount()){
//list.invalidateViews();
// }
如果(滚动状态==滚动状态空闲){
_loaddata();
adapter.notifyDataSetChanged();
}
}
@凌驾
public void onScroll(AbsListView视图,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
}
});
线程线程=新线程(null,loadMoreListItems);
thread.start();
}
@抑制警告(“未使用”)
私有void_loaddata(){
试一试{
//从URL获取JSON字符串
jParser=newjsonparser();
jsonpage=jsonpage+1;
json=jParser
.getJSONFromUrl(“http://india.exm.net/ads/page/"
+jsonpage+“/?json=get_recents_posts”);
posts=json.getJSONArray(KEY_posts);
如果(posts.length()>0){
对于(int i=0;i和
并替换为“”
content=content.replace(“
,”);
content=content.replace(“”,”);
内容=内容。替换(“”,“”);
//authornumber是agin JSON对象
JSONObject author=c.getJSONObject(KEY_author);
字符串名称=author.getString(键名称);
字符串url=null;
字符串slug=null;
试一试{
JSONArray atta=c.getJSONArray(“附件”);
对于(int j=0;jvalue
地图放置(钥匙ID,ID);
地图放置(图例标题、标题);
地图放置(钥匙日期,日期);
地图放置(键名称、名称);
地图放置(关键内容,内容);
map.put(键_SLUG,SLUG);
map.put(KEY_URL,
网址);
//将哈希列表添加到ArrayList
歌曲列表。添加(地图);
}
}否则
loadtext.setText(“数据ivlo thaan irukku k va summa look-u vidatha”);
}捕获(JSONException e){
e、 printStackTrace();
}
//可运行以加载项目
Runnable loadMoreListItems=新的Runnable(){
@凌驾
公开募捐{
//设置标志,以便我们不能同时加载新项目2
loadingMore=true;
//重置包含新项的数组
songsList1=新的ArrayList();
//模拟延迟,在生产环境中删除此延迟!
试一试{
睡眠(1000);
}捕捉(中断异常e){
}
runOnUiThread(returnRes);
}
};
//因为我们不能从线程更新UI,所以Runnable会处理这个问题
//那个!
private Runnable returnRes=new Runnable(){
@凌驾
公开募捐{
//循环浏览新项目并将其添加到适配器
if(songsList1!=null&&songsList1.size()>0){
对于(int i=0;iprivate Runnable _loaddata() {
...
return loadMoreListItems;
}
Runnable loadMoreListItems = _loaddata();
...
Thread thread = new Thread(null, loadMoreListItems);