notifyDataSetChanged()似乎没有工作?

时间:2015-07-26 22:48:25

标签: java android android-arrayadapter

这是我第一次发帖,所以请原谅我,如果我不够具体那么......这是我的delima ..

我试图在单击按钮时动态添加列表。

然而,经过无数次的研究,我提出的解决方案似乎并不适合我。

正如我在上面标题中提到的,方法notifyDataSetChanged()给出了错误'无法解析方法'。任何帮助将不胜感激,这就是我所拥有的。

public class ExercisesSection extends ActionBarActivity {

    private EditText userWorkoutInput;
    private Button addNewWorkoutButton;
    private ListAdapter edsAdapter;

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

        initViews();
        handleIntentData();
        inputTextHandler();

    }
    //This method initializes all the views
    public void initViews(){
        userWorkoutInput = (EditText) findViewById(R.id.userWorkoutInput);
        addNewWorkoutButton = (Button) findViewById(R.id.addNewWorkoutButton);
    }

    //This method makes the "Add new workout Button" clickable or not depending on user input
    public void inputTextHandler(){

        //userWorkoutInput = (EditText) findViewById(R.id.userWorkoutInput);
        //addNewWorkoutButton = (Button) findViewById(R.id.addNewWorkoutButton);

        userWorkoutInput.addTextChangedListener(
                new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        boolean isEmpty = false;
                        if ((userWorkoutInput.getText().toString().trim()).equals("")) {
                            isEmpty = true;
                        }
                        addNewWorkoutButton.setEnabled(!isEmpty);
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                }
        );
    }

    //An onClick method that will add user input into the list
    public void addWorkoutButtonClicked(View view){
        String input = (userWorkoutInput.getText().toString().trim());
        if (!input.equals("")){
            addItemToList(input);
            userWorkoutInput.setText("");   //Empties the edit text section
            Toast.makeText(getBaseContext(), "Item Added", Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(getBaseContext(), "Item Field is Empty", Toast.LENGTH_SHORT).show();
    }

    public void addItemToList(String input){

        String [] exercises = {input};
        edsAdapter = new CustomExerciseAdapter(this, exercises);

        ListView exerciseListView = (ListView) findViewById(R.id.exerciseListView);
        exerciseListView.setAdapter(edsAdapter);
        edsAdapter.notifyDataSetChanged(); //this line is red! ERROR 'cannot resolve method'

        exerciseListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String exercise = String.valueOf(parent.getItemAtPosition(position));
                        //DO SOMETHING WHEN EXERCISE IS CLICKED
                    }
                }
        );

    }

    public void handleIntentData(){

        Bundle workoutData = getIntent().getExtras();
        if (workoutData == null){
            return;
        }

        String workoutClicked = workoutData.getString("exerciseChosen");
        TextView exerciseChosenText = (TextView) findViewById(R.id.exerciseChosenText);
        exerciseChosenText.setText(workoutClicked);
        exerciseChosenText.setTypeface(null, Typeface.BOLD);
    }


public class CustomExerciseAdapter extends ArrayAdapter<String>{

    public CustomExerciseAdapter(Context context, String[] workouts) {
        super(context, R.layout.exercise_custom_row ,workouts);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.exercise_custom_row, parent, false);

        String singleExerciseItem = getItem(position);
        TextView exerciseTV = (TextView) customView.findViewById(R.id.exerciseTV);
        exerciseTV.setText(singleExerciseItem);

        return customView;
    }

0 个答案:

没有答案