제목에서 말한 것처럼. JSON 만 사용하고 싶습니다. 내 추측은 내 주요 활동이다. 이것은 모든 내용을 움켜 잡고 내가 바라는 정중 한 변수에 놓습니다. 시작 부분에 배치 :안드로이드 마스터/세부 흐름에서 JSON 구문 분석
public class WordDetailActivity extends FragmentActivity {
// Reading text file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null; {
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"wordlist.txt")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
String myjsonstring = sb.toString();
// Try to parse JSON
try {
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("employee");
// JSONArray has four JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
int id = jsonObj.getInt("id");
String word = jsonObj.getString("word");
String dictionary = jsonObj.getString("dictionary");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
이제
다시는 이러한 변수를 정의 WordContent.java라는 또 다른 파일 (비 편집 버전) 한 : 나는 때문에
public static Map<String, WordItem> ITEM_MAP = new HashMap<String, WordItem>();
static {
// Add 3 sample items.
addItem(new WordItem("1", "This Word", "Blah blah blah"));
}
private static void addItem(WordItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class WordItem {
public String id;
public String word;
public String dictionary;
public WordItem(String id, String word, String dictionary) {
this.id = id;
this.word = word;
this.dictionary = dictionary;
}
@Override
public String toString() {
return word;
}
}
}
내가 아직 편집하지 않은 여기에서 어디로 가야할지 모르겠다. 또는 내 JSON의 내용을 WordItem에 저장하여 프로그램을 실행할 때 표시하는 방법입니다. 이와 비슷한 모든 코드를 보는 또 다른 방법은 Eclipse ADT 번들에 Master/Detail Flow 프로젝트를 만드는 것입니다. 나는이 모든 권리를 말하고 싶다. 더 자세한 내용이 있으면 알려주세요. 안드로이드 Dev에 매우 익숙하지만 올바른 방향으로 어떤 포인터가 크게 감사드립니다.
그냥 WordItem 클래스에 ID를 String 유형으로 추가하고 싶습니다. 그런 다음 ID를 int에서 String으로 캐스팅해야합니다. .toString() 메서드를 사용하면 효과가 있습니다. – michaelcarrano
먼저이 답변에 감사드립니다. 이것은 확실히 올바른 방향으로 느껴지는 것입니다. 컨텍스트를 만드는 방법을 이해하는 데 문제가있어 활동 외부에서 getAssets() 메서드를 사용할 수 있습니다. 내가받는 또 다른 오류는 "ParseJsonTask 형식의 onPostExecute (Void) 메서드는 반드시 슈퍼 유형 메서드를 재정의하거나 구현해야합니다."나는 그 중 무엇을 의미하는지 완전히 확신하지 못합니다. – joshuar500
나는 내 대답을 약간 업데이트하고 약간의 수정을했다. 이제 Context를 인수로 취하는 ParseJsonTask의 생성자를 볼 수 있습니다. getBaseContext()를 전달하면 작동하고 getActivity()를 시도하면 안된다고 생각합니다. – michaelcarrano