按下以隐藏键盘时,EditText将重置为先前的值

时间:2017-04-29 17:35:47

标签: java android

我有一个包含多个列表项的ListView,每个列表项包含一个水平方向的TextView和一个EditText。当我向任何EditText输入值并按回以隐藏键盘时,它将重置为其默认值(0)。我希望值保持不变并保存到相应的EditText。我试图使用 setOnKeyListener() onBackPressed()来解决它。以下是我现在的代码。请提出必要的修改建议以解决此问题。

MainActivity.java

package com.example.busnotificationsender;

import ....;

public class MainActivity extends AppCompatActivity {

    private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
    ListView listView;
    ArrayList<Bus> buses;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.busListView);
        final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Buses", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        loadSavedPreferences();

        buses = new ArrayList<Bus>();
        buses.add(new Bus(getResources().getString(R.string.AgnibaanKhajaranaRaniSati), sharedPreferences.getInt("Agnibaan~Khajarana~Rani_Sati", 0)));
        buses.add(new Bus(getResources().getString(R.string.BhavarkuaV), sharedPreferences.getInt("Bhavarkua-V", 0)));
        buses.add(new Bus(getResources().getString(R.string.RaniSatiAgnibaanKhajranaRaniSati), sharedPreferences.getInt("Rani_Sati~Agnibaan~Khajrana~Rani_sati", 0)));
        buses.add(new Bus(getResources().getString(R.string.SaiMandirVijayNagar), sharedPreferences.getInt("Sai_Mandir~VijayNagar", 0)));
        ...Other additions to the DS

        final BusAdapter busAdapter = new BusAdapter(this, buses);

        // Get a reference to the ListView, and attach the adapter to the listView.
        listView.setAdapter(busAdapter);

        EditText editText = (EditText) listView.findViewById(R.id.busNumber);
        editText.setOnKeyListener();

        //sends RealtimeDatabase write operation to Firebase database and updates the EditTexts and shows a toast
        Button submitButton = (Button) findViewById(R.id.submit_button);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseReference bdRef = mDatabase.child("Buses");
                for (int i = 0; i < buses.size(); i++) {
                    EditText editText = (EditText) getViewByPosition(i, listView).findViewById(R.id.busNumber);
                    TextView textView = (TextView) getViewByPosition(i, listView).findViewById(R.id.busDestinations);
                    String busName = bdRef.child(textView.getText().toString()).getKey().toString();
                    if (busName.equals(buses.get(i).getDestinations())){
                        bdRef.child(buses.get(i).getDestinations()).setValue(Integer.valueOf(editText.getText().toString()));
                        buses.get(i).setBusNumber(Integer.valueOf(editText.getText().toString()));
                        busAdapter.notifyDataSetChanged();
                    }
                }
                Toast.makeText(getApplicationContext(), "Notifications Sent", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadSavedPreferences() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        for (int i = 0; i < listView.getCount(); i++){
            EditText editText = (EditText) listView.getChildAt(i).findViewById(R.id.busNumber);
            editText.setText(sharedPreferences.getInt(buses.get(i).getDestinations(), buses.get(i).getBusNumber()));
        }
    }

    private void savePreferences(String destinations, int number){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(destinations, number);
        editor.commit();
    }

    public void saveData(){
        for (int i = 0; i < buses.size(); i++){
            savePreferences(buses.get(i).getDestinations(), buses.get(i).getBusNumber());
        }
    }

    @Override
    public void onBackPressed(){
        saveData();
        super.onBackPressed();
    }

    //    method to get view from the listView
//    @param position, listView
    public View getViewByPosition(int position, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (position < firstListItemPosition || position > lastListItemPosition) {
            return listView.getAdapter().getView(position, listView.getChildAt(position), listView);
        } else {
            final int childIndex = position - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }
}

BusAdapter.java

package com.example.busnotificationsender;

import ...;

public class BusAdapter extends ArrayAdapter<Bus> {

    private HashMap<String, Integer> textValues = new HashMap<String, Integer>();

    public BusAdapter(Activity context, ArrayList<Bus> buses){
        super(context, 0, buses);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View busListItemView = convertView;
        if(busListItemView == null) {
            busListItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        //        EditText to enter the Bus number
        Bus currentBus = getItem(position);
        EditText editText = (EditText) busListItemView.findViewById(R.id.busNumber);
        editText.setText(String.valueOf(currentBus.getBusNumber()));

        Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/KGSecondChancesSolid.ttf");
        editText.setTypeface(typeFace);

        //        TextView for the destinations of the corresponding bus-number
        final TextView destinationsTextView = (TextView) busListItemView.findViewById(R.id.busDestinations);
        destinationsTextView.setText(currentBus.getDestinations());
        destinationsTextView.setTypeface(typeFace);
        return busListItemView;
    }
}

Bus.java

这是一个简单的java文件,用于获取和设置总线编号到特定总线。

0 个答案:

没有答案