I have a ListActivity:
public class MedTime extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.no_elements_l);
String[] receipts = getResources().getStringArray(R.array.receipts_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, receipts));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
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();
}
I'd like: when user clicks on list item, new activity开发者_JAVA百科 named "view" launches. In addition, i need to say to this "view" activity which element from list called it. For example, if I click to 3rd element of list, activity "view" must be started and show me text = 3rd element of string array. The code fragment of "view" is here (value of i in last line must be sent by main activity to this, in my example i=3):
String[] receipts = getResources().getStringArray(R.array.receipts_array);
TextView tv = new TextView(this);
tv.setText(receipts[i]);
Please, tell me how to 1) launch "view" activity when user click a list item; 2) how to launch in this case "view" with the parameter that describes number of item selected?
Add the item position to your intent and start the new activity.
....
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MedTime.this, NewActivity.class);
intent.putExtra("item", position);
startActivity(intent);
Then in the activity you started, extract the item in onCreate() from the intent.
Intent intent = getIntent();
int id = intent.getIntExtra("item", -1);
if (id != -1) {
Log.d(TAG, "Received item: " + id);
}
After this I can't say what you wanna do. One possibility is to get the string value from your array as.
String[] receipts = getResources().getStringArray(R.array.receipts_array);
String myItem = receipts[id]
third parameter "position" is clicked item position starts from zero . what else u need ??
精彩评论