java - Issues switching between fragments -
i have activity has 3 fragments each pulls collection of information user on each page. have set in way there 2 users in list, third fragment redirect second one, , if there 1 user second fragment redirect upon swipe first one.
below code use switch between fragments:
fragment mfragment = new fragment1(); getfragmentmanager().begintransaction() .replace(r.id.fragment1, mfragment ).commit();
below complete code
public class fragment2 extends fragment { private string currentuserid; private arrayadapter<string> namesarrayadapter; private arraylist<string> names; private listview userslistview; private button logoutbutton; string usergender = parseuser.getcurrentuser().getstring("gender"); string activityname = parseuser.getcurrentuser().getstring("activityname"); number maxdistance = parseuser.getcurrentuser().getnumber("maximum_distance"); string userlookinggender = parseuser.getcurrentuser().getstring("looking_gender"); number minimumage = parseuser.getcurrentuser().getnumber("minimum_age"); number maximumage = parseuser.getcurrentuser().getnumber("maximum_age"); number userage = parseuser.getcurrentuser().getnumber("age"); @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); setconversationslist(); } @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment2_layout, container, false); return view; } private void setconversationslist() { currentuserid = parseuser.getcurrentuser().getobjectid(); names = new arraylist<string>(); // string useractivityselectionname = null; parsequery<parseuser> query = parseuser.getquery(); // query.whereequalto("activityname",useractivityselectionname); query.wherenotequalto("objectid", parseuser.getcurrentuser().getobjectid()); // users gender = currentuser.looking_gender query.whereequalto("gender", userlookinggender); // users looking_gender = currentuser.gender query.setlimit(1); query.setskip(1); // skip first result, giving 2nd recent user query.whereequalto("looking_gender", usergender); query.whereequalto("activityname", activityname); //query.wheregreaterthanorequalto("minimum_age", minimumage).wheregreaterthanorequalto("age", userage); //query.wherelessthanorequalto("maximum_age", maximumage).wherelessthanorequalto("age", userage); // query.wherewithinkilometers("maximum_distance", point, maxdistance) query.findinbackground(new findcallback<parseuser>() { public void done(list<parseuser> userlist, parseexception e) { if (e == null) { (int i=0; i<userlist.size(); i++) { names.add(userlist.get(i).get("name").tostring()); names.add(userlist.get(i).get("headline").tostring()); names.add(userlist.get(i).get("age").tostring()); // names.add(userlist.get(i).getparseobject("profilepicture").; } userslistview = (listview)getactivity().findviewbyid(r.id.userlistview2); namesarrayadapter = new arrayadapter<string>(getactivity().getapplicationcontext(), r.layout.user_list_item, names); userslistview.setadapter(namesarrayadapter); userslistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> a, view v, int i, long l) { openconversation(names, i); } }); } else { fragment mfragment = new fragment1(); getfragmentmanager().begintransaction() .replace(r.id.fragment1, mfragment ).commit(); } } }); } public void openconversation(arraylist<string> names, int pos) { parsequery<parseuser> query = parseuser.getquery(); query.whereequalto("name", names.get(pos)); query.findinbackground(new findcallback<parseuser>() { public void done(list<parseuser> user, parseexception e) { if (e == null) { intent intent = new intent(getactivity().getapplicationcontext(), messagingactivity.class); intent.putextra("recipient_id", user.get(0).getobjectid()); startactivity(intent); } else { toast.maketext(getactivity().getapplicationcontext(), "error finding user", toast.length_short).show(); } } }); } }
below layout first fragment being refered to
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bac_blue" android:orientation="vertical" > <listview android:id="@+id/userlistname" android:layout_width="220dp" android:layout_centerhorizontal="true" android:layout_height="50dp" android:divider="@null" > </listview> <framelayout android:id="@+id/fragment1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <listview android:id="@+id/userlistheadline" android:layout_width="220dp" android:layout_below="@+id/userlistname" android:layout_centerhorizontal="true" android:layout_height="50dp" android:divider="@null" > </listview> <imageview android:id="@+id/profilepictureresult" android:layout_width="132dp" android:layout_height="120dp" android:layout_below="@+id/userlistheadline" android:layout_centerhorizontal="true" android:layout_margintop="7dp" android:layout_marginbottom="9dp" android:padding="3dp" android:scaletype="centercrop" android:croptopadding="true" android:background="@drawable/border_image" android:alpha="1" /> <listview android:id="@+id/userlistage" android:layout_width="220dp" android:layout_below="@+id/profilepictureresult" android:layout_centerhorizontal="true" android:layout_height="50dp" android:divider="@null" > </listview> <listview android:id="@+id/userlistactivityname" android:layout_width="220dp" android:layout_below="@+id/profilepictureresult" android:layout_centerhorizontal="true" android:layout_height="50dp" android:divider="@null" > </listview> <button android:id="@+id/button1" android:layout_below="@+id/userlistactivityname" android:layout_centerhorizontal="true" android:layout_width="100dp" android:layout_height="50dp" android:layout_margintop="12dp" android:alpha="0.7" android:textcolor="#000000" android:background="#add8e6" android:textsize="22sp" android:typeface="serif" android:text="confirm" /> <imageview android:id="@+id/imageview2" android:layout_width="70dp" android:layout_height="50dp" android:layout_alignparentright="true" android:layout_aligntop="@+id/imageview1" android:alpha="0.7" android:src="@drawable/left_right" /> <imageview android:id="@+id/imageview1" android:layout_width="70dp" android:layout_height="50dp" android:layout_alignbottom="@+id/userlistview" android:layout_alignparentleft="true" android:layout_marginbottom="119dp" android:alpha="0.7" android:src="@drawable/left_arrow2" /> </relativelayout>
thanks in advance.
Comments
Post a Comment