2013-04-28 5 views
1

Я использую загрузку данных нового Runnable httprequest в одном из моих классов. Я использую этот класс для заполнения ListView в одном из моих действий, но дело в том, что представления загружаются до того, как данные были загружены. Я попытался переместить мой метод инициализации, который инициализирует все представления, но ничего не произошло. Каждый раз, когда я загружаю приложение, я должен перезагрузить основной вид, потому что в ListView нет записей. LoadReportList() - это метод, который создает экземпляр класса, содержащего метод HTTP-запроса.Загрузка ListViewView Android перед загрузкой данных - необходимо перезапустить вид

public class HomeActivity extends Activity { 

private ArrayList<Report> reportList = null; 

ListView listview; 

private final boolean DEBUG = true; 

private MyCustomArrayAdapter adapter = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home);  
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    loadReportList(); 
    initialize(); 
} 

/** 
* This method initializes all the components needed for the activity 
*/ 
private void initialize() 
{ 
    /* Get a reference of the listview in our xml view */ 
    listview = (ListView) findViewById(R.id.reportsList); 

    adapter = new MyCustomArrayAdapter(this, R.layout.single_listview_row); 

    try{ 
     // Populate the list, through the adapter 
     for(final List_Item entry : getListEntries()) { 
      adapter.add(entry); 
      if(DEBUG)Log.i("HomeActivity","in adding List_Item entries to the adapter"); 
     } 
    }catch(Exception e){ 
     Log.i("In initialize() HomeActivity","Could not load the list with entries"); 

     loadReportList(); 
    } 

    listview.setAdapter(adapter); 

    listview.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { 

      /* Class to assist us in loading the activity */ 
      Class editClass = null; 
      try { 
       editClass = Class.forName(".DetailsActivity"); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      /* create bundle to pass the ID of the deck that was clicked */ 
      Bundle reportPassed = new Bundle(); 
      //reportPassed.putInt("Report", reportList.get(4).getId()); 
      reportPassed.putSerializable("report", reportList.get(position)); 

      /* Start the new intent and also pass a bundle that will contain the name of the card that was clicked */ 
      Intent ourIntent = new Intent(HomeActivity.this, editClass); 
      ourIntent.putExtras(reportPassed);//passing the bundle to the activity 
      //start the activity 
      startActivity(ourIntent); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.home, menu); 
    return true; 
} 

/* Create the list of objects of type List_Item, in our case it will be Decks */ 
private List<List_Item> getListEntries(){ 

    /* Create an ArrayList of List_Items */ 
    final ArrayList<List_Item> entries = new ArrayList<List_Item>(); 

    if(reportList == null || reportList.isEmpty()) 
     loadReportList(); 

    /* Create the rows for the ListView by adding them into the ArrayList "entries". 
    * reportList is a global ArrayList<Report> that we populate by a method call to the class JsonParser. 
    * Look above. 
    * */ 
    for(int i = 0; i < reportList.size(); i++) { 
     if(DEBUG)Log.i("getListEntries HomeActivity","Passing through reportlistEntries"); 
     entries.add(
       new List_Item(((Report)reportList.get(i)).getType(), Integer.toString(((Report)reportList.get(i)).getId()), ((Report)reportList.get(i)).getId()) 
       ); 
    } 
    return entries; 
} 

//This method loads the reportList arraylist with all Report objects 
void loadReportList(){ 
    try { 
     reportList = new JsonParser().getArrayList(); 
    } catch (UnsupportedEncodingException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IllegalStateException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 

} 

Вот мой HTTP запрос:

//The constructor simply calls the getHttpResponse() 
    public JsonParser() throws UnsupportedEncodingException, IllegalStateException, IOException, JSONException 
{ 
    getHttpResponse(); 
} 
/* 
* This method returns an HttpResponse 
*/ 
public void getHttpResponse(){ 
    Log.i("getHttpResponse", "Right after declaring HttpResponse response");  
    new Thread(new Runnable() { 
     public void run() { 
      runRequest(); 
     } 
     }).start(); 
} 

void runRequest() 
{ 
    HttpResponse response = null; 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet request = new HttpGet(); 
    try { 
     request.setURI(new URI("http://....../report_data.json")); 
     response = client.execute(request); 
    } catch (URISyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if(response!= null) 
     Log.i(response.toString(), "testing response.toString()"); 

    //call to populate the list 
    try { 
     populateList(response); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

ответ

2

Вместо того, чтобы использовать Java нить вы можете воспользоваться классом AsyncTask в Android SDK. Вы можете показать диалог прогресса по методуPreExecute, выполнить всю тяжелую работу (в вашем случае материал http) в методе doInBackground и заполнить список и отменить диалог прогресса в методе onPostExecute. Вы можете создать внутренний класс, например, задачу, которая расширяет метод AsyncTask и вызывает его из onCreate или onResume, независимо от того, что вам подходит.

new Task().execute(); 

AsyncTask дал название «Безболезненная резьба», потому что он помогает нам сделать нашу жизнь проще. Рекомендуется рассмотреть возможность использования уже реализованного кода, предназначенного для таких задач.

Вы можете найти много потоков относительно AsyncTasks использования в StackOverflow например progressDialog in AsyncTask

+0

Просто использовал эту тему, и я исправил свой код! Спасибо дружище. Я ценю помощь. –

0

Так что вам нужно сделать, это вызов Initialize() после того, как код runRequest завершается.

Я согласен с вышеуказанным положением в том, что AsyncTask упростит это. Избавьтесь от кода потока в вашем классе JsonParser. AsyncTask позаботится об этом для вас.

Вот фрагмент.

new AsyncTask<Void, Void, Void>() { 
    @Override 
    protected Void doInBackground(Void... voids) { 
     myparser = new JSONParser(); 
     myparser.runRequest() 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     initialize(); 
    } 
}.execute(); 
Смежные вопросы