Reputation: 257
I follow this video try to get data from mongodb
get the JsonArray from mongodb , and use debug mode check the class JsonTest.java : ArrayList<JsonSongs> songlist;
have 21 data in it , and all correct , and the listview has 21 row show up , but somehow the data did't show on the textview ,
I think the Class JsonTest.java : ˋArrayList songlist;did pass the data to class JsonSongAdapter.java ˋArrayList<JsonSongs> songlist;
right?
please help , stuck here for hours
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;
}
}
Upvotes: 3
Views: 72
Reputation: 75629
but somehow the data did't show on the textview ,
You set it nowhere so no wonders. You need to call setText()
on your EditText
s (in getView()
) once you done with convertView to populate it with data.
@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
should rather be named JsonSong
. And you got quite a mess in naming pattern.
Upvotes: 1