altin bana altin bana - 3 months ago 22
JSON Question

How to parse Videos ID Name Category from Json in Android

I am trying to parse a Json file (video.js) from a http server.
this code is working fine, I get the name and the url ,so the video is playing perfect, but the only problem is I cant get the Video ID or channel number (chid) to show just before the video name.
I also would love to know how to put it in categories like Rock, RNB, POP etc. I tried this with the below json but not working. It only work if I remove categories. sorry I am a total newbie

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is "200 OK"
String jsonResponse = new String(response);

try {


JSONArray jsonArray = new JSONArray(jsonResponse);
JSONObject jsonObject;
String videoTitle, videoUrl, videoNum ;

int length = jsonArray.length();
for (int i = 0; i < length; ++i) {
jsonObject = (JSONObject) jsonArray.get(i);



videoTitle = jsonObject.getString("chname");
videoUrl = jsonObject.getString("chlink");
videoNum = jsonObject.getString("chid");


VideoItem videoItem = new VideoItem(videoTitle, videoUrl, videoNum);
videoList.add(videoItem);

}


My Json file

{categories: [
{
"chid": 1,
"chname": "Video 1",
"chlogo": "",
"chlink": "http://samplevideo1.mp4",
"chenable": "",
"chnote": ""
},
{
"chid": 2,
"chname": "Video 2",
"chlogo": "",
"chlink": "http://samplevideo2.mp4",
"chenable": "",
"chnote": ""
},
{
"chid": 3,
"chname": "RO 3",
"chlogo": "",
"chlink": "http://samplevideo3.mp4",
"chenable": "",
"chnote":

}
]
}


My adapter

public class VideoSelectionListAdapter extends BaseAdapter {

private List<VideoItem> items;
private LayoutInflater inflater;

public VideoSelectionListAdapter(Context context,List<VideoItem> items) {
//items = VideoItems.getItems();
this.items=items;

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int position) {
return items.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

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

if (convertView == null) {
convertView = inflater.inflate(R.layout.simple_text_item, null);

holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.simple_text_text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text.setText(items.get(position).getTitle());
return convertView;
}

private static class ViewHolder {
TextView text;
}


}

Update
here is my VideoItem

public class VideoItem {
String title;
int mediaNum;
String mediaUrl;

public VideoItem(String title, String mediaUrl, int mediaNum) {
this.title = title;
this.mediaUrl = mediaUrl;
this.mediaNum = mediaNum;
}

public String getTitle() {
return title;
}
public int getMediaNum() {
return mediaNum;
}
public String getMediaUrl() {
return mediaUrl;
}

Answer

If you would like to display an ID as well as the title, then update this line to do that.

holder.text.setText(items.get(position).getTitle())

I might also suggest making a variable for the value of items.get(position)

VideoItem v = items.get(position);
holder.text.setText(v.getMediaNum() + " - " + v.getTitle());