2014-03-05 119 views
0

我正在創建一個應用程序,並且我有一個名爲SearchActivity的活動。我有兩個自定義ListViews,一個很好。我的問題是與AdapterEventos一起使用的列表。當我啓動應用程序時,此列表中不會顯示任何內容 該列表中的數據來自一則博文(DescarregarEventos方法),我認爲這個問題是因爲ArrayAdaptereventos爲空。如果您看到我的代碼[1],則在此列表的setAdapter之前打印的日誌將返回空白。 有人知道我能如何解決這個問題嗎?將項目添加到自定義列表視圖

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

感謝

編輯: 我已經看到POST請求返回的所有數據。 相關代碼:

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(); 
     } 

    } 

} 

}

+0

請在您的文章中添加最**相關的代碼。 – codeMagic

回答

0

您可以嘗試的數據添加到適配器後通知。因此,在onCreate函數結束時,您可以添加以下行:

mAdapter.notifyDataSetChanged(); 
+0

你能指點我如何使用它的跡象嗎?謝謝 – pdcc

相關問題