单个微调器多个活动

时间:2018-04-15 08:06:46

标签: android sqlite android-layout listview android-spinner

enter image description here我试图使用单个微调器来满足多个要求。我可以通过禁用每个适配器来满足这两个要求。我已经尝试了很多天并用Google搜索但失败了。

要求1. Spinner1根据edittext更改2.我应该能够单击Spinner1并查看项目。

请帮助我提供专业知识。 我的编码。

package edu.orangecoastcollege.cs273.occcoursefinder;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class CourseSearchActivity extends AppCompatActivity {

  private DBHelper db;
  private List < Course > allCoursesList;
  private List < Offering > allOfferingsList;
  private List < Offering > filteredOfferingsList;

  private EditText courseTitleEditText;
  private Spinner ok;
  private ListView offeringsListView;

  private OfferingListAdapter offeringListAdapter;

  private InstructorSpinnerAdapter instructorSpinnerAdapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_course_search);

    deleteDatabase(DBHelper.DATABASE_NAME);
    db = new DBHelper(this);
    db.importCoursesFromCSV("courses.csv");

    db.importOfferingsFromCSV("offerings.csv");

    allOfferingsList = db.getAllOfferings();
    filteredOfferingsList = new ArrayList < > (allOfferingsList);

    allCoursesList = db.getAllCourses();

    courseTitleEditText = (EditText) findViewById(R.id.courseTitleEditText);
    courseTitleEditText.addTextChangedListener(courseTitleTextWatcher);

    ok = (Spinner) findViewById(R.id.ok);


    // offeringListAdapter = new OfferingListAdapter(this, R.layout.offering_list_item, filteredOfferingsList);
    offeringListAdapter = new OfferingListAdapter(this, R.layout.offering_list_item, filteredOfferingsList);
    ok.setAdapter(offeringListAdapter);
    instructorSpinnerAdapter = new InstructorSpinnerAdapter(this, R.layout.offering_list_item, filteredOfferingsList);

    ArrayAdapter < String > instructorSpinnerAdapter = new ArrayAdapter < String >
      (this, android.R.layout.simple_spinner_item, getAllCourse());
    ok.setAdapter(instructorSpinnerAdapter);
    // ok.setOnItemSelectedListener(instructorSpinnerListener);


  }
  private String[] getAllCourse() {
    String[] instructorNames = new String[allCoursesList.size() + 1];
    instructorNames[0] = "[Select Course]";
    for (int i = 1; i < instructorNames.length; i++) {
      instructorNames[i] = allCoursesList.get(i - 1).getTitle();
    }
    return instructorNames;
  }

  public TextWatcher courseTitleTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      String input = charSequence.toString().toLowerCase();
      if (input.equals("")) {

      } else {
        String name = ok.getSelectedItem().toString();
        //Toast.makeText(CourseSearchActivity.this, name, Toast.LENGTH_SHORT).show();
        offeringListAdapter.clear();
        for (Offering offering: allOfferingsList) {
          // If the course title starts with the user input,
          // add it to the listAdapter
          Course course = offering.getCourse();
          if (course.getTitle().toLowerCase().contains(input))
            offeringListAdapter.add(offering);
        }
      }
    }

    @Override
    public void afterTextChanged(Editable editable) {


    }
  };
  public AdapterView.OnItemSelectedListener instructorSpinnerListener = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView << ? > adapterView, View view, int i, long l) {
      String selectedInstructorName = adapterView.getItemAtPosition(i).toString();
      if (selectedInstructorName.equals("[Select Instructor]")) {
        instructorSpinnerAdapter.clear();
        for (Offering offering: allOfferingsList)
          instructorSpinnerAdapter.add(offering);
      } else {
        instructorSpinnerAdapter.clear();

      }
    }


    @Override
    public void onNothingSelected(AdapterView << ? > adapterView) {
      adapterView.setSelection(0);
      Toast.makeText(getApplicationContext(), "Why?", Toast.LENGTH_SHORT).show();
    }
  };

  public void reset(View view) {
    courseTitleEditText.setText("");
    ok.setSelection(0);
  }


}

找到我的要求enter image description here

修改代码

public TextWatcher courseTitleTextWatcher = new TextWatcher() {
  @Override
  public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

  }

  @Override
  public void onTextChanged(CharSequence charSequence, int o, int i1, int i2) {
    String input = charSequence.toString().toLowerCase();
    if (input.equals("")) {

    } else {
      String name = ok.getSelectedItem().toString();
      //Toast.makeText(CourseSearchActivity.this, name, Toast.LENGTH_SHORT).show();
      offeringListAdapter.clear();
      Offering offering;
      for (int i = 0; i < allOfferingsList.size(); i++) {
        // If the course title starts with the user input,
        // add it to the listAdapter
        offering = allOfferingsList.get(i);
        Course course = offering.getCourse();
        if (course.getTitle().toLowerCase().contains(input)) {
          offeringListAdapter.add(offering);
          offeringListAdapter.notifyDataSetChanged();
          ok.setSelection(i);
        }
      }
      offeringListAdapter.notifyDataSetChanged();
      //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
  }

  @Override
  public void afterTextChanged(Editable editable) {


  }
};

错误修改代码

Process: edu.orangecoastcollege.cs273.occcoursefinder, PID: 16548
java.lang.ArrayIndexOutOfBoundsException: length = 16;
index = 21
at java.util.Arrays$ArrayList.get(Arrays.java: 3854)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java: 349)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java: 405)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java: 371)
at android.widget.Spinner.makeView(Spinner.java: 702)
at android.widget.Spinner.layout(Spinner.java: 650)
at android.widget.Spinner.onLayout(Spinner.java: 612)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java: 1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java: 1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java: 1494)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 323)
at android.widget.FrameLayout.onLayout(FrameLayout.java: 261)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java: 433)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 323)
at android.widget.FrameLayout.onLayout(FrameLayout.java: 261)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java: 1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java: 1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java: 1494)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java: 323)
at android.widget.FrameLayout.onLayout(FrameLayout.java: 261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java: 726)
at android.view.View.layout(View.java: 17637)
at android.view.ViewGroup.layout(ViewGroup.java: 5575)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java: 2346)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java: 2068)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java: 1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java: 6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java: 874)
at android.view.Choreographer.doCallbacks(Choreographer.java: 686)
at android.view.Choreographer.doFrame(Choreographer.java: 621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java: 860)
at android.os.Handler.handleCallback(Handler.java: 751)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 154)
at android.app.ActivityThread.main(ActivityThread.java: 6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 776)

1 个答案:

答案 0 :(得分:1)

private ArrayList < String > getAllCourse() {
  ArrayList < String > instructorNames = new ArrayList < > ();
  instructorNames.add("[Select Course]");
  for (int i = 0; i < allCoursesList.size(); i++) {

    instructorNames.add(allCoursesList.get(i).getTitle());
  }
  return instructorNames;
}

public TextWatcher courseTitleTextWatcher = new TextWatcher() {
  @Override
  public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

  }
  @Override
  public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    String input = charSequence.toString().toLowerCase();
    ArrayAdapter adapter = (ArrayAdapter) ok.getAdapter();
    adapter.clear();
    if (input.equals("")) {
      adapter.addAll(getAllCourse());
    } else {
      Course course;
      for (int j = 0; j < allCoursesList.size(); j++) {
        // If the course title starts with the user input,
        // add it to the listAdapter
        course = allCoursesList.get(j);
        if (course.getTitle().toLowerCase().startsWith(input)) {
          adapter.add(course.getTitle());
        }
      }
    }
    adapter.notifyDataSetChanged();
    if (adapter.getCount() != 0) ok.setSelection(0);
  }
  @Override
  public void afterTextChanged(Editable editable) {


  }
};