From ce8d424f95cc5a8a1919ba50bf37572abba1f891 Mon Sep 17 00:00:00 2001 From: yasarba71520 Date: Tue, 31 Jan 2023 23:12:05 +0100 Subject: [PATCH] App: Abzweigungsliste erstellt --- .../ui/dashboard/DashboardFragment.java | 113 +++++++++++++++++- .../main/res/layout/fragment_automatik.xml | 52 +++++--- 2 files changed, 149 insertions(+), 16 deletions(-) diff --git a/MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/dashboard/DashboardFragment.java b/MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/dashboard/DashboardFragment.java index 330b508..397de41 100644 --- a/MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/dashboard/DashboardFragment.java +++ b/MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/dashboard/DashboardFragment.java @@ -1,21 +1,36 @@ package com.example.lfrmobileapp.ui.dashboard; +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import com.example.lfrmobileapp.R; import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; + +import kotlin.collections.UArraySortingKt; + public class DashboardFragment extends Fragment { private FragmentAutomatikBinding binding; + String listKey = "listkey"; + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { DashboardViewModel dashboardViewModel = @@ -26,6 +41,86 @@ public class DashboardFragment extends Fragment { final TextView textView = binding.textDashboard; dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); + + SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPref.edit(); + + + + ArrayList autoList = stringToArraylist(sharedPref.getString(listKey, "")); + + // on the below line we are initializing the adapter for our list view. + ArrayAdapter adapter = new ArrayAdapter(root.getContext(), android.R.layout.simple_list_item_1, autoList){ + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View view =super.getView(position, convertView, parent); + + TextView textView=(TextView) view.findViewById(android.R.id.text1); + + /*YOUR CHOICE OF COLOR*/ + textView.setTextColor(Color.BLACK); + + return view; + } + + @Override + public void notifyDataSetChanged() { + super.notifyDataSetChanged(); + editor.putString(listKey, listToString(autoList)); + editor.apply(); + } + }; + + // on below line we are setting adapter for our list view. + binding.automaticList.setAdapter(adapter); + + // on below line we are adding click listener for our button. + binding.addLeft.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // on below line we are getting text from edit text + String item = Integer.toString(autoList.size() + 1) + ": Links"; + + // on below line we are checking if item is not empty + if (!item.isEmpty()) { + // on below line we are adding item to our list. + autoList.add(item); + // on below line we are notifying adapter + // that data in list is updated to + // update our list view. + adapter.notifyDataSetChanged(); + } + + } + }); + + // on below line we are adding click listener for our button. + binding.addRight.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // on below line we are getting text from edit text + String item = Integer.toString(autoList.size() + 1) + ": Rechts"; + + // on below line we are checking if item is not empty + if (!item.isEmpty()) { + // on below line we are adding item to our list. + autoList.add(item); + // on below line we are notifying adapter + // that data in list is updated to + // update our list view. + adapter.notifyDataSetChanged(); + } + } + }); + + binding.automaticList.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + autoList.remove(position); + adapter.notifyDataSetChanged(); + } + }); + return root; } @@ -34,4 +129,20 @@ public class DashboardFragment extends Fragment { super.onDestroyView(); binding = null; } -} \ No newline at end of file + + public String listToString(ArrayList list){ + String result = ""; + for (String s : list) { + result += s + ";"; + } + return result; + } + + public ArrayList stringToArraylist(String str){ + ArrayList list = null; + String[] stringArray = str.split(";"); + list = new ArrayList(Arrays.asList(stringArray)); + return list; + } +} + diff --git a/MobileApp/app/src/main/res/layout/fragment_automatik.xml b/MobileApp/app/src/main/res/layout/fragment_automatik.xml index 00ed1e9..3eeea6b 100644 --- a/MobileApp/app/src/main/res/layout/fragment_automatik.xml +++ b/MobileApp/app/src/main/res/layout/fragment_automatik.xml @@ -11,15 +11,16 @@ android:layout_width="224dp" android:layout_height="38dp" android:layout_marginStart="8dp" - android:layout_marginTop="4dp" + android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:checked="false" android:fontFamily="sans-serif-medium" android:text="Roboter losfahren " android:textSize="16sp" app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.147" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent"> + app:layout_constraintTop_toTopOf="parent" /> - + +