这是主要活动中的数组
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);
这是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);
答案 0 :(得分:1)
如果要通过单击应该执行的不同按钮将数据传递给片段,listview必须位于片段内:
<强> EDITED 强>
重要!强>
这个类仅作为示例(它不遵循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();
}
当你点击按钮时,这个代码会将片段放在碎片上,正如你可以通过点击后退按钮看到的那样,这不是一个好主意。您应该根据需要重新设置代码。
在设置片段的参数后,您必须接收片段内的数据:
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;
}
希望它有所帮助。
答案 1 :(得分:0)
首先listview
必须是片段布局的一部分,然后您可以在片段的onCreateView()
方法中写这个。
onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstance){
View v = inflater.inflate(R.layout.fragment_layout, container, false);
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(getActivity(), android.R.layout.simple_list_item_1, items);
ListView listView = (ListView) v.findViewById(R.id.listview);
listView.setAdapter(adapter);
return v;
}