0

我在一个虚拟项目中测试了无限滚动功能的功能,它工作正常,但如果我将相同的代码粘贴到我的原始项目中,我在变量“loadMoreListItems”和“returnRes 不能被解析为变量”解析为变量”,即使我检查了 android.R 导入和布局名称,一切都是正确的,不知道错误在哪里。

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;

}

};
4

1 回答 1

1

这与Android资源无关。

该变量loadMoreListItems在 中使用onCreate(),但在 中定义_loaddata()。您不能在一种方法中定义变量并在另一种方法中使用它。

你可以loadMoreListItems_loaddata()

private Runnable _loaddata() {
    ...
    return loadMoreListItems;
}

onCreate()像这样使用它

Runnable loadMoreListItems = _loaddata();
...
Thread thread = new Thread(null, loadMoreListItems);

并且您在使用returnResinRunnable loadMoreListItems之前使用它稍后在相同的方法中定义它。您必须先定义和初始化returnRes,然后才能在loadMoreListItems. 所以移动returnRes之前loadMoreListItems应该解决这个问题。

于 2013-03-13T11:08:48.467 回答