Where to write code?

Use a fragment for output of the listview, clicking on a listview will cause the other fragment data (text). So you need to connect the arrayadapter and listview addlistener to item. Question, which section of code it is best to write all this?

MainActivity
public class MainActivity extends AppCompatActivity{

 FragmentManager fragmentManager;
 FragmentTransaction fragmentTransaction;
 FragmentList fragmentList;

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

 fragmentManager = getSupportFragmentManager();
 fragmentTransaction = fragmentManager.beginTransaction();

 fragmentList = new FragmentList();

 fragmentTransaction.add(R. id.container, fragmentList);

fragmentTransaction.commit();

 }


the MainActivity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="parent" android:layout_height="parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.anton.auto.Activity.MainActivity" tools:showin="@layout/app_bar_main">

 <linearlayout android:id="@+id/container" android:layout_width="parent" android:layout_height="parent" android:layout_marginbottom="0dp" android:layout_marginleft="0dp" android:layout_marginright="0dp" android:layout_margintop="0dp" android:orientation="vertical" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent">
</linearlayout></android.support.constraint.constraintlayout>


FragmentList
public class FragmentList extends Fragment{

 ListView listView;

 ArrayAdapter<string> arrayAdapter;

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 return inflater.inflate(R. layout.list_fragment, null, false);
}

@Override
 public void onAttach(Context Context) {
super.onAttach(context);
}


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

@Override
 public void onStart() {
super.onStart();

 listView = (ListView) getActivity().findViewById(R. id.listView);
 String[] array = getResources().getStringArray(R. array.basis);
 arrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1,
array);

listView.setAdapter(arrayAdapter);

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
 public void onItemClick(AdapterView<?> parent, View View, int position, long id) {
 switch (position){
 case 0:
 //call the second fragment with webview
break;
 case 1:
 //call the second fragment with webview
break;
}
}
});
}

@Override
 public void onResume() {
super.onResume();
}

@Override
 public void onPause() {
super.onPause();
}

@Override
 public void onStop() {
super.onStop();
}

@Override
 public void onDestroyView() {
super.onDestroyView();
}

@Override
 public void onDestroy() {
super.onDestroy();
}

@Override
 public void onDetach() {
super.onDetach();
}
}</string>


layout FragmentList
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="parent" android:layout_height="parent">

 <listview android:id="@+id/listView" android:layout_width="parent" android:layout_height="parent" android:scrollbars="vertical">
</listview></linearlayout>
June 14th 19 at 18:08
0 answer

Find more questions by tags Android