我正在使用操作栏标签(不建议使用)来测试某些标签功能。我想将表单验证信息从第一个选项卡保存到共享首选项,然后在onTabSelected方法上,在切换到下一个选项卡之前检查值。我目前的问题是检查tablistener类
中共享首选项中保存的值TAB1:
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class clientFragmentTab extends Fragment {
ArrayList<String> saleRecord;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());
EditText currDate, invoiceNo , vehicle;
Spinner clientName, territory;
String spinnerValue;
ShowAlert alert;
View rootView = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.client_layout, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
currDate = (EditText) rootView.findViewById(R.id.editText4);
clientName = (Spinner) rootView.findViewById(R.id.spinner);
invoiceNo = (EditText) rootView.findViewById(R.id.editText3);
vehicle = (EditText) rootView.findViewById(R.id.editText6);
territory = (Spinner) rootView.findViewById(R.id.spinner3);
currDate.setText(formattedDate);
List<String> list = new ArrayList<String>();
list.add("Select Client");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_dropdown_item , list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
clientName.setAdapter(adapter);
clientName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
spinnerValue = clientName.getSelectedItem().toString();
SharedPreferences pref = getActivity().getPreferences(0);
SharedPreferences.Editor edt = pref.edit();
edt.putString("client_valid", spinnerValue);
edt.commit();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
spinnerValue = "Nothing";
SharedPreferences pref = getActivity().getPreferences(0);
SharedPreferences.Editor edt = pref.edit();
edt.putString("client_valid", spinnerValue);
edt.commit();
}
});
}
}
TabListener:
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
public class MyTabListener implements ActionBar.TabListener{
private Fragment fragment;
// The contructor
public MyTabListener(Fragment fragment)
{
this.fragment = fragment;
}
// When a tab is tapped, the FragmentTransaction replaces
// the content of our main layout with the specified fragment;
// that's why we declared an id for the main layout.
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(clientFragmentTab.getActivity());
String id = sp.getString("client_valid", "empty");
if(tab.getPosition() == 0)
{
ft.replace(R.id.content, fragment);
}
else if(tab.getPosition() == 1)
{
ft.replace(R.id.content, fragment);
}
else if(tab.getPosition() == 2)
{
ft.replace(R.id.content, fragment);
}
}
// When a tab is unselected, we have to hide it from the user's view.
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
// Nothing special here. Fragments already did the job.
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}