更换JSON填充的TextView使用JSON已填充的ListView [英] Replacing JSON Populated TextView with JSON Populated ListView
问题描述
我有一个JSON请求返回从YouTube包含特定视频的评论的回应。我现在有3 textviews:一个用于名称/上载,一个用于内容,和一个用于出版的日期 - 然后填充与来自我的JSON响应的数据。
我的问题是 - 只有第一个评论,发布日期和上传者出现。
我相信我需要用一个列表视图代替我textviews和解析3场给它 - 我根本不知道怎么办。
JAVA
公共类播放器扩展YouTubeBaseActivity工具 YouTubePlayer.OnInitializedListener {
公共静态最后弦乐API_KEY =XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.player);
字符串标题= getIntent()getStringExtra(标题)。
字符串上传= getIntent()getStringExtra(上传)。
串观看次数= getIntent()getStringExtra(观看次数)。
TextView的titleTv =(TextView中)findViewById(R.id.titleTv);
TextView的uploaderTv =(TextView中)findViewById(R.id.uploaderTv);
TextView的viewCountTv =(TextView中)findViewById(R.id.viewCountTv);
titleTv.setText(职称);
uploaderTv.setText(本经+上传+|);
viewCountTv.setText(观看次数+意见);
YouTubePlayerView youTubePlayerView =(YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY,这一点);
处理程序处理程序=新的处理程序(新Handler.Callback(){
@覆盖
公共布尔的handleMessage(信息MSG){
返回false;
}
});
GetYouTubeUserCommentsTask任务=新GetYouTubeUserCommentsTask(处理器,观看次数);
task.execute();
}
@覆盖
公共无效onInitializationFailure(供应商提供,
YouTubeInitializationResult结果){
Toast.makeText(getApplicationContext(),onInitializationFailure(),
Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onInitializationSuccess(供应商提供,
YouTubePlayer球员,布尔wasRestored){
如果(!wasRestored){
字符串VIDEO_ID = getIntent()getStringExtra(ID)。
player.loadVideo(VIDEO_ID);
}
}
公共final类GetYouTubeUserCommentsTask扩展
AsyncTask的<虚空,虚空,虚空> {
公共静态最终字符串库=CommentsLibrary;
私人最终处理程序的replyTo;
私人最终字符串用户名;
字符串VIDEO_ID = getIntent()getStringExtra(ID)。
公共GetYouTubeUserCommentsTask(处理器的replyTo,字符串username){
this.replyTo =的replyTo;
this.username =用户名;
}
@覆盖
保护无效doInBackground(虚空......为arg0){
尝试 {
HttpClient的客户端=新DefaultHttpClient();
HttpUriRequest请求=新HTTPGET(
http://gdata.youtube.com/feeds/api/videos/
+ VIDEO_ID
+/评论ν= 2及?中高音= JSON&安培;启动索引= 1&安培;最大-结果= 50安培; prettyprint =真);
HTT presponse响应= client.execute(要求);
字符串jsonString = StreamUtils.convertToString(响应
。.getEntity()的getContent());
JSONObject的JSON =新的JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject(饲料)。getJSONArray(
条目);
名单<意见>评论=新的ArrayList<意见>();
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject条目= jsonArray.getJSONObject(ⅰ);
JSONArray authorArray = entry.getJSONArray(作家);
JSONObject的publishedObject = entry.getJSONObject(发布);
字符串公布= publishedObject.getString($ T);
JSONObject的contentObject = entry.getJSONObject(内容);
字符串的内容= contentObject.getString($ T);
的JSONObject authorObject = authorArray.getJSONObject(0);
JSONObject的nameObject = authorObject.getJSONObject(姓名);
字符串名称= nameObject.getString($ T);
comments.add(新评论(名称,内容,出版));
CommentsLibrary LIB =新CommentsLibrary(出版,内容,名称);
捆绑数据=新包();
data.putSerializable(图书馆,LIB);
消息味精= Message.obtain();
msg.setData(数据);
replyTo.sendMessage(MSG);
}
}赶上(ClientProtocolException E){
Log.e(Feck,E);
}赶上(IOException异常E){
Log.e(Feck,E);
}赶上(JSONException E){
Log.e(Feck,E);
}
返回null;
}
@覆盖
保护无效onPostExecute(无效的结果){
ListView的意见=(ListView控件)findViewById(R.id.comments);
comments.setFilterText(com.idg.omv.domain.CommentsLibrary.getName());
}
}
}
CommentsLibrary
公共类CommentsLibrary实现Serializable {
//注释的所有者的用户名
私有静态字符串名称;
//注释
私有静态字符串的内容;
//日期注释出版
私有静态字符串出版;
公共CommentsLibrary(字符串名称,字符串内容,字符串出版){
this.name =名称;
this.content =内容;
this.published =出版;
}
/ **
返回:用户名
* /
公共静态字符串的getName(){
返回名称;
}
/ **
返回:视频
* /
公共静态字符串的getContent(){
返回的内容;
}
/ **
返回:视频
* /
公共静态字符串出版文章(){
返回出版;
}
}
XML
<的TextView
机器人:ID =@ + ID /名称
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=左
机器人:文本=/>
<的TextView
机器人:ID =@ + ID /内容
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ ID /名称
机器人:layout_weight =1
机器人:重力=左
机器人:文本=/>
<的TextView
机器人:ID =@ + ID /出版的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ ID /内容
机器人:layout_weight =1
机器人:重力=左
机器人:文本=/>
从previous问题@继续我的答案<一href="http://stackoverflow.com/questions/20548108/json-string-is-incorrectly-mapped-to-textviews">JSON字符串被错误地映射到textviews
首先你需要一个列表视图在布局XML
&LT;的ListView
机器人:ID =@ + ID /列表
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/&GT;
声明列表作为一个实例变量
的ArrayList&LT; CommentsLibrary&GT;名单=新的ArrayList&LT; CommentsLibrary&GT;();
然后
的for(int i = 0; I&LT; jsonArray.length();我++){
的JSONObject的JSONObject = jsonArray.getJSONObject(我);
字符串名称= jsonObject.optString(名,设置defaultValue);
字符串的内容= jsonObject.optString(内容,设置defaultValue);
字符串公布= jsonObject.optString(发布,设置defaultValue);
list.add(新CommentsLibrary(名称,内容,出版));
}
然后初始化列表视图
的ListView LV =(ListView控件)findViewById(R.id.list);
CustomAdapter CUS =新CustomAdapter(ActivityName.this,清单);
lv.setAdapter(CUS);
定义自定义布局3 textviews。 (list_item.xml)
&LT; RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT&GT;
&LT;的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_marginLeft =18dp
机器人:layout_marginTop =31dp
机器人:文本=TextView的/&GT;
&LT;的TextView
机器人:ID =@ + ID / textView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / textView1
机器人:layout_alignBottom =@ + ID / textView1
机器人:layout_centerHorizontal =真
机器人:文本=TextView的/&GT;
&LT;的TextView
机器人:ID =@ + ID / textView3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / textView2
机器人:layout_alignBottom =@ + ID / textView2
机器人:layout_alignParentRight =真
机器人:layout_marginRight =24dp
机器人:文本=TextView的/&GT;
&LT; / RelativeLayout的&GT;
然后通过扩展底座适配器定义自定义适配器。覆盖 getView
膨胀的自定义布局和设置文本视图存在。
公共类CustomAdapter扩展了BaseAdapter
{
LayoutInfalter mInflater;
ArrayList的&LT; CommentsLibrary&GT;清单;
公共CustomAdapter(上下文的背景下,ArrayList的&LT; CommentsLibrary&GT;名单)
{
mInflater = LayoutInfalter.from(上下文);
this.list =清单;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回则为list.size();
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
ViewHolder持有人;
如果(convertView == NULL)
{
convertView = minflater.inflate(R.layout.list_item,父母,假);
持有人=新ViewHolder();
holder.tv1 =(TextView中)convertView.findViewById(R.id.textView1);
holder.tv2 =(TextView中)convertView.findViewById(R.id.textView2);
holder.tv3 =(TextView中)convertView.findViewById(R.id.textView3);
convertView.setTag(保持器);
}
其他
{
支架=(ViewHolder)convertView.getTag();
}
holder.tv1.setText(list.get(位置).getName());
holder.tv2.setText(list.get(位置).getContent());
holder.tv3.setText(list.get(位置).getPublished());
返回convertView;
}
静态类ViewHolder
{
TextView的TV1,TV2,TV3
}
}
I have a JSON request which returns a response from youtube containing the comments for a particular video. I currently have 3 textviews: one for the name/uploader, one for the content, and one for the date published - which are then populated with the data from my JSON response.
My problem is - only the first comment, date published and uploader appear.
I belive I'll need to replace my textviews with a list view and parse the 3 fields to it - I simply do not know how.
JAVA
public class Player extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
String title = getIntent().getStringExtra("title");
String uploader = getIntent().getStringExtra("uploader");
String viewCount = getIntent().getStringExtra("viewCount");
TextView titleTv = (TextView) findViewById(R.id.titleTv);
TextView uploaderTv = (TextView) findViewById(R.id.uploaderTv);
TextView viewCountTv = (TextView) findViewById(R.id.viewCountTv);
titleTv.setText(title);
uploaderTv.setText("by" + uploader + " |");
viewCountTv.setText(viewCount + " views");
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(handler , viewCount);
task.execute();
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(), "onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
String video_id = getIntent().getStringExtra("id");
player.loadVideo(video_id);
}
}
public final class GetYouTubeUserCommentsTask extends
AsyncTask<Void, Void, Void> {
public static final String LIBRARY = "CommentsLibrary";
private final Handler replyTo;
private final String username;
String video_id = getIntent().getStringExtra("id");
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(
"http://gdata.youtube.com/feeds/api/videos/"
+ video_id
+ "/comments?v=2&alt=json&start-index=1&max-results=50&prettyprint=true");
HttpResponse response = client.execute(request);
String jsonString = StreamUtils.convertToString(response
.getEntity().getContent());
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("feed").getJSONArray(
"entry");
List<Comments> comments = new ArrayList<Comments>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject entry = jsonArray.getJSONObject(i);
JSONArray authorArray = entry.getJSONArray("author");
JSONObject publishedObject = entry.getJSONObject("published");
String published = publishedObject.getString("$t");
JSONObject contentObject = entry.getJSONObject("content");
String content = contentObject.getString("$t");
JSONObject authorObject = authorArray.getJSONObject(0);
JSONObject nameObject = authorObject.getJSONObject("name");
String name = nameObject.getString("$t");
comments.add(new Comments(name, content, published));
CommentsLibrary lib = new CommentsLibrary(published, content, name);
Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);
Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);
}
} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
ListView comments = (ListView) findViewById(R.id.comments);
comments.setFilterText(com.idg.omv.domain.CommentsLibrary.getName());
}
}
}
CommentsLibrary
public class CommentsLibrary implements Serializable{
// The username of the owner of the comment
private static String name;
// The comment
private static String content;
// The date the comment was published
private static String published;
public CommentsLibrary(String name, String content, String published) {
this.name = name;
this.content = content;
this.published = published;
}
/**
* @return the user name
*/
public static String getName() {
return name;
}
/**
* @return the videos
*/
public static String getContent() {
return content;
}
/**
* @return the videos
*/
public static String getPublished() {
return published;
}
}
XML
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_weight="1"
android:gravity="left"
android:text="" />
<TextView
android:id="@+id/published"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/content"
android:layout_weight="1"
android:gravity="left"
android:text="" />
Continuing my answer from your previous question @ JSON String is incorrectly mapped to textviews
First you need a listview in your layout xml
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Declare list as a instance variable
ArrayList<CommentsLibrary> list = new ArrayList<CommentsLibrary>();
Then
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name","defaultValue");
String content = jsonObject.optString("content","defaultValue");
String published = jsonObject.optString("published","defaultValue");
list.add(new CommentsLibrary(name, content, published));
}
Then initialize listview
ListView lv =(ListView)findViewById(R.id.list);
CustomAdapter cus = new CustomAdapter(ActivityName.this,list);
lv.setAdapter(cus);
Define a custom layout with 3 textviews. (list_item.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="31dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="24dp"
android:text="TextView" />
</RelativeLayout>
Then define a custom adapter by extending a base adapter. Override getView
inflate the custom layout and set text views there.
public class CustomAdapter extends BaseAdapter
{
LayoutInfalter mInflater;
ArrayList<CommentsLibrary> list;
public CustomAdapter(Context context,ArrayList<CommentsLibrary> list)
{
mInflater = LayoutInfalter.from(context);
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null)
{
convertView =minflater.inflate(R.layout.list_item, parent,false);
holder = new ViewHolder();
holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
holder.tv3 = (TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
}
else
{
holder= (ViewHolder) convertView.getTag();
}
holder.tv1.setText(list.get(position).getName());
holder.tv2.setText(list.get(position).getContent());
holder.tv3.setText(list.get(position).getPublished());
return convertView;
}
static class ViewHolder
{
TextView tv1,tv2,tv3
}
}
这篇关于更换JSON填充的TextView使用JSON已填充的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!