简体   繁体   English

将项目添加到自定义ListView

[英]Adding items to a custom ListView

I'm making an app and I have an activity called SearchActivity . 我正在制作一个应用程序,并且有一个名为SearchActivity的活动。 I have two custom ListViews and one works well. 我有两个自定义ListViews ,一个很好用。 My problem is the list used with AdapterEventos . 我的问题是与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. 此列表中的数据是从Post( DescarregarEventos方法)中添加的,我认为问题是因为ArrayAdapter eventos为空。 If you see my code [1], the log that I print before the setAdapter of this list returns empty. 如果看到我的代码[1],则在此列表的setAdapter之前打印的日志将返回空。 Does somebody know how I can fix this? 有人知道我该如何解决吗?

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

Thanks 谢谢

EDIT: I'm already see that POST returns the all data requested. 编辑:我已经看到POST返回请求的所有数据。 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();
        }

    }

}

} }

You can try to notify after data is added to adapter. 将数据添加到适配器后,您可以尝试通知。 So, at the end of onCreate function you can add the following line: 因此,在onCreate函数的末尾,您可以添加以下行:

mAdapter.notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM