Reputation: 9020
I have an ArrayList
of objects of class MyClass
, each of which has four fields (namely firstItem
, secondItem
, thirdItem
and fourthItem
), the first three of which need to be displayed in the ListView
of a ListFragment
. For this, I have extended the ArrayAdapter
, and overridden its getView()
method, as follows:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = layoutInflater.inflate(
R.layout.myFragment_listview_item, parent, false);
} else {
view = convertView;
}
MyClass myClassItem = getItem(position);//******But this method is inside Adapter
((TextView) view
.findViewById(R.id.myclassListFragment_ListViewItem1))
.setText(myClassItem.firstItem);
((TextView) view
.findViewById(R.id.myclassListFragment_ListViewItem2))
.setText(myClassItem.secondItem);
((TextView) view
.findViewById(R.id.myclassListFragment_ListViewItem3))
.setText(myClassItem.thirdItem);
return view;
}
But when the user clicks an item from the ListView
, the fourthItem
field for the object whose other fields are displayed as that clicked listview item, should be displayed (in a TextView
) in a separate Fragment
(detailsFragment
).
The program adds the new fragment on the click of an item from the ListView
as follows, But the question is that how do I get the value of fourthItem
field for the MyClass
object whose corresponding ListView
item had been clicked by the user, so that I can set it in a TextView
in the detailsFragment
?
In the mentioned first ListFragment
:
The ListFragment
implements LoaderManager.LoaderCallBacks<List<MyClass>>
, which means that the List
containing the MyClass
objects is passed to the LoaderManager.LoaderCallBacks
's onLoadFinished()
method:
@Override
public void onLoadFinished(Loader<List<MyClass>> loader, List<MyClass> dataList) {
customArrayListAdapter.setData(dataList);
if (isResumed()) {setListShown(true);} else {setListShownNoAnimation(true);}
}
We have onListItemClick()
method, which instantiates the new detailsFragment
to display fourthItem
in it:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetailsFragment(position);
}
private void showDetailsFragment(int index) {
listViewItemPosition = index;
DetailsFragment detailsFragment = DetailsFragment.newInstance(index);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.myActivity_frameLayout1, detailsFragment);
fragmentTransaction.commit();
}
Upvotes: 0
Views: 1005
Reputation: 18112
Change your method like this
private void showDetailsFragment(int index) {
String fourthItem = ((MyClass)getListAdapter().getItem(index)).fourthItem;
DetailsFragment detailsFragment = DetailsFragment.newInstance(fourthItem);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.myActivity_frameLayout1, detailsFragment);
fragmentTransaction.commit();
}
After this you will have to change the type of parameter passed in newInstance()
of DetailsFragment
to String
and simply display that.
Upvotes: 1
Reputation: 132992
You can get selected row item object from ListView by calling getItem
method of Adapter using getListAdapter
as:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
MyClass class = (MyClass)getListAdapter().getItem(position);
}
Upvotes: 3
Reputation: 22493
By using setTag()
and getTag()
mechanism to make your views remember something, that could be an object an integer a string or anything you like.
set this in getView()
view.SetTag(myClassItem );
use getTag() in onItemClickListener() to retrieve data from item
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
MyClass class = (MyClass) v.getTag();
}
Upvotes: 1