2013-03-13 1 views
0

가짜 프로젝트에서 끝없는 스크롤 기능의 기능을 테스트했는데 정상적으로 작동하지만 동일한 원본 코드를 붙여 넣으면 "loadMoreListItems를 확인할 수 없습니다. 변수에 "변수"loadMoreListItems "및"returnRes 변수로 해결할 수 없습니다 변수 ", 심지어 나는 android.R 가져 오기 및 레이아웃 이름을 모두 올바른지 알 수없는 실수입니다."returnRes 변수로 해결할 수 없습니다"android

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; 

} 

}; 
+0

Welcome to Stackoverflow. 도움이되는 답변을 찾으면 업 보턴 트 (upvote) 할 수 있습니다. 답변으로 질문이 해결되면 동의하십시오. [여기에 질문하는 방법은 무엇입니까?] (http://stackoverflow.com/faq#howtoask)를 참조하십시오. –

답변

1

이것은 Android 리소스와 관련이 없습니다.

onCreate()에 변수 loadMoreListItems이 사용되지만, _loaddata()으로 정의됩니다. 하나의 메소드에서 변수를 정의하고 다른 메소드에서 사용할 수 없습니다.

당신은 _loaddata()

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

에서 loadMoreListItems을 반환하고

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

처럼 onCreate()에서 사용할 그리고이 같은 방법으로 나중에 정의하기 전에 Runnable loadMoreListItemsreturnRes를 사용할 수 있습니다. 먼저 returnRes을 정의하고 초기화해야하며 loadMoreListItems에서 사용할 수 있습니다. 따라서 returnResloadMoreListItems 앞에 붙여야 해결할 수 있습니다.

관련 문제