将bundle从活动传递到片段

时间:2015-12-05 11:56:21

标签: java android android-fragments

我对android编程很新,遇到了一个小问题。我有一个活动,允许用户从多选列表视图中选择名称。我可以将它存储在ArrayList中,但是如何将该ArrayList作为要从片段中检索的包传递?感谢您将来的任何答案。

MainActivity.java:

public class MainActivity extends Activity {

ListView myListView;
Button getResult;
ConnectionClass connectionClass;

private ArrayList<String> emp_names_list = new ArrayList<String>();
public ArrayList<Integer> emp_id_list = new ArrayList<Integer>();



MyArrayAdapter myArrayAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    connectionClass = new ConnectionClass();
    emp_names_list.add("LOL");


    //PAGKUHA NG RESULTS SA DB
    try {
        Connection con = connectionClass.CONN();
        if (con == null) {
            Toast.makeText(getApplicationContext(), "CONNECTION FAIL", Toast.LENGTH_LONG).show();
        } else {
            String query = "select * from users WHERE user_type=3";
            PreparedStatement ps = con.prepareStatement(query);
            ResultSet rs = ps.executeQuery();

            ArrayList<String> data1 = new ArrayList<String>();
            while (rs.next()) {
                String fname =rs.getString("user_fname");
                String lname =rs.getString("user_lname");
                String name = String.valueOf(fname)+" "+String.valueOf(lname);
                emp_names_list.add(fname);

            }


            Toast.makeText(getApplicationContext(), "FETCH SUCCESS", Toast.LENGTH_LONG).show();
        }
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(), "FETCH FAIL", Toast.LENGTH_LONG).show();
        Log.e("MYAPP", "exception", ex);

    }

    myListView = (ListView)findViewById(R.id.list);

    //PARA SA LAYOUT
    myArrayAdapter = new MyArrayAdapter(
            this,
            R.layout.row,
            android.R.id.text1,
            emp_names_list
            );

    myListView.setAdapter(myArrayAdapter);
    myListView.setOnItemClickListener(myOnItemClickListener);

    getResult = (Button)findViewById(R.id.getresult);
    getResult.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            String result = "";

            /*
            //getCheckedItemPositions
            List<Integer> resultList = myArrayAdapter.getCheckedItemPositions();
            for(int i = 0; i < resultList.size(); i++){
                result += String.valueOf(resultList.get(i)) + " ";
            }
            */

            //getCheckedItems
            List<String> resultList = myArrayAdapter.getCheckedItems();
            for(int i = 0; i < resultList.size(); i++){
                result += String.valueOf(resultList.get(i)) + "\n";
            }

            myArrayAdapter.getCheckedItemPositions().toString();
            //Toast.makeText(getApplicationContext(),result, Toast.LENGTH_LONG).show();

            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    Toast.makeText(getApplicationContext(), "CONNECTION FAIL", Toast.LENGTH_LONG).show();
                } else {


                    //FOR INSERTION ITO USING ARRAYLIST
                    String samp = "";
                    String names = "";
                    samp = myArrayAdapter.getCheckedItems().toString();
                    List<String> data1 = new ArrayList<String>(Arrays.asList(samp.replace("[","").replace("]","").split(",")));
                    //data1.add(samp);

                    for(String name : data1)
                    {
                        names = name;  


                        String query = "INSERT INTO AUTOINC(PersonName)"+"VALUES('"+names+"')";
                          PreparedStatement preparedStatement =   con.prepareStatement(query);
                          preparedStatement.executeUpdate();

                    }
                    Toast.makeText(getApplicationContext(), "INSERT SUCCESS", Toast.LENGTH_LONG).show();
                }
            } catch (Exception ex) {
                Toast.makeText(getApplicationContext(), "INSERT FAILED", Toast.LENGTH_LONG).show();
                Log.e("MYAPP", "exception", ex);
            }

        }});

}

OnItemClickListener myOnItemClickListener = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        myArrayAdapter.toggleChecked(position);


    }};

private class MyArrayAdapter extends ArrayAdapter<String>{

    private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

    public MyArrayAdapter(Context context, int resource,
            int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);

        for(int i = 0; i < objects.size(); i++){
            myChecked.put(i, false);
        }
    }

    public void toggleChecked(int position){
        if(myChecked.get(position)){
            myChecked.put(position, false);
        }else{
            myChecked.put(position, true);
        }

        notifyDataSetChanged();
    }

    public List<Integer> getCheckedItemPositions(){
        List<Integer> checkedItemPositions = new ArrayList<Integer>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItemPositions).add(i);
            }
        }

        return checkedItemPositions;
    }

    public List<String> getCheckedItems(){
        List<String> checkedItems = new ArrayList<String>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItems).add(emp_names_list.get(i));
            }
        }

        return checkedItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;

        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.row, parent, false);  
        }

        CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.checkedTextView);
        checkedTextView.setText(emp_names_list.get(position));

        Boolean checked = myChecked.get(position);
        if (checked != null) {
            checkedTextView.setChecked(checked);
        }

        return row;
    }

}



}

我尝试过以下示例但返回null:

Bundle bundle=new Bundle();
bundle.putBundle("bundle_DescriptioneTab",bundle_DescriptioneTab);
bundle.putBundle("bundle_User_Review",bundle_User_Review);

2 个答案:

答案 0 :(得分:1)

首先,您必须将您的类声明为Serializable

public class MyClass implements Serialisable{
}
and using 

Bundle bundle = new Bundle();
bundle.putSerialisable("myclass",MyClass); 

仅发送类

的数据

如果您想发送Arraylisyt,请使用:

public class MyClass implements Parcelable{

}

 Intent intent = new Intent(this,SecondaryActivity.class);

ArrayList<MyClass> mArrayList = new ArrayList<MyClass>();

并使用

intent.putParcelableArrayListExtra("key", mArrayList);

答案 1 :(得分:0)

您可以将其存储在对象中:

public class Thing implements Serializable {

    private ArrayList<String> emp_names_list = new ArrayList<String>();
    public ArrayList<Integer> emp_id_list = new ArrayList<Integer>();

    [...]
}

然后传递它:

bundle.putBundle("thing",object_thing);