我有一个活动,在将数据填充到数组适配器时遇到问题。我的问题在于" setdata方法"数组适配器。
在方法parseJSONRespone中,如果我使用下一个代码,则一切正常并且数据显示在Listview中:
JSONArray entries = new JSONArray(s);
lvA.clear();
for (int count = 0; count < entries.length(); count++) {
JSONObject anEntry = entries.getJSONObject(count);
lvA.add(new ObjectLogados(anEntry.getString("usuario"),anEntry.getInt("nr_usuario"),
anEntry.getString("nh_data_hora"), anEntry.getString("aplicacao"),
anEntry.getString("tipousu"), anEntry.getString("atendendo"), anEntry.getString("atd_com_preferencia"),
anEntry.getString("atd_sem_preferencia")));
}
lvA.notifyDataSetChanged();
但如果我使用下一个,listview项目总是空的。我的列表视图中没有数据显示。我的setData方法有什么问题?
JSONArray entries = new JSONArray(s);
tmplista.clear();
for (int count = 0; count < entries.length(); count++) {
JSONObject anEntry = entries.getJSONObject(count);
tmplista.add(new ObjectLogados(anEntry.getString("usuario"), anEntry.getInt("nr_usuario"), anEntry.getString("nh_data_hora"), anEntry.getString("aplicacao"),
anEntry.getString("tipousu"), anEntry.getString("atendendo"), anEntry.getString("atd_com_preferencia"),
anEntry.getString("atd_sem_preferencia")));
}
lvA.setData(tmplista);
这是我的活动
public class LogadosActivity extends Activity {
ObjectLogados cur_ob;
ArrayList<ObjectLogados> lista = new ArrayList<ObjectLogados>();
ArrayList<ObjectLogados> tmplista= new ArrayList<ObjectLogados>();
ListviewLogAdapter lvA;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle args = new Bundle();
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_logados);
lv = (ListView) findViewById(R.id.list_log);
View v = (View) findViewById(R.id.empty_log);
lv.setEmptyView(v);
lvA = new ListviewLogAdapter(LogadosActivity.this, lista);
lv.setAdapter(lvA);
lvA.setData(lista);
//..do more something
execute_search();
}
private void parseJSONRespone(JSONObject response) {
// TODO Auto-generated method stub
try {
JSONArray LResult = response.getJSONArray("result");
String s = LResult.get(0).toString();
JSONArray entries = new JSONArray(s);
tmplista.clear();
//lvA.clear();
for (int count = 0; count < entries.length(); count++) {
JSONObject anEntry = entries.getJSONObject(count);
//lvA.add(new ObjectLogados(anEntry.getString("usuario"),anEntry.getInt("nr_usuario"),
tmplista.add(new ObjectLogados(anEntry.getString("usuario"), anEntry.getInt("nr_usuario"),
anEntry.getString("nh_data_hora"), anEntry.getString("aplicacao"),
anEntry.getString("tipousu"), anEntry.getString("atendendo"), anEntry.getString("atd_com_preferencia"),
anEntry.getString("atd_sem_preferencia")));
}
lvA.setData(tmplista);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void execute_search() {
String url = Common.getServerURL() + "\"PrefAtdMed\"";
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
parseJSONRespone(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
jr.setRetryPolicy(new DefaultRetryPolicy(Common.DEFAULT_TIMEOUT_MS,
Common.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Common.getInstance().addToReqQueue(jr, "jreq"); }
}
我的模特是:
public class ObjectLogados {
private String usuario;
private int nr_usuario;
private String nh_data_hora;
private String aplicacao;
private String tipousu;
private String atendendo;
private String atd_com_preferencia;
private String atd_sem_preferencia;
public Boolean getBatd_com_preferencia() {
return atd_com_preferencia != null && atd_com_preferencia.equals("T");
}
public Boolean getBatd_sem_preferencia() {
return atd_sem_preferencia != null && atd_sem_preferencia.equals("T");
}
public Boolean getBatendendo() {
return atendendo != null && atendendo.equals("T");
}
public String getAplicacao() {
return aplicacao;
}
public void setAplicacao(String aplicacao) {
this.aplicacao = aplicacao;
}
public String getAtd_com_preferencia() {
return atd_com_preferencia;
}
public void setAtd_com_preferencia(String atd_com_preferencia) {
this.atd_com_preferencia = atd_com_preferencia;
}
public String getAtd_sem_preferencia() {
return atd_sem_preferencia;
}
public void setAtd_sem_preferencia(String atd_sem_preferencia) {
this.atd_sem_preferencia = atd_sem_preferencia;
}
public String getAtendendo() {
return atendendo;
}
public void setAtendendo(String atendendo) {
this.atendendo = atendendo;
}
public String getNh_data_hora() {
return nh_data_hora;
}
public void setNh_data_hora(String nh_data_hora) {
this.nh_data_hora = nh_data_hora;
}
public String getTipousu() {
return tipousu;
}
public void setTipousu(String tipousu) {
this.tipousu = tipousu;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public int getNr_usuario() {
return nr_usuario;
}
public void setNr_usuario(int nr_usuario) {
this.nr_usuario = nr_usuario;
}
public ObjectLogados( String usuario, int nr_usuario,String nh_data_hora, String aplicacao, String tipousu, String atendendo, String atd_com_preferencia, String atd_sem_preferencia)
{
super();
this.usuario = usuario;
this.nr_usuario = nr_usuario;
this.nh_data_hora = nh_data_hora;
this.aplicacao = aplicacao;
this.tipousu = tipousu;
this.atendendo = atendendo;
this.atd_com_preferencia = atd_com_preferencia;
this.atd_com_preferencia = atd_sem_preferencia;
}
}
我的适配器是:
public class ListviewLogAdapter extends ArrayAdapter<ObjectLogados> {
ArrayList<ObjectLogados> items;
private Activity context;
public ListviewLogAdapter(Activity context, ArrayList<ObjectLogados> items) {
super(context, R.layout.singlerowlog, items);
this.context = context;
this.items = items;
}
public void add(ObjectLogados items){
Log.v("AddView", items.getUsuario());
this.items.add(items);
}
public void setData(ArrayList<ObjectLogados> items) {
this.items = items;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ObjectLogados o = items.get(position);
final ViewHolder holder;
View vi = convertView;
if (vi == null){
LayoutInflater inflater = context.getLayoutInflater();
vi = inflater.inflate(R.layout.singlerowlog, null);// e' preciso fazer o inflate pois a view nao ainda nao esta em activity alguma
holder = new ViewHolder();
holder.ivImgMedLog= (ImageView) vi.findViewById(R.id.ivp_imagemedlog) ;
holder.tv_usuario = (TextView) vi.findViewById(R.id.tv_hnomemedlogado);
holder.cb_atendendo=(CheckBox)vi.findViewById(R.id.chk_atendendo);
holder.cb_atd_com_preferencia=(CheckBox)vi.findViewById(R.id.chk_atendendo_cp);
holder.cb_atd_sem_preferencia=(CheckBox)vi.findViewById(R.id.chk_atendendo_sp);
holder.tv_nh_data_hora=(TextView) vi.findViewById(R.id.tv_datalog);
holder.tv_aplicacao=(TextView) vi.findViewById(R.id.tv_applog);
holder.tv_tipousu=(TextView) vi.findViewById(R.id.tv_tipousulog);
vi.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_usuario.setText(o.getUsuario());
holder.tv_nh_data_hora.setText(o.getNh_data_hora());
holder.tv_aplicacao.setText(o.getAplicacao());
holder.tv_tipousu.setText(o.getTipousu());
holder.cb_atendendo.setChecked(o.getBatendendo());
holder.cb_atd_com_preferencia.setChecked(o.getBatd_com_preferencia());
holder.cb_atd_sem_preferencia.setChecked(o.getBatd_sem_preferencia());
String imageKey="MED"+String.valueOf(o.getNr_usuario());
final Bitmap bitmap = Common.getInstance().getBitmapFromMemCache(imageKey);
if (bitmap != null) {
holder.ivImgMedLog.setImageBitmap(bitmap);
}else {
//new ImageDownloaderTask(context, Common.getServerURL() + "\"RetrieveImagePac\"", Integer.valueOf(o.getPront()), holder.ivImg).execute();
holder.ivImgMedLog.setImageDrawable(context.getResources()
.getDrawable(R.drawable.empty));
downloadimage(holder.ivImgMedLog,o.getNr_usuario());
}
return vi;
}
static class ViewHolder {
TextView tv_usuario;
TextView tv_nh_data_hora;
TextView tv_aplicacao;
TextView tv_tipousu;
CheckBox cb_atendendo;
CheckBox cb_atd_com_preferencia;
CheckBox cb_atd_sem_preferencia;
ImageView ivImgMedLog;
}
private void downloadimage(final ImageView imageView,final int nr){
//final ImageView imageView=imV;
String url=Common.getServerURL() + "\"RetrieveImageStaff\"";
JSONArray ja = new JSONArray();
ja.put(nr);
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("_parameters", ja);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url,
jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject anEntry = null, entries = null;
String ims = "";
try { // TODO Auto-generated method stub
JSONArray LResult = response.getJSONArray("result");
String s = LResult.get(0).toString();
anEntry = new JSONObject(s);
ims = anEntry.getString("imagem");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap decodedByte = null;
try {
if (!TextUtils.isEmpty(ims)) {
byte[] decodedString = Base64.decode(ims, Base64.DEFAULT);
decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (imageView != null) {
if (decodedByte != null) {
imageView.setImageBitmap(decodedByte);
Common.getInstance().addBitmapToMemoryCache("MED"+String.valueOf(nr), decodedByte);
} else {
imageView.setImageDrawable(imageView.getContext().getResources()
.getDrawable(R.drawable.empty));
}
}
} else {
imageView.setImageDrawable(imageView.getContext().getResources()
.getDrawable(R.drawable.empty));
}
}catch (Exception e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
//Log.v(TAG, error.getMessage());
Toast.makeText(context, "Erro adquirindo imagem...." + error.getMessage(), Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
});
jr.setRetryPolicy(new DefaultRetryPolicy(Common.DEFAULT_TIMEOUT_MS,
Common.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Common.getInstance().addToReqQueue(jr, "jreq"); //reqQueue.add(jr);
}
}
我的布局是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:id="@+id/h_namepac"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Usuários Logados"
android:background="@color/wallet_holo_blue_light"
android:textAppearance="?android:attr/textAppearanceLarge" />
<FrameLayout
android:id="@+id/listContainerLogados"
android:layout_width="match_parent"
android:layout_below="@id/h_namepac"
android:visibility="visible"
android:layout_height="match_parent">
<ListView android:id="@+id/list_log"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#FFCC00"
android:dividerHeight="4px"
android:drawSelectorOnTop="false"
/>
<TextView android:id="@+id/empty_log"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Sem itens"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</RelativeLayout>
答案 0 :(得分:0)
我认为在第一种情况下,当你执行lva.add(假设它是列表视图对象)时,适配器负责为你添加项目到列表。但是,如果您使用的是setData,我认为它每次都会重置您的items数组,而不是向其中添加数据。
解决方案是将项添加到arrayList并在添加后调用notifyDataSetChanged。