I have a listView with some data coming from a server.I want to do an activity every time i press one of the list items.any idea please?
public void Status1() {
setContentView(R.layout.status);
update = (Button) findViewById(R.id.update);
refresh = (Button) findViewById(R.id.refresh);
update.setOnClickListener(update_btn);
refresh.setOnClickListener(refresh_btn);
lv1 = (ListView) findViewById(R.id.ListView);
// By using setAdpater method in listview we an add string array in
// list.
lv1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lv_arr));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
final EditText username2 = (EditText) findViewById(R.id.username1);
//String a = username1.getText().toString();
//username2.setText(a);
username2.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
// Editable a=findViewById(R.id.username).getText();
// username2.setText(a);
Toast.makeText(yassou.this, username2.getText(),
Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
i have tried this:EDIT1
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startActivity(new Intent("com.example.HelloGoogleMaps2"));
}
});
and this:EDIT2
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Intent myIntent = new Intent(view.getContext(), HelloGoogleMaps2.class);
startActivityForResult(myIntent, 0);
}
});
but noone method is working…please help!!
You can attach an intent and a bundle as part of the adapter for the listview. This will pass an appropriate bundle to the activity being launched when user clicks on the list item.