Passing object to new activity in a clickable List View - android

I've implemented a clickable List View using an ArrayAdaptor:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_tasks);
myTasksListView = (ListView) findViewById(R.id.mytasks_listView);
myTasksListView.setOnItemClickListener(this);
tasks = getTasks(); //of type Task[]
ArrayAdapter<Task> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, tasks);
myTasksListView.setAdapter(adapter);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Intent intent = new Intent(this, TaskDetail.class);
intent.putExtra("position", position);
intent.putExtra("id", id);
startActivity(intent);
}
Now, in the new activity I'm starting on item click, I can get the position of the item that was clicked; however, I'm not sure what would be the best method of getting the actual array. Can I
Find the Activity that launched the new activity and then access the ArrayAdaptor from there?
make my class Task parcelable and then just call intent.putExtra() on my object itself?
or is this another approach that's considered "best practice"? I was trying to follow along this helpful comment but it looks like the array in question was hard coded?

The best approach would be to make the Task class implement the Parcelable interface. If you did so, you'd be able to use Intent.putExtra(String, Parcelable[]) and simply call
intent.putExtra("tasks", tasks);

Related

new intent after click callback

I have a listview and when it clicks on a certain option I want it to create a new intent. Like a menu item. This is my first time working with list views, so I'm probably just missing the right way to do this, but is there an easy fix?
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.mainlist);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
TextView textView = (TextView) viewClicked;
Toast.makeText(main.this, position, Toast.LENGTH_SHORT).show();
if(textView.getText().toString() == "my Profile"){
Intent intent = new Intent(this.getApplicationContext(), myProfile.class);
startActivity(intent);
}
}
});
cannot resolve method 'getapplicationcontext'
You can't use this.getApplicationContext() since you're inside a new AdapterView.OnItemClickListener() and there,this doesn't have that method.
You should create a class field containing the application context and use it or use viewClicked.getContext() as suggested by #codeMagic.
Also, as you might have been read in the comments, you should use .equals for string comparison. The == operator is not enough.
Try -
Intent intent = new Intent(MainActivity.this, myProfile.class);
startActivity(intent);
Where MainActivity is he activity where you implemented onItemClick().
You should replace it by your activity name.

Displaying ListView Items in another activity thruough onItemLongClick method

How to display the details of any of the items clicked in a listview in another activity?
I referred to this. But unfortunately couldn't understand as to how to go about in displaying the details. Could anyone please help me on this?
you can use putExtra of intent and can pass any value or any serialized class object
yourActivity.java
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Intent intent = new Intent(yourActivity.this,item.class);
intent.putExtra("item",parent.getSelectedItem().toString);
startActivity(intent)
}
item.java
Intent intent = getIntent();
String itemText = intent.getStringExtra("item");
textView.settext(itemText);
hope u got the point..:

What is the right way to edit/delete items from ListView

So, I have MainActivity with ArrayList< MyObject>, ListView for display it and EditActivity to get UI for editing items. Adapter for ListView extends ArrayAdapter< MyObject>. When user click on item I want to start EditActivity with object for editing. How could I put the object to EditActivity? I have:
Intent i = new Intent(this, EditActivity.class);
startActivity(i);
how could I get the object in EditActivity?
Of course, I could declare ArrayList< MyObject> as static and put index of the item with:
Intent i = new Intent(this, EditActivity.class);
i.putExtra("index", iItemIdex);
startActivity(i);
and then, in EditActivity, get it like:
int iIndex = getIntent().getExtras().getInt("index");
MyObject o = MainActivity.MyArray.get(iIndex);
but I guess that is not best decision :-)
If you want to edit a ListView, you just edit the ArrayAdapter by using its add, insert, remove, and clear functions on the Adapter.
After you've done that, you call the notifyDataSetChanged() to notify that the contents of the ArrayAdapter have changed. Your ListView will be updated with the new values.
You don't have to declare your List static. Here is the code you should have to get it to work (one possibility) :
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
MyObjectClass obj = (MyObjectClass) arg0.getAdapter().getItem(arg2);
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("myKey", obj);
startActivity(intent);
}
This way there is no useless static variables, and you are using the method the way it has been made for. One of the easiest and cleanest solution IMO.
Be carefull, to use this method, your have to redefine the getItem(int) method in your custom ArrayAdapter. You should do this :
#Override
public MyObjectClass getItem(int position) {
return this.myList.get(position);
}
EDIT : Then if you want to be able to remove items, I think you should put the whole list containing your objects in the intent (don't declare it static). Then just call add()/remove() methods, and when you want to update UI to show the modifed list, just call notifyDataSetChanged() on your custom ArrayAdapter, for example when returning in MainActivity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
yourListAdater.notifyDataSetChanged();
/* Assuming you have a reference pointing to your adapter in your MainActivity, if you don't just do: ((MyCustomAdapterClass) myListView.getAdapter()).notifyDataSetChanged(); */
}

Implementing viewpager in a list

How to implement an ExpandableList in a ViewPager in Android?
My question is the exact opposite of the above question, I want to display a list view (preferably an expandable one) and when an item is clicked I want display a bunch of images (different ones for each item of course).
P.S: Please dont tell me to launch a different activity for each list item.
This is some possible guidance not solution. (I am not exactly clear on what the problem is)
I would probably just create one activity. Each time you get a click on the listview it creates a intent with some stored flags. Then you start the activity with that intent. Unpackage the intent to find out what you want you activity to do.
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, myclass.class);
intent.putExtra(KEY, v.getId())
intent.putExtra(KEY2, position)
intent.putExtra(KEY3, id)
startActivity(intent);
}
};
And then get the intents flags in the new activity onCreate()
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
resId = intent.getIntExtra(KEY, 0);
...
super.setTitle(title);
Not sure if that what you were looking for hope it helps

Get specific item property on onListItemClick() using custom ArrayAdapter<>

I have a ListActivity that displays a list of search results I grab from a webservice off the internet. I parse the XML I receive into an ArrayList<MyObjects> which I then bind to a ListView using my own adapter (as in MyObjectAdapter extends ArrayAdapter<MyObject>).
So then I want the user to be able to click on one of the items in the list. Each item has an identifier which will then be put into an intent and sent to a new activity (which then triggers a new webservice request based on this, downloads the rest of the data). But I don't know how to get this one property of the MyObject that was selected.
Here's the onListItemClick() method as it stands:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String myObjectId;
// I want to get this out of the selected MyObject class here
Intent i = new Intent(this, ViewObject.class);
i.putExtra("identifier_key", myObjectId);
startActivityForResult(i, ACTIVITY_VIEW);
}
If you are using ArrayAdapter you must initialize that adapter with an array, right? So, the better you can do is to use the postition that you get from onListItemClick and take the object from the original array.
For instance:
// somewhere you have this
ArrayList<MyObjects> theItemsYouUsedToInitializeTheArrayAdapter;
// and inside onListItemClick....
String myObjectId = theItemsYouUsedToInitializeTheArrayAdapter.get(position).getObjectId();
try this getIntent().getExtras().getString(key);

Categories

Resources