gpt4 book ai didi

java - 如何在 fragment 中加载字符串数组?

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:37 24 4
gpt4 key购买 nike

这是主要 Activity 中的数组

String[] items = {
"Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5", "Unit 6", "Unit 7",
"Unit 8", "Unit 9", "Unit10", "Unit 11", "Unit 12", "Unit 13"};
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);

这是我想将该列表加载到 fragment 中的 onclick 方法。

   bt2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragmentB);
fragmentTransaction.addToBackStack("");
fragmentTransaction.commit();

我有很多字符串数组,想为菜单中的每个按钮分配一个。现在我应该把这段代码放在哪里?

ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

最佳答案

ListView 必须在 fragment 内,如果你想通过点击不同的按钮将数据传递给 fragment ,你应该这样做:

已编辑

重要!

该类仅作为示例(不遵循android开发中的最佳实践)。

public class MainActivity extends FragmentActivity implements View.OnClickListener{

public static final String LISTVIEW_DATA = "myData";
public static final String DATA_TYPE1 = "DataType1";
public static final String DATA_TYPE2 = "DataType2";
public static final String DATA_TYPE3 = "DataType3";

Bundle bundle = null;

ArrayList<String> items1 = new ArrayList<String>();
ArrayList<String> items2 = new ArrayList<String>();
ArrayList<String> items3 = new ArrayList<String>();

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

//set some data to the items
items1 = generateStringData("Unit 1");
items2 = generateStringData("Unit 2");
items3 = generateStringData("Unit 3");

Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}

private ArrayList<String> generateStringData(String prefix){
ArrayList<String> temp = new ArrayList<String>();

for (int i = 0; i<10; i++){
temp.add(prefix + i);
}

return temp;
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
bundle = new Bundle();
bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE1);
bundle.putStringArrayList(LISTVIEW_DATA, items1);
break;
case R.id.button2:
bundle = new Bundle();
bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE2);
bundle.putStringArrayList(LISTVIEW_DATA, items2);
break;
case R.id.button3:
bundle = new Bundle();
bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE3);
bundle.putStringArrayList(LISTVIEW_DATA, items3);
break;
default:
break;
}

setListFragment(bundle);
}

private void setListFragment(Bundle arguments){
Fragment fragment = new MainActivityFragment();

if (arguments != null){
fragment.setArguments(arguments);
}

FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack("");
fragmentTransaction.commit();
}

当您点击按钮时,这段代码会将 fragment over fragment over fragmen,您可以通过点击后退按钮看到,这不是个好主意。您应该根据需要重构代码。

为 fragment 设置参数后,您必须接收 fragment 内的数据:

public class MainActivityFragment extends Fragment {

public final static String DATA_TYPE = "DataType";

private ArrayList<String> data = new ArrayList<>();
private String dataTag = "";

public MainActivityFragment() {
}

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

if (getArguments() != null){
data = getArguments().getStringArrayList(MainActivity.LISTVIEW_DATA);
dataTag = getArguments().getString(DATA_TYPE);
}

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

ListView listView = (ListView) rootView.findViewById(R.id.listView1);

ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, data);

listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

switch (dataTag) {
case MainActivity.DATA_TYPE1:
//here is where you can implement your logic for type 1
break;
case MainActivity.DATA_TYPE2:
//here is where you can implement your logic for type 2
break;
case MainActivity.DATA_TYPE3:
//here is where you can implement your logic for type 3
break;
default:
//do something else
break;
}

}
});

return rootView;
}

希望对您有所帮助。

关于java - 如何在 fragment 中加载字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648944/

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