The title of this question is vague because I don't really know what is happening. I hava list view inside a class called SummerJobFragment.java this list view has a onItemClicked() that is suppose to open and other fragment called SummerJobDetailsFragment.java. Below I've posted the code and the logcat screenshot.
SummerJobFragment.java
public class SummerJobsFragmnet extends Fragment {
public SummerJobsFragmnet() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showMessage (String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final MainActivity activity = (MainActivity) getActivity();
String [] places = activity.getAllPositionsNamesPhone().toArray(
new String[activity.getAllPositionsNamesPhone().size()]);
final ListView list = (ListView) getView().findViewById(R.id.joblistView);
int prgmImages=R.mipmap.ic_launcher;
list.setAdapter(new CustomListAdapter(activity,places,prgmImages));
// OnClick listner for the individual cells of the listView
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SummerJobDetailsFragment.mMyAppsBundle.putInt("value", position);
SummerJobDetailsFragment fragment = new SummerJobDetailsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.layout.displayjobs_detailed, fragment).commit();
}
});
}
SummerJobDetailsFragment.java
public class SummerJobDetailsFragment extends Fragment {
DataBaseHelper summerJobDB;
public static Bundle mMyAppsBundle = new Bundle();
public int position = SummerJobDetailsFragment.mMyAppsBundle.getInt("value");
public SummerJobDetailsFragment() {
// Required empty public constructor
}
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.displayjobs_detailed,container,false);
super.onViewCreated(view, savedInstanceState);
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
DataBaseHelper summerJobDB;
summerJobDB = new DataBaseHelper(getActivity());
Cursor res = summerJobDB.getAllData(position+1);
EditText jobPlace = (EditText)view.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
/*
EditText jobPlace = (EditText)summer.getView().findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
EditText jobPosition = (EditText)summer.getView().findViewById(R.id.jobPlace);
jobPlace.setText(res.getString(2));
EditText starTime = (EditText)summer.getView().findViewById(R.id.jobStartingTime);
jobPlace.setText(res.getString(3));
EditText address = (EditText)summer.getView().findViewById(R.id.jobAddress);
jobPlace.setText(res.getString(5));
EditText phone = (EditText)summer.getView().findViewById(R.id.jobPhoneNum);
jobPlace.setText(res.getString(6));
EditText hours = (EditText)summer.getView().findViewById(R.id.jobHours);
jobPlace.setText(res.getString(4));
*/
return
}
}
and here is the displayjobs_detail.xml
<?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:id="#+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/jobplace"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="#+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobStartingTime" />
</LinearLayout>
and here is the logcat:
First of all you are doing the layout operations in onCreate, you should do it in onCreateView() like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
EditText jobPlace = (EditText)rootView.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
...
...
}
Also remove this statement
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
it doesn't make any sense, you call findViewById on the rootView.
No, this is how you do it,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/displayjobs_detailed"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/jobplace"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="#+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobStartingTime" />
</LinearLayout>
</FrameLayout>
Then in your fragment use this to commit
fragmentManager.beginTransaction().replace(R.id.displayjobs_detailed, fragment).commit(); // Note R.id instead of R.layout
Initialize your view items in onCreateView of the Fragment not in onCreate
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
You are calling wrong. Difference between onCreateView and onViewCreated in Fragment
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Instead of, call
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
Related
This is my fragment class:
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
return view;
}
This is adapter:
public class DDayMonthlyListAdapter extends BaseAdapter implements View.OnClickListener {
private Context mContext;
private ArrayList<MonthlyModel> monthlyModels;
ListView listView;
public DDayMonthlyListAdapter(Context context,ArrayList<MonthlyModel> monthlyModels, ListView listView) {
this.mContext = context;
this.monthlyModels = monthlyModels;
this.listView = listView;
}
public int getCount() {
return monthlyModels.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.item_monthly_list, parent, false);
return convertView;
}
Layout fragment_dday_monthly just have a Listview. And here is layout of Listview's item (R.layout.item_monthly_list):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/frequency_choose_container_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dday_activity_divider"
android:layout_alignBottom="#+id/frequency_choose_container_1">
<Spinner
android:id="#+id/spin_chosen_month"
android:layout_width="#dimen/spinner_frequency_width"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/img_chosen_month_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/spin_chosen_month"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frequency_choose_container_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_month"
android:layout_width="#dimen/spinner_frequency_width"
android:layout_height="wrap_content"
android:text="Monthly"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_month_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/txt_chosen_month"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frequency_monthly_picker_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/frequency_choose_container_2"
android:layout_marginTop="10dp"
>
<RadioButton
android:id="#+id/rb_frequency_date_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radiobutton_selector" />
<RelativeLayout
android:id="#+id/date_choose_container_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_date_5"
android:background="#drawable/dday_activity_divider">
<Spinner
android:id="#+id/spin_chosen_date_2"
android:layout_width="#dimen/spinner_chosen_date_width"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/sp14_text_size"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/img_chosen_date_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/spin_chosen_date_2"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/date_choose_container_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_date_5"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_date_1"
android:layout_width="#dimen/spinner_chosen_date_width"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_date_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_date_1"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RadioButton
android:id="#+id/rb_frequency_week_n_month_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/date_choose_container_2"
android:button="#drawable/radiobutton_selector" />
<RelativeLayout
android:id="#+id/daily_choose_container_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_week_n_month_5"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_day_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weekly"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_day_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_day_child_6"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/daily_choose_container_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/daily_choose_container_child_6"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_day_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_day_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_day_child_7"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<ImageView
android:id="#+id/img_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_right_10dp"
android:src="#drawable/add_ic_delete" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img_delete"
android:layout_alignLeft="#+id/img_delete"
android:layout_toLeftOf="#+id/img_add"
android:layout_marginTop="10dp"
android:src="#drawable/add_ic_add" />
</RelativeLayout>
</RelativeLayout>
As you can see I just inflate view to fragment. I dont do any heavy task, but the fragment load so long, it take 1-2 second to be shown. Please help me to fix it!
Instead of inflating , setting adapter in onCreateView do those things in onViewCreated() method
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
return view;
}
}
Like this
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
}
}
There is no need of passing Arraylist To DDayMonthlyListAdapter as this is not necessary in your case
also you should modify the code as below:
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
This must work as the solution!>..
In my app i have to call a fragment from activity. so that i am using Frgament mangaer. While i am running that code it throws the above exception.
this is my main activity
public class UserDashBoardActivity extends DrawerActivity {
private Context context;
private ImageButton searchBtn;
private ImageButton favBtn;
private ImageButton profileBtn;
private ImageButton reminderBtn;
private ImageButton logoutBtn;
private ImageButton notificationBtn;
private ImageView seatchIcon;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onResume();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_user_dash_boad, menu);
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
return true;
case R.id.action_favourite:
return true;
case R.id.action_navigation:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
searchBtn = (ImageButton) findViewById(R.id.search_btn);
favBtn = (ImageButton) findViewById(R.id.fav_btn);
profileBtn = (ImageButton) findViewById(R.id.profile_btn);
reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
final EditText Search = (EditText) findViewById(R.id.emailAddress);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
// Clears History of Activity
regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(regAct);
}
});
favBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//here i am calling the fragment
MyFavouritesFragment fragment = new MyFavouritesFragment();
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
});
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent tabAct = new Intent(getApplicationContext(), AboutCollegeFragment.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
}
}
this is my fragment to be called
public class MyFavouritesFragment extends Fragment {
private FavouriteDelegates favouriteDelegates = new FavouriteDelegates();
private Gson gson = new Gson();
private Context context;
private List<CollegeMO> collegeMOs = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = inflater.inflate(R.layout.my_favourites_list_view, container, false);
return view;
}
private class FavouriteCollege extends BaseAdapter {
LayoutInflater mInflater;
TextView collegeText;
FavouriteCollege() {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return collegeMOs.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
// show list values name and mobile number in contact page
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.my_favourites, null);
collegeText = (TextView) convertView.findViewById(R.id.clg_details);
collegeText.setText(collegeMOs.get(position).getCollegeName());
return convertView;
}
}
}
this is my main activity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/appblue"
android:orientation="vertical">
<ImageButton
android:id="#+id/search_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="150dp"
android:layout_marginTop="70dp"
android:background="#drawable/search_blue"
android:gravity="center" />
<TextView
android:id="#+id/searchCollege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/search_college"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/fav_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-167dp"
android:background="#drawable/fav_blue"
android:gravity="center" />
<TextView
android:id="#+id/myFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="370dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-30dp"
android:text="#string/my_favourites"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/profile_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="150dp"
android:layout_marginTop="30dp"
android:background="#drawable/profile_blue"
android:gravity="center" />
<TextView
android:id="#+id/myProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="105dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/my_profile"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/notification_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-165dp"
android:background="#drawable/notification_blue"
android:gravity="center" />
<TextView
android:id="#+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="390dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/notification"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/reminder_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="55dp"
android:layout_marginRight="200dp"
android:layout_marginTop="20dp"
android:background="#drawable/reminder_blue"
android:gravity="center" />
<TextView
android:id="#+id/reminder"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="110dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/reminder"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/logout_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-220dp"
android:background="#drawable/logout_blue"
android:gravity="center" />
<TextView
android:id="#+id/logout"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="410dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/logout"
android:textColor="#color/green"
android:textSize="20sp" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="1000dp"
android:layout_height="1000dp"
android:layout_gravity="center"
>
</FrameLayout>
</LinearLayout>
this is fragment listview xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<ListView
android:id="#+id/course_detail_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbarStyle="outsideOverlay" />
</LinearLayout>
this is the item of myfavourite listview xml
<?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:background="#color/appblue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="40dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:background="#color/white">
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="#+id/clgImage"
android:src = "#drawable/ic_launcher"
android:layout_weight="1" android:background="#color/white"
android:padding="20dip" android:gravity="center"/>
<TextView
android:id="#+id/clg_details" android:text="Row 2 column 2"
android:layout_weight="1" android:background="#color/white"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
<ImageView
android:id="#+id/downloadImage"
android:src = "#drawable/ic_launcher"
android:layout_weight="1" android:background="#color/white"
android:padding="20dip" android:gravity="center"/>
</TableRow>
</LinearLayout>
</LinearLayout>
you dont have container (R.id.container) in your main activity xml where you should display your fragment into.
like
<framelayout android:id="#+id/container"/> // this snippet is just for idea.
So I know this has been answered multiple times, such as Fragment Inside Fragment. I am using the support library for getChildFragmentManager() as advised in the post. The issue is that my viewpager's fragments are not appearing despite being inflated. My tabs (with icons) are showing perfectly fine. Here is the relevant code:
DialogFragment:
public class DialogFragment extends DialogFragment {
private static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private HotSpot hotSpot;
private Gson gson = new Gson();
private Map nonEmptyPerks;
private TabLayout tabLayout;
private ViewPager viewPager;
public DialogFragment() {
// Empty constructor required for DialogFragment
}
public static PerkDetailDialogFragment newInstance(int page, String hotSpotJson) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString("HOTSPOTDETAILED", hotSpotJson);
PerkDetailDialogFragment fragment = new PerkDetailDialogFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
String json = getArguments().getString("HOTSPOTDETAILED");
hotSpot = gson.fromJson(json, HotSpot.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, parent, false);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.perkSelector);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
nonEmptyPerks = getNonEmptyPerks(hotSpot.getPerks());
PerkFragmentPagerAdapter pagerAdapter =
new PerkFragmentPagerAdapter(getChildFragmentManager(), nonEmptyPerks.size());
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
tabLayout.setupWithViewPager(viewPager);
// ..... More code here
}
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
}
public class PerkFragmentPagerAdapter extends FragmentPagerAdapter {
private int pageCount;
public PerkFragmentPagerAdapter(FragmentManager fm, int pageCount) {
super(fm);
this.pageCount = pageCount;
}
#Override
public int getCount() {
return pageCount;
}
#Override
public Fragment getItem(int position) {
String perk = "temp";
return PerkFragment.newInstance(position + 1, perk);
}
}
}
PerkFragment:
public class PerkFragment extends Fragment {
private static final String ARG_PAGE = "ARG_PAGE";
private static final String ARG_PERK_DESCRIPTION = "PERK_DESCRIPTION";
private int mPage;
private String perkDescription;
public static PerkFragment newInstance(int page, String perk) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString(ARG_PERK_DESCRIPTION, perk);
PerkFragment fragment = new PerkFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
perkDescription = getArguments().getString(ARG_PERK_DESCRIPTION);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_perk, container, false);
TextView perkDescriptionTextView = (TextView) view.findViewById(R.id.perkDescription);
perkDescriptionTextView.setText(perkDescription);
return view;
}
}
fragment_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/hotSpotImg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="#drawable/img_venue_placeholder" />
<ImageView
android:id="#+id/iconBackArrow"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignStart="#+id/hotSpotImg"
android:layout_alignTop="#+id/hotSpotImg"
android:src="#drawable/img_back_arrow_shadow" />
<ImageView
android:id="#+id/iconGPS"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignBottom="#+id/hotSpotImg"
android:layout_alignStart="#+id/hotSpotImg"
android:src="#drawable/img_gps" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/hotSpotImg">
<TextView
android:id="#+id/memberSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="#string/member_space"
android:textAllCaps="true"
android:textColor="#color/grey_2"
android:textSize="16sp" />
<com.test.test.helpers.LetterSpacingTextView
android:id="#+id/hotSpotName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/memberSpace"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="That filler info"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/black"
android:textSize="22sp" />
<android.support.design.widget.TabLayout
android:id="#+id/perkSelector"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/hotSpotName"
android:layout_marginBottom="10dp"
app:tabGravity="center" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/checkIn"
android:layout_below="#id/perkSelector"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="20dp"
android:paddingStart="20dp">
<TextView
android:id="#+id/hotSpotHours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/hours"
android:textColor="#color/grey"
android:textSize="14sp" />
<TextView
android:id="#+id/hotSpotDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/hotSpotHours"
android:layout_marginTop="20dp"
android:lineSpacingExtra="10dp"
android:text="FILLER DESCRIPTION"
android:textAlignment="center"
android:textColor="#color/grey"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/checkIn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/text_view_button"
android:clickable="true"
android:text="#string/check_in"
android:textAlignment="center"
android:textAllCaps="true" />
</RelativeLayout>
</RelativeLayout>
fragment_perk.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/perkDescription"
style="#style/MyCustomTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERK FILLER"
android:textColor="#color/magnises_grey"
android:textSize="18sp" />
The issue is that the viewpager should include tablayout inside the viewpager tags. This fixes the problem.
e.g:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MY.DIALOG"
card_view:tabTextColor="#666666"
card_view:tabSelectedTextColor="#666666" />
</android.support.v4.view.ViewPager>
I was trying to change the color of a text view in a list fragment after onclick button.
Im using an add button(addBtn) to add the contents to list dynamically an another button(clrBtn) to change the color of the textview which is already added to the list
Im getting a nullPointerException. Can you please let me know where am i going wrong
Below is my code :
public class FragmentOne extends ListFragment {
String[] order = new String[] {};
int[] qty;
public FragmentOne(){}
/** Declaring an ArrayAdapter to set items to ListView */
SimpleAdapter adapter1;
// Keys used in Hashmap
String[] from = { "num","itm","price","qty" };
// Ids of views in listview_layout
int[] to = { R.id.num,R.id.itm,R.id.price,R.id.qty};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
Button addBtn,clrBtn;
rootView = inflater.inflate(R.layout.fragment_1, container, false);
addBtn = (Button) rootView.findViewById(R.id.btnAdd);
clrBtn = (Button) rootView.findViewById(R.id.btnClear);
addBtn.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
//ListFragment fragment=new FragmentProdQtyPrice();
Fragment fragment=new FragmentExListView();
Bundle bundle = new Bundle();
bundle.putInt("fragmentId", 1);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
});
clrBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/*MainActivity.aList.clear();
adapter1=new SimpleAdapter(getActivity().getBaseContext(), MainActivity.aList, R.layout.listview_layout, from, to);
setListAdapter(adapter1);*/
LinearLayout tv=(LinearLayout)arg0.findViewById(R.id.listtest);
tv.setBackgroundColor(Color.parseColor("#FF0000"));
}
});
adapter1=new SimpleAdapter(getActivity().getBaseContext(), MainActivity.aList, R.layout.listview_layout, from, to);
setListAdapter(adapter1);
return rootView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
String prompt =
"clicked item: " + getListView().getItemAtPosition(position).toString();
Toast.makeText(getActivity(),prompt , Toast.LENGTH_SHORT).show();
}}
Edited:
My Layout files :
Fragment_1.xml
<?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:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="add" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clr" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/LinearLayout02" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/LinearLayout02"
android:gravity="center_horizontal"
android:text="#string/txtEmpty" />
</RelativeLayout>
listview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/itm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/qty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingTop="10dp" android:textSize="15sp" /></LinearLayout>
Try this Solution:
You are looking child view in Button element. Button element doesn't have any child element.
clrBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LayoutInflater inflater = LayoutInflater.from(FragmentOne.this.getActivity().getApplicationContext());
View root = inflater.inflate(R.layout.listview_layout,null);
LinearLayout tv=(LinearLayout)root.findViewById(R.id.listtest);
tv.setBackgroundColor(Color.parseColor("#FF0000"));
}
});
In my app, I have requirement of Multi Payne layout. In this layout, the first fragment is a ListView which shows the list of items. On click of the list item, a detail view will open up on the right hand side of the list item. But, in my case, when I run my app on tablet, the detail view appears along with ListView by default. While, I want that it should appear on click of the list item.
Below is my code:
Activity Class:
public class OrderActivity extends FragmentActivity implements
OnOrderSelectedListener {
private static final String TAG = "OrderActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
setContentView(R.layout.order_details);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
OrderListFragment orderListFragment = new OrderListFragment();
orderListFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, orderListFragment).commit();
}
}
#Override
public void onOrderSelected(int position) {
Log.d(TAG, "onOrderSelected called");
OrderDetailFragment detailsFrag = (OrderDetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.order_detail_fragment);
if (detailsFrag != null) {
if (!detailsFrag.isVisible()) {
detailsFrag.setUserVisibleHint(true);
detailsFrag.updateOrderView(position);
}
} else {
OrderDetailFragment newFragment = new OrderDetailFragment();
Bundle args = new Bundle();
args.putInt(OrderDetailFragment.ARG_POSITION, position);
newFragment.setArguments(args);;
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
LIst Fragment
public class OrderListFragment extends ListFragment {
private static final String TAG = "OrderListFragment";
OnOrderSelectedListener mOnOrderSelectedListener;
public interface OnOrderSelectedListener {
public void onOrderSelected(int position);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
return inflater.inflate(R.layout.order_list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated called");
ArrayList<Data> mDataList = new ArrayList<Data>();
Data mData1 = new Data("1", "11001", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
Data mData2 = new Data("1", "11002", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
mDataList.add(mData1);
mDataList.add(mData2);
setListAdapter(new OrderAdapter(getActivity(),
R.layout.order_list_item, mDataList));
}
#Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart called");
if (getFragmentManager().findFragmentById(R.id.order_detail_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG, "onAttach called");
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mOnOrderSelectedListener = (OnOrderSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d(TAG, "onListItemClicked");
mOnOrderSelectedListener.onOrderSelected(position);
getListView().setItemChecked(position, true);
}
}
Detail Fragment
public class OrderDetailFragment extends Fragment {
public final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static final String TAG = "OrderDetailFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
return inflater.inflate(R.layout.acceptance_details, container, false);
}
#Override
public void onStart() {
Log.d(TAG, "onStart called");
super.onStart();
Bundle args = getArguments();
if (args != null) {
updateOrderView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
updateOrderView(mCurrentPosition);
}
}
public void updateOrderView(int position) {
Log.d(TAG, "updateOrderView called");
/*
* TextView article = (TextView)
* getActivity().findViewById(R.id.article);
* article.setText(Ipsum.Articles[position]); btnNext = (Button)
* getActivity().findViewById(R.id.btnNext);
* btnNext.setOnClickListener(this); mCurrentPosition = position;
*/
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState called");
super.onSaveInstanceState(outState);
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="horizontal" >
<com.dzo.dispatchcrude.driverapp.ui.HeaderBar
android:id="#+id/headerBar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true" >
</com.dzo.dispatchcrude.driverapp.ui.HeaderBar>
<LinearLayout
android:id="#+id/linOrderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/headerBar"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/order_list_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/order_detail_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderDetailFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:layout_weight="2" />
</LinearLayout>
</RelativeLayout>
List Fragment layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
</LinearLayout>
Order Detail Fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/corner_shape"
android:orientation="vertical" >
<TextView
android:id="#+id/txtHeaderType"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="-5dp"
android:background="#drawable/upper_corner"
android:gravity="center"
android:text="#string/acceptance_details"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtHeaderSource"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/rectangle"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTruck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/truck"
android:textColor="#color/text_color"
android:textSize="20sp"
android:typeface="sans" />
<Spinner
android:id="#+id/truckSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailorSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor2"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailor2Spinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:background="#drawable/input_button"
android:gravity="center"
android:text="#string/accept"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
I did gone through Fragments Basic Demo given on the Android developer's site, but couldn't figure out my mistake.
try this
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setItemChecked(0,false);