Android Custom Arrayadapter Does Not Take An Array
I correctly fill an array? NewsData_data cannot be resolved to a variable. NewsData NewsData_data[] = new NewsData[] { new NewsData(header[i
Solution 1:
You need to declare your array outside of the try block so that it is visible to the ArrayAdapter
constructor.
Solution 2:
As corsair922 mentioned, you'll want to declare your NewsData_data array outside your try catch block, otherwise you won't have access to it from outside the block. Additionally, you want to initialize your array once, and populate the array elements as you go as opposed to reinitializing your array each time:
Edit, I would also recommend you spend some time getting familiar with the Java coding conventions. They will make your code easier to maintain/tidier and will allow other people to better understand your code (http://www.oracle.com/technetwork/java/codeconv-138413.html).
publicvoidListDrwaer() {
String[] header;
String[] short_text;
String[] team;
String[] datatime;
String[] photo_url;
NewsDataNewsData_data[];
try {
JSONObject jsonResponse = newJSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("news");
NewsData_data = newNewsData[jsonMainNode.length()];
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
header[i] = jsonChildNode.optString("header");
short_text[i] = jsonChildNode.optString("short_text");
team[i] = jsonChildNode.optString("team");
datatime[i] = jsonChildNode.optString("datatime");
photo_url[i] = jsonChildNode.optString("photo_url");
NewsData_data[i] = newNewsData(header[i], short_text[i], team[i], datatime[i], photo_url[i]);
}
} catch (JSONException e) {
Toast.makeText(getActivity(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
}
NewsDataAdapter adapter = newNewsDataAdapter(this, R.layout.news_details, NewsData_data);
View header1 = getActivity().getLayoutInflater().inflate(R.layout.news_details, null);
listView.addHeaderView(header1);
listView.setAdapter(adapter);
}
Post a Comment for "Android Custom Arrayadapter Does Not Take An Array"