Reputation: 1543
Im trying to pass some data from a ListFragment to another Fragment. The list shows up fine but when I click on an item in the list, nothing happens, and there is nothing showing up in the log that indicates an error. I'm new to android/java development and trying to wrap my ahead around the different concepts (activities, fragments, bundle, etc). Coming from the iOS/objective C world I see similarities in the concept involved in passing data between 'views' but I guess I'm not fully understanding the android concept.
Anyway, the fragments and layouts are below. Can someone help me to see where I am going wrong? Much appreciated.
Layout for main (fragment_ehschedule.xml) and list view (schedule_info.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText android:id="@+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/some_hint">
<requestFocus />
</EditText>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/frameLayout"
android:padding="6dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewSessionTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:text="sessiontime" />
<TextView
android:id="@+id/textViewScheduleDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="scheduledate"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewSessionName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="sessionname"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</FrameLayout>
Layout for the fragment detail (fragment_ehschedule_detail.xml) is below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView android:id="@+id/sessionname_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<TextView android:id="@+id/scheduledate_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"/>
<TextView android:id="@+id/sessiontime_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
</LinearLayout>
Class file for main view (EhallSchedFragment2.java) is below:
public class EhallSchedFragment2 extends ListFragment{
public static String strDate = null;
private SQLiteDB sqlite_obj;
private SimpleCursorAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View v = inflater.inflate(R.layout.fragment_ehschedule, container, false);
sqlite_obj = new SQLiteDB(getActivity());
sqlite_obj.open();
Cursor cursor = sqlite_obj.fetchAllSchedules();
// The desired columns to be bound
String[] columns = new String[] {
SQLiteDB.KEY_SCHEDULEDATE,
SQLiteDB.KEY_SESSIONNAME,
SQLiteDB.KEY_SESSIONTIME,
SQLiteDB.KEY_DESC
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.textViewScheduleDate,
R.id.textViewSessionName,
R.id.textViewSessionTime,
R.id.textViewDesc,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
getActivity(), R.layout.schedule_info,
cursor,
columns,
to,
0);
ListView listView = (ListView)v. findViewById(android.R.id.list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String exHallScheduleDate =
cursor.getString(cursor.getColumnIndexOrThrow("scheduleDate"));
String exHallSessionName =
cursor.getString(cursor.getColumnIndexOrThrow("sessionName"));
String exHallSessionTime =
cursor.getString(cursor.getColumnIndexOrThrow("sessionTime"));
EhallSchedDetailFragment2 myDetailFragment = new EhallSchedDetailFragment2();
Bundle bundle = new Bundle();
bundle.putString("scheduleDate", exHallScheduleDate);
bundle.putString("sessionName", exHallSessionName);
bundle.putString("sessionTime", exHallSessionTime);
myDetailFragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
myDetailFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frameLayout, myDetailFragment);
transaction.remove(EhallSchedFragment2.this);
/*
* Add this transaction to the back stack.
* This means that the transaction will be remembered after it is
* committed, and will reverse its operation when later popped off
* the stack.
*/
transaction.addToBackStack(null);
transaction.commit();
}
});
EditText myFilter = (EditText)v. findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return sqlite_obj.fetchScheduleByDate(constraint.toString());
}
});
return v;
}
}
Class file for detail view (EhallSchedDetailFragment2.java) is below:
public class EhallSchedDetailFragment2 extends Fragment {
TextView scheduleDateDetail;
TextView sessionNameDetail;
TextView sessionTimeDetail;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ehschedule_detail, null);
scheduleDateDetail = (TextView)view.findViewById(R.id.scheduledate_label);
sessionNameDetail = (TextView)view.findViewById(R.id.sessionname_label);
sessionTimeDetail = (TextView)view.findViewById(R.id.sessiontime_label);
Bundle bundle = getArguments();
if(bundle != null){
String schedDateDet = bundle.getString("scheduleDate");
scheduleDateDetail.setText(schedDateDet);
String sessNameDet = bundle.getString("sessionName");
sessionNameDetail.setText(sessNameDet);
String sessTimeDet = bundle.getString("sessionTime");
sessionTimeDetail.setText(sessTimeDet);
}
return view;
}
}
Upvotes: 1
Views: 121
Reputation: 5626
You basically need four things to get what you need:
As mentioned, you should not communicate between fragments directly. Here is a good step by step tutorial on how to do all the above steps:
Communicating between Fragments and Activities
I hope this helped you solve or getting closer to solving your problem.
Upvotes: 1
Reputation: 741
If you are using a ListFragment, you can do something like this:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView = getListView();
SetClicklListener(mListView);
}
public void SetClicklListener(ListView listView){
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here you can catch the clicked item
}
});
}
Upvotes: 0