2016-02-18 42 views
3

我按照這個video嘗試從MongoDB的 獲取數據從MongoDB中獲得JsonArray的數據,並使用調試模式檢查類JsonTest.java:ArrayList<JsonSongs> songlist;在它21點的數據,正確的,並列表視圖有21行顯示,但不知何故數據沒有顯示在文本視圖上,JsonAdapter不能打印

我覺得類JsonTest.java:'ArrayList歌曲列表; did pass the data to class JsonSongAdapter.java ˋArrayList<JsonSongs> songlist;對不對? 請大家幫忙,困在這裏小時

JsonTest.java

public class JsonTest extends Activity { 

ListView list; 
ArrayList<JsonSongs> songlist; 
    private TextView test; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.jsonmain); 
    list = (ListView)findViewById(R.id.JsonlistView); 
    songlist = new ArrayList<JsonSongs>(); 
    new JsonTalk().execute("http://192.168.1.102:3000/songs"); 

     } 





class JsonTalk extends AsyncTask< String , String ,String>{ 

    @Override 
    protected String doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 
      String finalJson = buffer.toString(); 
      // JSONObject parentJsonObject = new JSONObject(finalJson); 
      JSONArray jsonArray = new JSONArray(finalJson); 
      JsonSongs jsonSongs = new JsonSongs(); 
      for (int i = 0 ; i < jsonArray.length();i++){ 
       JSONObject finaObject = jsonArray.getJSONObject(i); 
       jsonSongs.set_id(finaObject.getString("_id")); 
       jsonSongs.setDecade(finaObject.getString("decade")); 
       jsonSongs.setArtist(finaObject.getString("artist")); 
       jsonSongs.setSong(finaObject.getString("song")); 
       jsonSongs.setWeeksAtOne(finaObject.getInt("weeksAtOne")); 
       songlist.add(jsonSongs); 
      } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }catch (JSONException e){ 
      e.printStackTrace(); 
     } 

     finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     if(result == null){ 
      JsonSonAdapter adapter = new JsonSonAdapter(getApplicationContext(),R.layout.jsonlayout,songlist); 
      list.setAdapter(adapter); 
     }else { 
      Toast.makeText(JsonTest.this,"NOP",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
} 

JsonSonAdapter

public class JsonSonAdapter extends ArrayAdapter<JsonSongs> { 
ArrayList<JsonSongs> songList; 
int Resource; 
Context context; 
LayoutInflater vi; 

public JsonSonAdapter(Context context, int resource, ArrayList<JsonSongs> objects) { 
    super(context, resource, objects); 
    songList = objects; 
    Resource = resource; 
    this.context = context; 
    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    if(convertView == null){ 
     convertView = vi.inflate(Resource,null); 
     holder = new ViewHolder(); 
     holder.textJson_ID=(TextView)convertView.findViewById(R.id.textJson_ID); 
     holder.textJSONname=(TextView)convertView.findViewById(R.id.textJSONname); 
     holder.textJsonSong=(TextView)convertView.findViewById(R.id.textJsonSong); 
     holder.textJsonYear=(TextView)convertView.findViewById(R.id.textJsonYear); 
     holder.textJsonWeeks=(TextView)convertView.findViewById(R.id.textJsonWeeks); 
     convertView.setTag(holder); 
    }else { 
     holder = (ViewHolder)convertView.getTag(); 
    } 

    return convertView; 
} 

static class ViewHolder{ 

public TextView textJsonYear; 
public TextView textJSONname; 
public TextView textJsonSong; 
public TextView textJsonWeeks; 
public TextView textJson_ID; 
} 

} 

JsonSongs

public class JsonSongs { 

private String _id; 
private String decade; 
private String artist; 
private String song; 
private int weeksAtOne; 

public String get_id() { 
    return _id; 
} 

public void set_id(String _id) { 
    this._id = _id; 
} 

public String getArtist() { 
    return artist; 
} 

public void setArtist(String artist) { 
    this.artist = artist; 
} 

public String getDecade() { 
    return decade; 
} 

public void setDecade(String decade) { 
    this.decade = decade; 
} 

public String getSong() { 
    return song; 
} 

public void setSong(String song) { 
    this.song = song; 
} 

public int getWeeksAtOne() { 
    return weeksAtOne; 
} 

public void setWeeksAtOne(int weeksAtOne) { 
    this.weeksAtOne = weeksAtOne; 
} 




} 

回答

1

但不知何故,該數據在我以前不顯示textview,

你沒有設置它,所以沒有奇蹟。你需要在你的EditText s(getView())上調用setText(),一旦你完成了convertView的數據填充。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    if(convertView == null){ 
     convertView = vi.inflate(Resource,null); 
     holder = new ViewHolder(); 
     holder.textJson_ID=(TextView)convertView.findViewById(R.id.textJson_ID); 
     ... 
     convertView.setTag(holder); 
    }else { 
     holder = (ViewHolder)convertView.getTag(); 
    } 


    // populate it with data 
    JsonSongs song = songs.get(position); 
    holder.textJson_ID.setText(song.getSomething()); 
    ... 


    return convertView; 
} 

PS:JsonSongs而應被命名爲JsonSong。而且你在命名模式上弄得一團糟。

+0

非常感謝你,這是尷尬>< –

+0

我應該問一個新問題,但仍然有60分鐘...所以如果你能幫助我,這將是非常棒的, 我給了21個相同的數據,它只是顯示第一個元素割 我檢查人們說:「不要把ListView放在ScrollView裏面」 比我檢查這個[link](http://nex-otaku-en.blogspot.tw/2010/12/android- put-listview-in-scrollview.html) 但我不確定這是答案,我無法正確解答它... –

+0

您需要提出新問題。我回滾了你最近的編輯。 –