Android : Open New Activity by id on Listview Item Click

on Monday, April 13, 2015


My ListView is opening and everything is ok. I don´t know how to pass params from onPostExecute() to onItemClick() to open a new activity (SingleItem.java) by id. Nothing that I´ve tried has worked.


ListItems.java



public class ListItems extends Activity {

private ListView listV;
TextView estado, cidade, noItem;

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

listV = (ListView) findViewById(R.id.listV);

estado = (TextView) findViewById(R.id.Estado);
cidade = (TextView) findViewById(R.id.Cidade);
noItem = (TextView) findViewById(R.id.noItem);

estado.setText(getIntent().getExtras().getString("state"));
cidade.setText(getIntent().getExtras().getString("city"));

Task task = new Task();
task.execute();

listV.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

Intent intent = new Intent(getApplicationContext(), SingleItem.class);

startActivity(intent);
}
});
}

public class Task extends AsyncTask<String, String, Void>{

private ProgressDialog progressDialog = new ProgressDialog(ListItems.this);

InputStream is = null;
String result = "";

protected void onPreExecute() {
progressDialog.setMessage("Listing Items...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {

@Override
public void onCancel(DialogInterface dialog) {
Task.this.cancel(true);
}
});
};

@Override
protected Void doInBackground(String... params) {

String url = "http://myip/webviews/jsonlistItems.php";

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

try {
httpPost.setEntity(new UrlEncodedFormEntity(param));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();

is = httpEntity.getContent();

} catch (Exception e) {
Log.e("log_tag", "Error connecting to database " + e.toString());
Toast.makeText(ListItems.this, "Try again.", Toast.LENGTH_LONG).show();
}

try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";

while((line = br.readLine()) != null){
sb.append(line+"\n");
}
is.close();
result = sb.toString();

}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}

protected void onPostExecute(Void v){

try {
JSONArray Jarray = new JSONArray(result);
for (int i = 0; i < Jarray.length(); i++) {
JSONObject jsonObject = null;
jsonObject = Jarray.getJSONObject(i);

// output
String item_id = jsonObject.getString("item_id");

String item_name = jsonObject.getString("item_name");
String item_color = jsonObject.getString("item_color");
String city = jsonObject.getString("city");
String statee = jsonObject.getString("state");

if(estado.getText().toString().equalsIgnoreCase(statee) &&
cidade.getText().toString().equalsIgnoreCase(city)){

String[] values = new String[] {item_name, item_color};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);

listV.setAdapter(adapter);
break;
}
else{
noItem.setText("No Item to show");
}
}
this.progressDialog.dismiss();
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
}

}


public class EventById{

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_events, 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);
}
}


SingleItem.java



public class SingleItem extends Activity {

TextView item_name, item_color;

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

item_name = (TextView) findViewById(R.id.item_name);
Item_color = (TextView) findViewById(R.id.item_color);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event, 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);
}
}


How to pass params from onPostExecute() to onItemClick() to open a new activity (SingleItem.java) by id?


0 comments:

Post a Comment