|
|
@@ -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<String> autoList = stringToArraylist(sharedPref.getString(listKey, "")); |
|
|
|
|
|
|
|
// on the below line we are initializing the adapter for our list view. |
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public String listToString(ArrayList<String> list){ |
|
|
|
String result = ""; |
|
|
|
for (String s : list) { |
|
|
|
result += s + ";"; |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
public ArrayList<String> stringToArraylist(String str){ |
|
|
|
ArrayList<String> list = null; |
|
|
|
String[] stringArray = str.split(";"); |
|
|
|
list = new ArrayList<String>(Arrays.asList(stringArray)); |
|
|
|
return list; |
|
|
|
} |
|
|
|
} |
|
|
|
|