Here’s my my_lifts_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/white_linen">
<LinearLayout android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button android:id="@+id/bookButton" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/bookRide"
android:onClick="bookRide"/>
</LinearLayout>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="@id/bottom_control_bar"
android:divider="#b5b5b5"
android:dividerHeight="3dp"
android:listSelector="@drawable/custom_list_selector"/>
<TextView
android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have not joined any rides."/>
</RelativeLayout>
Here’s my fragment activity:
public class LiftActivity extends FragmentActivity {
private static final String TAG = LiftActivity.class.getName();
private ArrayList<Lift> mLifts = new ArrayList<Lift>();
private LiftArrayAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_lift_list);
mAdapter = new LiftArrayAdapter(this, R.layout.ride_item_card, mLifts);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LiftListFragment list = new LiftListFragment();
ft.add(android.R.id.content, list);
list.setListAdapter(mAdapter);
MyLiftsResponderFragment responder = (MyLiftsResponderFragment) fm.findFragmentByTag("RESTResponder");
if (responder == null) {
responder = new MyLiftsResponderFragment();
ft.add(responder, "RESTResponder");
}
ft.commit();
}
public void bookRide(View v){
Intent intent = new Intent(this, LiftSearchActivity.class);
startActivity(intent);
}
public LiftArrayAdapter getArrayAdapter() {
return mAdapter;
}
}
my liftlistfragment :
public class LiftListFragment extends ListFragment {
private static final String TAG = LiftListFragment.class.getName();
private static final int ACTIVITY_VIEW_LIFT = 0;
@Override
public void onListItemClick(ListView list, View v, int position, long id) {
Lift item = (Lift) getListAdapter().getItem(position);
Intent intent = new Intent(this.getActivity(), LiftDetailActivity.class);
intent.putExtra("lift", item.getId());
intent.putExtra("time", item.getTime());
intent.putExtra("price", item.getPrice());
startActivity(intent);
}
}
And finally, my lift list adapter:
public class LiftArrayAdapter extends ArrayAdapter<Lift> {
private final ArrayList<Lift> lifts;
public LiftArrayAdapter(Context context, int textViewResourceId, ArrayList<Lift> results) {
super(context, textViewResourceId, results);
this.lifts = results;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.ride_item_card, null);
}
Lift i = lifts.get(position);
if (i != null) {
ImageView imageView = (ImageView) v.findViewById(R.id.list_image);
TextView departureDate = (TextView) v.findViewById(R.id.departureDate);
TextView driverName = (TextView) v.findViewById(R.id.driverName);
TextView placesRemaining = (TextView) v.findViewById(R.id.placesRemaining);
TextView ridePrice = (TextView) v.findViewById(R.id.ridePrice);
imageView.setTag(i.getDriver().getImage());
ImageLoader image = new ImageLoader();
image.execute(imageView);
if (departureDate != null){
departureDate.setText(i.getFrom().getDescription() + " vers " + i.getTo().getDescription());
}
if (driverName != null){
driverName.setText(i.getTime());
}
if (placesRemaining != null){
placesRemaining.setText("Places remaining : " + Integer.toString(i.getCapacity() - i.getPassengers().size()));
}
if (ridePrice != null){
ridePrice.setText("$" + Float.toString(i.getPrice()));
}
}
// the view must be returned to our activity
return v;
}
}
So basically, I have my list of items (which can be clicked) and my button at the bottom that is unclickable.
I don’t understand why I can’t get this button working. If I use the keyboard I can select it. I presume it has to be some fragment issues the onclicklistener usage between my listfragment and my activityfragment (so, probably my button act as an item). is there a better way to implement this?
You set the layout file for your activity but then you add your
LiftListFragmentfragment directly to theFragmentActivitiy‘s contentFramelayout(which has the idandroid.R.id.content) which will cover the entire previously set layout. Are you sure you don’t want theLiftListFragmentadded in themy_lifts_list.xmllayout file like this?:Then in your code: