我有一个数组列表,对按钮点击调用的AsyncTask进行更改,然后我想循环遍历arraylist
for (int f = 0; f < getid.size();f++){
teste[f] = getid.get(f);
Log.d("teste", "welelelelelele ?");
}
但它不能因为getid.size返回0所以它不执行(仅在我第一次点击按钮时),但为什么?如果我按回然后再次单击该按钮它会工作,它会返回正确的大小并执行for循环。
ButtonClick
btnCriar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add((String) adapter.getItem(position));
}
final String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
Log.d("teste", String.valueOf(i));
}
Log.e("teste", "chega aqui ?===??");
new Get(outputStrArr).execute();
Log.e("teste", String.valueOf(getid.size()));
String[] teste = new String[getid.size()];
Log.e("teste", "chega aqui ?===??");
for (int f = 0; f < getid.size();f++){
teste[f] = getid.get(f);
Log.d("teste", "welelelelelele ?");
}
for (int va = 0; va < teste.length;va++){
Log.d("teste", "adadada ?");
pls +=teste[va];
}
editText.setText(pls);
Intent x = new Intent(AddCenario.this, GerirCenario.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
String text = editText.getText().toString();
x.putExtra("ola" , text);
x.putExtras(b);
startActivity(x);
listView.getSelectedItem();
}
});
Asynctask:
private class Get extends AsyncTask<String, Void, Void> {
private final String[] outputStrArr;
Get(String[] outputStrArr)
{
this.outputStrArr = outputStrArr;
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(String... integers) {
for (x = 0; x < outputStrArr.length; x++) {
try {
RequestQueue queue = Volley.newRequestQueue(AddCenario.this);
String url = "http://brunos.000webhostapp.com/teste/obter_id.php?descricao=" + outputStrArr[x];
JsonArrayRequest jsonRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); ++i) {
JSONObject obj = response.getJSONObject(i);
id[0] = obj.getString("id");
getid.add(id[0]);
Log.d("teste", "chega aqui ?");
Log.e("teste", String.valueOf(getid.size()));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonRequest);
} catch (Exception ex) {
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
完整代码:
public class AddCenario extends AppCompatActivity {
String input,pls, kappa;
Integer podeIR = 0,x;
EditText editText,esc;
String idDivisao;
private String my_sel_items;
ArrayAdapter adapter;
String[] id = new String[1];
ArrayList<String> getid = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
final ArrayList<String> divisoes = new ArrayList<>();
my_sel_items=new String();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_cenario);
editText = (EditText) findViewById(R.id.editText4);
final ListView listView = (ListView) findViewById(R.id.listview);
Button btnCriar = (Button) findViewById(R.id.button_criarr);
RequestQueue queue = Volley.newRequestQueue(AddCenario.this.getApplicationContext());
try {
String url = "http://brunos.000webhostapp.com/teste/listar_divisoes.php";
JsonArrayRequest jsonRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
public void onResponse(JSONArray response) {
try {
adapter = new ArrayAdapter(getApplicationContext(),R.layout.custom_divi_mult,divisoes);
Integer i = 0;
String divisao;
while (i!= response.length()){
JSONObject obj = response.getJSONObject(i);
idDivisao = obj.getString("id");
divisao = obj.getString("descricao");
divisoes.add(divisao);i++;
}
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonRequest);
} catch (Exception ex) {
} finally {
}
btnCriar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add((String) adapter.getItem(position));
}
final String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
Log.d("teste", String.valueOf(i));
}
Log.e("teste", "chega aqui ?===??");
new Get(outputStrArr).execute();
Log.e("teste", String.valueOf(getid.size()));
String[] teste = new String[getid.size()];
Log.e("teste", "chega aqui ?===??");
for (int f = 0; f < getid.size();f++){
teste[f] = getid.get(f);
Log.d("teste", "welelelelelele ?");
}
for (int va = 0; va < teste.length;va++){
Log.d("teste", "adadada ?");
pls +=teste[va];
}
editText.setText(pls);
Intent x = new Intent(AddCenario.this, GerirCenario.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
String text = editText.getText().toString();
x.putExtra("ola" , text);
x.putExtras(b);
startActivity(x);
listView.getSelectedItem();
}
});
}
private class Get extends AsyncTask<String, Void, Void> {
private final String[] outputStrArr;
Get(String[] outputStrArr)
{
this.outputStrArr = outputStrArr;
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(String... integers) {
for (x = 0; x < outputStrArr.length; x++) {
try {
RequestQueue queue = Volley.newRequestQueue(AddCenario.this);
String url = "" + outputStrArr[x];
JsonArrayRequest jsonRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); ++i) {
JSONObject obj = response.getJSONObject(i);
id[0] = obj.getString("id");
getid.add(id[0]);
Log.d("teste", "chega aqui ?");
Log.e("teste", String.valueOf(getid.size()));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonRequest);
} catch (Exception ex) {
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
}