Galera,
To com um problema aqui e não to sabendo resolver por "Leiguisse".
Tenho a MainActivity que tem uma AsyncTask que me busca os dados de um jSon. Como é AsyncTask quando retorna o valor do jSon a MainActivity já termino o processamento e eu perco a minha VIEW. Quando tento jogar o retorno do jSon na tela da NullPointer pq não consigo encontrar a minha ListView.
Quero saber como faço pra ter minha listview nesse momento.
O momento do erro esta em Azul.
MainActivity
public class MainActivity extends ActionBarActivity {
private ProgressDialog pd;
private String URL_WS = "http://teste.obraedecoracao.com/bepro/request.php";
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Retorno> retList = new ArrayList<Retorno>();
// Progresso por tempo indeterminado.
pd = null;
pd = ProgressDialog.show(this, "Aguarde",
"Sincronizando com WebService", true, false);
// pd.onStart();
listView = (ListView) findViewById(R.id.listaPrincipal);
// Chamada do WebService
String obJson = JSONFile(URL_WS);
if (!obJson.equals("")) {
// Trata Retorno da String JSON
try {
retList = trataResult(obJson);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
preparaTela(retList);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView
.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Posição :" + itemPosition + " Item : " + itemValue,
Toast.LENGTH_SHORT).show();
}
});
}
private String JSONFile(String url) {
String retorno = "";
try {
GetHttp task = new GetHttp();
task.execute(url);
// retorno = task.get();
Log.d("WS", "Sincronizado com sucesso");
} catch (Exception e) {
Log.e("WS", e.toString());
pd.dismiss();
}
return retorno;
}
/**
*
* @param resultado
* @throws JSONException
*/
public List<Retorno> trataResult(String resultado) throws JSONException {
List<Retorno> retList = null;
JsonParser parser = new JsonParser();
JsonObject rootObejct = parser.parse(resultado).getAsJsonObject();
JsonElement ret = rootObejct.get("retorno");
JsonArray jArray = ret.getAsJsonArray();
Gson gson = new Gson();
retList = new ArrayList<Retorno>();
for (JsonElement obj : jArray) {
Retorno lista = gson.fromJson(obj, Retorno.class);
retList.add(lista);
}
return retList;
}
/**
*
* @param retList
*/
@SuppressLint("ServiceCast") public void preparaTela(List<Retorno> retList) {
ArrayList<String> promo = new ArrayList<String>();
int i = 0;
if (retList != null && retList.size() != 0) {
for (i = 0; i < retList.size(); i++) {
promo.add(i, retList.get(i).getProduto());
}
} else {
for (i = 0; i < 10; i++) {
promo.add(i, "Aguarde...");
}
}
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ApplicationContextProvider.getContext(),
R.layout.list_row, R.id.listtext, promo);
//listView = (ListView) getWindow().getDecorView().findViewById(R.id.listaPrincipal);
listView = (ListView) findViewById(R.id.listaPrincipal);
// Assign adapter to ListView
listView.setAdapter(adapter);
pd.dismiss();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Metodos Async
protected String doInBackground(String... url) {
String recebe = null;
//Busca String JSON
try {
recebe = GetHttpRequest(url);
} catch (Exception e) {
e.printStackTrace();
}
return recebe;
}
protected void onPostExecute(String result) {
List<Retorno> retList = new ArrayList<Retorno>();
MainActivity mActivity = new MainActivity();
//Trata Retorno da String JSON
try {
retList = mActivity.trataResult(result);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
mActivity.preparaTela(retList);
}
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.






0 comentários:
Postar um comentário