Skip to content

Commit

Permalink
Rename cities by click
Browse files Browse the repository at this point in the history
  • Loading branch information
woheller69 committed Mar 4, 2022
1 parent 0355a32 commit df0a141
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.woheller69.weather.activities;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;

import androidx.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import org.woheller69.weather.R;
Expand Down Expand Up @@ -39,13 +42,14 @@ public class ManageLocationsActivity extends NavigationActivity {
private ItemTouchHelper touchHelper;
RecyclerOverviewListAdapter adapter;
List<CityToWatch> cities;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_locations);
overridePendingTransition(0, 0);

context=this;
database = PFASQLiteHelper.getInstance(getApplicationContext());

//cities = new ArrayList<>();
Expand Down Expand Up @@ -75,7 +79,19 @@ public int compare(CityToWatch o1, CityToWatch o2) {
new RecyclerItemClickListener(getBaseContext(), recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {

AlertDialog.Builder alert = new AlertDialog.Builder(context);
final EditText edittext = new EditText(context);
edittext.setText(adapter.getCityName(position));
edittext.setTextSize(18);
edittext.setGravity(Gravity.CENTER);
alert.setTitle(getString(R.string.edit_location_hint_name));
alert.setView(edittext);

alert.setPositiveButton(getString(R.string.dialog_edit_change_button), (dialog, whichButton) -> adapter.renameCity(position, String.valueOf(edittext.getText())));
alert.setNegativeButton(getString(R.string.dialog_add_close_button), (dialog, whichButton) -> {
});

alert.show();
}

public void onLongItemClick(View view, int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,15 @@ public void onItemMove(int fromPosition, int toPosition) {

}

public String getCityName(int position){
CityToWatch cityToWatch = cities.get(position);
return cityToWatch.getCityName();
}

public void renameCity(int position, String s) {
CityToWatch cityToWatch = cities.get(position);
cityToWatch.setCityName(s);
database.updateCityToWatch(cityToWatch);
notifyItemChanged(position);
}
}

0 comments on commit df0a141

Please sign in to comment.