我正在为时间线构建一个评论系统,但我在从 JSON 加载评论时遇到问题,我在加载时间线上的帖子之前使用了相同的方法,并且工作起来就像一个魅力。现在正在加载,但没有显示任何内容。
我有 2 个布局,singleitemview.xml 包含帖子本身和一个名为 listcomment 的列表视图,用于加载评论:
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:orientation="horizontal">
<ListView
android:id="@+id/listcoments"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
此 LinearLayout 在其他 4 个线性布局中。
其他布局 list_comenarios.xml 具有列表视图的格式:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="1dp"
android:minWidth="50dip"
android:contentDescription="Foto perfil"/>
</LinearLayout>
<LinearLayout
android:id="@+id/quienLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/thumbnail"
android:background="#ABD2FA" >
<TextView
android:textColor="#333"
android:shadowColor="#000000"
android:textSize="14sp"
android:id="@+id/quien"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:orientation="vertical"
android:layout_below="@id/quienLay"
android:layout_toRightOf="@+id/thumbnail"
>
<TextView
android:id="@+id/comentario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#eee"/>
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:orientation="horizontal"
android:background="#eee">
<TextView
android:id="@+id/fecha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="9sp"
android:textColor="#999"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
奇怪的是,模拟器的视图日志上没有显示任何错误。这是我用于加载数组的类和适配器:
适配器:
package com.gettford.community;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListComentAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListComentAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView quien;
TextView comentario;
ImageView imagen;
TextView fecha;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_comentarios, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
// Locate the ImageView in listview_item.xml
imagen = (ImageView) itemView.findViewById(R.id.imagen);
quien = (TextView) itemView.findViewById(R.id.quien);
comentario = (TextView) itemView.findViewById(R.id.comentario);
fecha = (TextView) itemView.findViewById(R.id.fecha);
// Capture position and set results to the TextViews
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(SingleItemView.IMAGEN), imagen);
quien.setText(resultp.get(SingleItemView.QUIEN));
comentario.setText(resultp.get(SingleItemView.COMENTARIO));
fecha.setText("Publicado "+resultp.get(SingleItemView.FECHA));
// Capture ListView item click
return itemView;
}
}
以及单独加载每个帖子的类:
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
public class SingleItemView extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
// Declare Variables
String quien;
String contenido;
String imagen;
String idpost;
String fecha;
public static String usuarioID;
public static SessionManager session;
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListComentAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String IDPOST = "idpost";
static String QUIEN = "quien";
static String IMAGEN = "imagen";
static String COMENTARIO = "comentario";
static String FECHA = "fecha";
ImageLoader imageLoader = new ImageLoader(this);
static private final String DEVELOPER_KEY = "APIKEY";
static private final String VIDEO = "VIDEOTOLOAD";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
session = new SessionManager(getApplicationContext());
session.checkLogin();
new DownloadJSON().execute();
Intent i = getIntent();
// Get the result of rank
// Get the result of country
quien = i.getStringExtra("quien");
// Get the result of population
contenido = i.getStringExtra("contenido");
// Get the result of flag
imagen = i.getStringExtra("imagen");
fecha = i.getStringExtra("fecha");
HashMap<String, String> usuario = session.getUserDetails();
usuarioID = usuario.get(SessionManager.KEY_USER_ID);
YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
// Locate the TextViews in singleitemview.xml
TextView txtquien = (TextView) findViewById(R.id.quien);
TextView txtcontenido = (TextView) findViewById(R.id.contenido);
TextView txtfecha = (TextView) findViewById(R.id.fecha);
// Locate the ImageView in singleitemview.xml
ImageView imgflag = (ImageView) findViewById(R.id.imagen);
// Set results to the TextViews
txtquien.setText(quien);
txtcontenido.setText(contenido);
txtfecha.setText(fecha);
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(imagen, imgflag);
}
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(SingleItemView.this);
// Set progressdialog title
mProgressDialog.setTitle("Comentarios del post");
// Set progressdialog message
mProgressDialog.setMessage("Cargando...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.server.com/showing/data/json/encoded");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("comentarios");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("quien", jsonobject.getString("quien"));
map.put("imagen", jsonobject.getString("imagen"));
map.put("comentario", jsonobject.getString("comentario"));
map.put("fecha", jsonobject.getString("fecha"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listcoments);
// Pass the results into ListViewAdapter.java
adapter = new ListComentAdapter(SingleItemView.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
Toast.makeText(this, "Error cargando el servicio de video de YouTube",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
player.loadVideo(VIDEO);
}
}
正如我所说,我使用相同的类和具有不同名称的适配器来加载帖子并且工作正常。
我希望你能帮助我。
提前致谢!
编辑:我已经检查了 logcat 并且没有显示任何错误。因此,我更改了数组的名称以查看 JSON 解析器是否无法正常工作,通常情况下,我该如何调试它?