2014-03-29 43 views
1

我正在創建一個應用程序,它使用本地json文件來顯示一些信息。因此我解析了數據和顯示數據的文本視圖。但是我實際需要的是解析的數據必須顯示在Listview中。這個怎麼做 ?如何從本地JSON文件解析JSON到Android中的ListView?

這是我的代碼。

 // Reading text file from assets folder 
     StringBuffer sb = new StringBuffer(); 
     BufferedReader br = null; 
     try { 
      br = new BufferedReader(new InputStreamReader(getAssets().open(
        "jsonshoutdata.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(); 
      } 
     } 

     myjsonstring = sb.toString(); 
     // Try to parse JSON 
     try { 
      urlist = new ArrayList<HashMap<String, String>>(); 
        // Creating JSONObject from String 
      JSONObject jsonObjMain = new JSONObject(myjsonstring); 

      // Creating JSONArray from JSONObject 
      JSONArray jsonArray = jsonObjMain.getJSONArray("message"); 

      // 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 
       String message = jsonObj.getString("msg"); 

       HashMap<String, String> map = new HashMap<String, String>(); 

       map.put("msg", msg); 

       urlist.add(map); 

      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 

這是我customList適配器

CustomList adapter = new CustomList(MainActivity.this,urlist); 
     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 

      @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
         Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show(); 

        } 
       }); 

這是我的適配器類

public class CustomList extends ArrayAdapter<String> 
{ 
    public final ArrayList<HashMap<String, String>> urlist; 
    private Activity context; 

    public CustomList(Activity context,ArrayList<HashMap<String, String>> urlist) 
    { 
     super(context, R.layout.list_single); 
     this.context = context; 
     this.urlist=urlist; 

    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.list_single, null, true); 

    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
    txtTitle.setText((CharSequence) urlist); 
    return rowView; 

} 
} 

回答

4

創建customList(dont use hashmap)

改變您的自定義列表的代碼(這是你的代碼)

public class CustomList extends ArrayAdapter<String> 
{ 
public final ArrayList<HashMap<String, String>> urlist; 
private Activity context; 

public CustomList(Activity context,ArrayList<HashMap<String, String>> urlist) 
{ 
    super(context, R.layout.list_single); 
    this.context = context; 
    this.urlist=urlist; 

} 
@Override 
public View getView(int position, View view, ViewGroup parent) { 
LayoutInflater inflater = context.getLayoutInflater(); 
View rowView= inflater.inflate(R.layout.list_single, null, true); 

TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 
txtTitle.setText((CharSequence) urlist); 
return rowView; 

} 
} 

public class customList extends ArrayAdapter<String>{ 

Context context; 
int layoutResourceId;  
ArrayList<String> data = null; 

public customList(Context context, int layoutResourceId, ArrayList<String> data) { 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.data = data; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    CustomHolder holder = null; 

    if(row == null) 
    { 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder = new CustomHolder(); 
     holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 

     row.setTag(holder); 
    } 
    else 
    { 
     holder = (CustomHolder)row.getTag(); 
    } 

    String s = data.get(postition); 
    holder.txtTitle.setText(s); 

    return row; 
} 

static class CustomHolder 
{ 
    TextView txtTitle; 
} 
} 

和ch安格你的JSON解析代碼和解析您的JSON像

// JSONArray has four JSONObject 

    ArrayList<String> messages = new ArrayList<String>(); 
     for (int i = 0; i < jsonArray.length(); i++) { 

      // Creating JSONObject from JSONArray 
      JSONObject jsonObj = jsonArray.getJSONObject(i); 

      // Getting data from individual JSONObject 
      String message = jsonObj.getString("msg"); 
      messages.add(message); 

     } 

終於取代您的代碼

CustomList adapter = new CustomList(MainActivity.this,urlist); 
    list=(ListView)findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 

     @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show(); 

       } 
      }); 

這一個

customList adapter = new customList(MainActivity.this,R.layout.list_single,messages); //messagses is your arraylist in which you added string by parsing your json (see before this code mean upward) 
    list=(ListView)findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 

     @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show(); 

       } 
      }); 

解決方案2

由於您只顯示單個文本,因此您不需要創建自己的自定義適配器,而只需執行此操作。

ArrayList<String> messages = new ArrayList<String>(); 
     for (int i = 0; i < jsonArray.length(); i++) { 

      // Creating JSONObject from JSONArray 
      JSONObject jsonObj = jsonArray.getJSONObject(i); 

      // Getting data from individual JSONObject 
      String message = jsonObj.getString("msg"); 
      messages.add(message); 

     } 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, 
       android.R.layout.simple_list_item_1, messages); 
    list=(ListView)findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 

     @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show(); 

       } 
      }); 
+1

哪裏..​​.哪部分讓你困惑......? –

+0

問我..如果你什麼都不明白.. –

+0

請問你能按照正確的順序編輯它嗎? – Nevaeh

0

使用此。

try { 
     urlist = new ArrayList<HashMap<String, String>>(); 
     JSONObject jsonObjMain = new JSONObject(myjsonstring); 

     // Creating JSONArray from JSONObject 
     JSONArray jsonArray = jsonObjMain.getJSONArray("message"); 

     // 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 
      String message = jsonObj.getString("msg"); 

      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put("msg", message); 

      urlist.add(map); 

     } 

    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

的onCreate之前這裏全局聲明()

ArrayList<HashMap<String, String>> urlist; 

現在你必須urList的ArrayList傳遞給你的CustomAdapterListView作爲參數,並且該適配器設置爲您ListView後。