ListView的数据没有显示在Android SDK中 [英] ListView data not showing on android sdk

查看:140
本文介绍了ListView的数据没有显示在Android SDK中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,我有一个名为SearchActivity的活动。我有两个自定义列表视图和一个效果很好。我的问题是与AdapterEventos使用的列表。当我启动应用程序没有出现在列表中。从这个列表中的数据是从邮政(DescarregarEventos法)加入,我认为这个问题是因为一个ArrayAdapter EVENTOS是空的。如果你看到我的code [1],我打印之前这个名单的setAdapter返回空日志。是否有人知道我怎么能解决这个问题?
我已经看到POST请求返回的所有数据。

[1] http://pastebin.com/FZacCrHD

感谢

相关code:

 公共类SearchActivity扩展ListActivity {
公众的ArrayList< Evento> EVENTOS =新的ArrayList< Evento>();
静态最后弦乐TAG =AsyncTaskParseJson.java;
静态最终的HttpClient HttpClient的=新DefaultHttpClient();公共类Evento {
    公共字符串诺姆;
    公共字符串地方;
    公共字符串INICIO;    公共Evento(诺姆字符串,字符串地方,字符串INICIO){
        this.nome =诺姆;
        this.local =地方;
        this.inicio = INICIO;
    }    公共字符串getNome(){
        返回诺姆;
    }    公共无效setNome(字符串诺姆){
        this.nome =诺姆;
    }    公共字符串中的getLocal(){
        返回地方;
    }    公共无效SETLOCAL(字符串本地){
        this.local =地方;
    }    公共字符串getInicio(){
        返回INICIO;
    }    公共无效setInicio(字符串INICIO){
        this.inicio = INICIO;
    }    }
公共类AdapterEventos扩展ArrayAdapter< Evento> {    私人最终上下文的背景下;
    私人最终的ArrayList< Evento> eventosArrayList;    公共AdapterEventos(上下文的背景下,ArrayList的< Evento> EVENTOS){        超(背景下,R.layout.listeventos,EVENTOS);        this.context =背景;
        this.eventosArrayList = EVENTOS;
    }    公共查看getViewEventos(INT位置,查看convertView,父母的ViewGroup){        //创建气筒
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        //从吹气获取rowView
        查看LinhaEventoView = inflater.inflate(R.layout.listeventos,父母,假);        //从rowView文本视图
        TextView的nomeView =(TextView中)LinhaEventoView.findViewById(R.id.tvNomeEvento);
        TextView的localView =(TextView中)LinhaEventoView.findViewById(R.id.tvLocalEvento);
        TextView的inicioView =(TextView中)LinhaEventoView.findViewById(R.id.tvInicioEvento);        //设置TextView的文本
        nomeView.setText(eventosArrayList.get(位置).getNome());
        localView.setText(eventosArrayList.get(位置).getLocal());
        inicioView.setText(eventosArrayList.get(位置).getInicio());        //返回rowView
        返回LinhaEventoView;
    }
}
@覆盖
保护无效的onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
的setContentView(R.layout.search_activity);         新DescarregarEventos()执行();ListView控件ListEventos =(ListView控件)findViewById(R.id.listEventos);
Log.d(EVENTOS,EVENTOS:+ EVENTOS);
ListEventos.setAdapter(新AdapterEventos(这一点,EVENTOS));
公共类DescarregarEventos扩展的AsyncTask<字符串,字符串,字符串> {
JSONArray dataJsonArr = NULL;保护字符串doInBackground(字符串... ARG){
    HttpPost httppost =新HttpPost(eventosUrl);
    字符串EVT = NULL;尝试{    // CriarparâmetrosØ段后。
    清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
    params.add(新BasicNameValuePair(EVENTOS,数据));    httppost.setEntity(新UrlEn codedFormEntity(PARAMS));    // ExecutarØ帖子。
    ResponseHandler所<串GT; ResponseHandler所=新BasicResponseHandler();
    EVT = httpclient.execute(httppost,ResponseHandler所);}赶上(UnsupportedEncodingException五){    Log.d(HTTP,ERRO一个ADICIONAR OSPARÂMETROSPARAØPOST EM \\DescarregarEventos()\\);
    e.printStackTrace();}赶上(IOException异常五){    Log.d(HTTP,ERRO EM \\DescarregarEventos()\\);
    e.printStackTrace();
}返回EVT;}// Tratar一个resposta做邮政电子adicionar AO阵列respetivo。
公共无效onPostExecute(字符串EVT){
    尝试{
        JSONArray E =新JSONArray(EVT);
        的for(int i = 0; I< E.length();我++){
            JSONObject的evento = E.getJSONObject(I)
            eventos.add(新Evento(evento.getString(咔嗒),evento.getString(地点客户),evento.getString(data_inicio)));
        }    }赶上(JSONException E){
        Log.d(HTTP,ERRO一个TRATAR OS EVENTOS EM \\DescarregarEventos()\\ - \\onPostExecute()\\);
        e.printStackTrace();
    }}}}


解决方案

在第一次创建AdapterEventos的情况下,

  AdapterEventos adapterEventos =新AdapterEventos(这一点,EVENTOS);
ListEventos.setAdapter(adapterEventos);

现在每次添加项目时间 EVENTOS 您必须通知新的数据已经到达适配器。

  eventos.add(新Evento(evento.getString(诺姆),evento.getString(地点客户),evento.getString(data_inicio)));
adapterEventos.notifyDataSetChanged();

I'm making an app and I have an activity called SearchActivity. I have two custom ListViews and one works well. My problem is the list used with AdapterEventos. When I start the app nothing appears in this list. The data from this list is added from a Post (DescarregarEventos method) and I think the problem is because the ArrayAdapter eventos is empty. If you see my code [1], the log that I print before the setAdapter of this list returns empty. Does somebody know how I can fix this? I'm already see that POST returns the all data requested.

[1] http://pastebin.com/FZacCrHD

Thanks

RELEVANT CODE:

public class SearchActivity extends ListActivity {


public ArrayList<Evento> eventos = new ArrayList<Evento>();
static final String TAG = "AsyncTaskParseJson.java";
static final HttpClient httpclient = new DefaultHttpClient();

public class Evento {
    public String nome;
    public String local;
    public String inicio;

    public Evento(String nome, String local, String inicio) {
        this.nome = nome;
        this.local = local;
        this.inicio = inicio;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    public String getInicio() {
        return inicio;
    }

    public void setInicio(String inicio) {
        this.inicio = inicio;
    }

    }


public class AdapterEventos extends ArrayAdapter<Evento> {

    private final Context context;
    private final ArrayList<Evento> eventosArrayList;

    public AdapterEventos(Context context, ArrayList<Evento> eventos) {

        super(context, R.layout.listeventos, eventos);

        this.context = context;
        this.eventosArrayList = eventos;
    }

    public View getViewEventos(int position, View convertView, ViewGroup parent) {

        //Create inflater
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //Get rowView from inflater
        View LinhaEventoView = inflater.inflate(R.layout.listeventos, parent, false);

        //Get the text view from the rowView
        TextView nomeView = (TextView) LinhaEventoView.findViewById(R.id.tvNomeEvento);
        TextView localView = (TextView) LinhaEventoView.findViewById(R.id.tvLocalEvento);
        TextView inicioView = (TextView) LinhaEventoView.findViewById(R.id.tvInicioEvento);

        //Set the text for textView
        nomeView.setText(eventosArrayList.get(position).getNome());
        localView.setText(eventosArrayList.get(position).getLocal());
        inicioView.setText(eventosArrayList.get(position).getInicio());

        //return rowView
        return LinhaEventoView;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.search_activity);

         new DescarregarEventos().execute();

ListView ListEventos=(ListView)findViewById(R.id.listEventos);
Log.d("eventos","eventos: " + eventos);
ListEventos.setAdapter(new AdapterEventos(this, eventos));


public class DescarregarEventos extends AsyncTask<String, String, String> {


JSONArray dataJsonArr = null;

protected String doInBackground(String... arg) {


    HttpPost httppost = new HttpPost(eventosUrl);
    String evt = null;

try {

    //Criar parâmetros para o Post.
    List<NameValuePair> params = new ArrayList<NameValuePair>();    
    params.add(new BasicNameValuePair("eventos", "data"));

    httppost.setEntity(new UrlEncodedFormEntity(params));

    //Executar o Post.
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    evt = httpclient.execute(httppost, responseHandler);

} catch (UnsupportedEncodingException e) {          

    Log.d("HTTP","ERRO A ADICIONAR OS PARÂMETROS PARA O POST EM \"DescarregarEventos()\"");
    e.printStackTrace();

} catch (IOException e) {

    Log.d("HTTP", "ERRO EM \"DescarregarEventos()\"");
    e.printStackTrace();
}

return evt;

}

// Tratar a resposta do Post e adicionar ao array respetivo.
public void onPostExecute(String evt) {
    try {
        JSONArray E = new JSONArray(evt);
        for (int i = 0; i < E.length(); i++) {
            JSONObject evento = E.getJSONObject(i);
            eventos.add(new Evento(evento.getString("nome"),evento.getString("localizacao"),evento.getString("data_inicio")));
        }

    } catch (JSONException e) {
        Log.d("HTTP","ERRO A TRATAR OS EVENTOS EM \"DescarregarEventos() \" - \"onPostExecute()\"");
        e.printStackTrace();
    }

}

}

}

解决方案

at first create instance of AdapterEventos,

AdapterEventos adapterEventos = new AdapterEventos(this, eventos);
ListEventos.setAdapter(adapterEventos );

now every time you add item to eventos you have to notify the adapter that new data has arrived.

eventos.add(new Evento(evento.getString("nome"),evento.getString("localizacao"),evento.getString("data_inicio")));
adapterEventos.notifyDataSetChanged();

这篇关于ListView的数据没有显示在Android SDK中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆