gpt4 book ai didi

java - 将包从 Activity 传递到 Fragment

转载 作者:行者123 更新时间:2023-12-01 10:47:10 24 4
gpt4 key购买 nike

我对 Android 编程相当陌生,遇到了一个小问题。我有一个 Activity ,允许用户从多选 ListView 中选择名称。我可以将它存储在 ArrayList 中,但是如何将该 ArrayList 作为包传递以从 fragment 中检索?感谢您将来的任何答复。

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);

最佳答案

第一件事是你必须将你的类声明为可序列化

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);

关于java - 将包从 Activity 传递到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34104616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com