使用SessionInfo为不同的用户信息使用相同的片段

时间:2016-01-06 14:18:13

标签: java android android-fragments

我想知道是否有人可以提供帮助,我制作了这个Android Studio项目,其中包含一个MainActivity,4个片段,一个名为TestList的类列表类型,另一个用于保存名为SessionInfo的信息会话的类和一个名为MyCustomArrayAdapter的个性化ArrayAdapter。

首先,我希望程序使用DetailUserFragment来显示用户信息,这个相同的片段可以为每个用户部署不同的信息,具体取决于在列表信息中选择的用户

其次,从列表中选择用户应使用SessionInfo.java中相同的信息扩展相应的用户信息

这是MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startNewFragment(new Fragment1());
}

public void startNewFragment(Fragment fragment) {
    // Initialize fragment transaction
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Replace with fragment content
    ft.replace(R.id.main_frame, fragment);
    // Animation on change
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    // Clear stack (back button memory)
    ft.addToBackStack(null);
    ft.commitAllowingStateLoss();
}

public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}
}

UserListFragment.java

public class UserListFragment extends Fragment {

public ListView lista;
protected SessionInfo session = null;

public UserListFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Indicates fragment has menu
    this.setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.user_list_layout, container, false);
    session = SessionInfo.getInstance();

    lista = (ListView) view.findViewById(R.id.listView_secondFragment);

    // Construct the data source
    ArrayList<TestList> arrayOfUsers = session.getTestList();

    //test user for be beginning
    if (arrayOfUsers.isEmpty()) {
        TestList newUser = new TestList(1, "Prueba", "30", "San José");
        session.getTestList().add(newUser);
    }

    // Create the adapter to convert the array to views
    MyCustomArrayAdapter adapter = new MyCustomArrayAdapter(getActivity(), arrayOfUsers);
    // Attach the adapter to a ListView
    lista.setAdapter(adapter);

    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int itemPosition = position;

            if (itemPosition == 0){
                ((MainActivity)getActivity()).startNewFragment(new DetailUserFragment());
            } else {
                Toast.makeText(getActivity(), "no posee detalle", Toast.LENGTH_LONG).show();
            }
        }
    });

    return view;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == R.id.menu_opt_location) {
        ((MainActivity)getActivity()).startNewFragment(new NewUserFragment());
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // super.onCreateOptionsMenu(menu, inflater);

    inflater.inflate(R.menu.list_items, menu);
}

public void onResume() {
    super.onResume();
    ((MainActivity)getActivity()).setActionBarTitle(getString(R.string.fragment_title_2));
}
}

NewUserFragment.java

public class NewUserFragment extends Fragment {

public TextView text_name;
public TextView text_age;
public TextView text_hometown;
public EditText edit_text_name;
public EditText edit_text_age;
public EditText edit_text_hometown;
public Button fill_button;

public Random random = new Random();
protected SessionInfo session = null;

protected String name;
protected String age;
protected String hometown;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.new_user_layout, container, false);

    session = SessionInfo.getInstance();

    text_name = (TextView) view.findViewById(R.id.text_name);
    text_age = (TextView) view.findViewById(R.id.text_age);
    text_hometown = (TextView) view.findViewById(R.id.text_hometown);

    edit_text_name = (EditText) view.findViewById(R.id.edit_text_name);
    edit_text_age = (EditText) view.findViewById(R.id.edit_text_age);
    edit_text_hometown = (EditText) view.findViewById(R.id.edit_text_hometown);

    fill_button = (Button) view.findViewById(R.id.fill_button);

    fill_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            name = edit_text_name.getText().toString();
            age = edit_text_age.getText().toString();
            hometown = edit_text_hometown.getText().toString();

            if (name.matches("") | age.matches("") | hometown.matches("")) {

                Toast.makeText(getActivity(), "Por favor complete el formulario", Toast.LENGTH_SHORT).show();

            } else {

                //Random number for the icon
                int icon = random.nextInt(5 - 1) + 1;

                // Add item to adapter
                TestList newUser = new TestList(icon, name, age, hometown);
                session.getTestList().add(newUser);

                ((MainActivity) getActivity()).startNewFragment(new UserListFragment());
            }
        }
    });

    return view;
}

public void onResume() {
    super.onResume();
    ((MainActivity)getActivity()).setActionBarTitle(getString(R.string.fragment_title_4));
}
}

DetailUserFragment.java

public class DetailUserFragment extends Fragment {

public ImageView icon_detail_layout = null;
public TextView detail_name = null;
public TextView detail_age = null;
public TextView detail_hometown = null;
public TestList test_list;

protected SessionInfo session = null;

public DetailUserFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.detail_user_layout, container, false);

    session = SessionInfo.getInstance();

    icon_detail_layout = (ImageView) view.findViewById(R.id.icon_detail_layout);
    detail_name = (TextView) view.findViewById(R.id.detail_name);
    detail_age = (TextView) view.findViewById(R.id.detail_age);
    detail_hometown = (TextView) view.findViewById(R.id.detail_hometown);

    return view;
}

private void setPersonData() {

    if (!test_list.getName().equalsIgnoreCase(Constants.EMPTY_STRING)) {
        this.detail_name.setText(getString(R.string.name, this.test_list.getName()));
        this.detail_name.setVisibility(View.VISIBLE);
    }

    if (!test_list.getAge().equalsIgnoreCase(Constants.EMPTY_STRING)) {
        this.detail_age.setText(getString(R.string.age, this.test_list.getAge()));
        this.detail_age.setVisibility(View.VISIBLE);
    }

    if (!test_list.getHometown().equalsIgnoreCase(Constants.EMPTY_STRING)) {
        this.detail_hometown.setText(getString(R.string.hometown, this.test_list.getHometown()));
        this.detail_hometown.setVisibility(View.VISIBLE);
    }

}

public void onResume() {
    super.onResume();
    ((MainActivity)getActivity()).setActionBarTitle(getString(R.string.fragment_title_3));
//        setPersonData();
}
}

SessionInfo.java

public class SessionInfo {

public static final int selected_user = -1;

private SessionInfo() {
    super();
    test_list = new ArrayList<TestList>();
    this.setTestList(null);
}

public static SessionInfo instance = null;
public static SessionInfo getInstance() {
    if (SessionInfo.instance == null) { SessionInfo.instance = new SessionInfo(); }
    return SessionInfo.instance;
}

public static void endInstance() { SessionInfo.instance = null; }

private ArrayList<TestList> test_list;
public ArrayList<TestList> getTestList() { return this.test_list; }
public void setTestList(ArrayList<TestList> list) {this.test_list = (list != null) ? list : new ArrayList<TestList>(); }
}

TestList.java

public class TestList {

public TestList() {
    this.setName(null);
    this.setAge(null);
    this.setHometown(null);
}

public TestList (int imageID, String name, String age, String hometown) {
    super();
    this.setImageID(imageID);
    this.setName(name);
    this.setAge(age);
    this.setHometown(hometown);
}

private int imageID;
public int getImageID() { return imageID; }
public void setImageID(int value) { this.imageID = value; }

private String name;
public String getName() { return name; }
public void setName(String value) { this.name = (value != null) ? value : Constants.EMPTY_STRING; }

private String age;
public String getAge() { return age; }
public void setAge(String value) { this.age = (value != null) ? value : Constants.EMPTY_STRING; }

private String hometown;
public String getHometown() { return hometown; }
public void setHometown(String value) { this.hometown = (value != null) ? value : Constants.EMPTY_STRING; }
}

和MyCustomArrayAdapter.java

public class MyCustomArrayAdapter extends ArrayAdapter<TestList>{

public MyCustomArrayAdapter(Context context, ArrayList<TestList> testLists) {
    super(context, 0, testLists);
}

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout_adapter, parent, false);
    }
    // Get the data item for this position
    TestList testList = getItem(position);

    if (testList != null) {

        // Lookup view for data population
        ImageView iconID = (ImageView) convertView.findViewById(R.id.iconID);
        TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
        TextView tvAge = (TextView) convertView.findViewById(R.id.tvAge);
        TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome);
        // Populate the data into the template view using the data object
        if (iconID != null) {
            iconID.setImageResource(setIcon(testList.getImageID()));
        }
        if (tvName != null) {
            tvName.setText("Nombre: "+testList.getName());
        }
        if (tvAge != null) {
            tvAge.setText("Edad: " + testList.getAge() +" años");
        }
        if (tvHome != null) {
            tvHome.setText("País de Origen: "+testList.getHometown());
        }
    }
    // Return the completed view to render on screen
    return convertView;
}

private int setIcon(int detail_icon) {

    switch (detail_icon) {
        case 1:
            detail_icon = R.drawable.ic_launcher;
            break;
        case 2:
            detail_icon = R.drawable.firefox_noshadow;
            break;
        case 3:
            detail_icon = R.drawable.example_1;
            break;
        case 4:
            detail_icon = R.drawable.example_2;
            break;
        case 5:
            detail_icon = R.drawable.example_3;
            break;
        default:
            detail_icon = R.drawable.ic_launcher;
            break;
    }
    return detail_icon;
}
}

0 个答案:

没有答案