在列表视图上的旋转器

在列表视图上的旋转器

本文介绍了在列表视图上的旋转器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在微调器中打开下拉细节。



我尝试过:



< pre> 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;

公共类CourseSearchActivity扩展AppCompatActivity {

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

私人EditText courseTitleEditText;
私人Spinner ok;
private ListView offeringListView;

private OfferingListAdapter offeringListAdapter;


@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(offering.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);

ok.setAdapter(offeringListAdapter);
ArrayAdapter< String> instructorSpinnerAdapter = new ArrayAdapter< String>
(这个,android.R.layout.simple_spinner_item,getAllInstructorNames());
//ok.setAdapter(instructorSpinnerAdapter);
// ok.setOnItemSelectedListener(instructorSpinnerListener);


}
private String [] getAllInstructorNames(){
String [] instructorNames = new String [allCoursesList.size()+ 1];
instructorNames [0] =[选择课程];
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(提供产品:allOfferingsList){
//如果课程标题以用户输入开头,
//将其添加到listAdapter
课程课程= offering.getCourse( );
if(course.getTitle()。toLowerCase()。contains(input))
offeringListAdapter.add(offering);
}
}
}

@Override
public void afterTextChanged(可编辑的可编辑){


}
};
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])){
offeringListAdapter.clear();
for(提供产品:allOfferingsList)
offeringListAdapter.add(offering);
}
else {
offeringListAdapter.clear();

}
}


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

public void reset(查看视图){
courseTitleEditText.setText();
ok.setSelection(0);
}

}
 package edu.orangecoastcollege.cs273.occcoursefinder; 

/ **
*< code>课程< / code> class代表Orange Coast College的一门课程,
*包括其alpha(例如CS),编号(例如A273)和标题(例如移动应用程序开发)
*
* @author Michael Paulding
* /
公共课程{
private int mId;
private String mAlpha;
private String mNumber;
private String mTitle;

public Course(int id,String alpha,String number,String title){
mId = id;
mAlpha = alpha;
mNumber =数字;
mTitle = title;
}

public课程(String alpha,String number,String title){
this(-1,alpha,number,title);
}

public int getId(){
return mId;
}

public String getAlpha(){
return mAlpha;
}

public void setAlpha(String alpha){
mAlpha = alpha;
}

public String getNumber(){
return mNumber;
}

public String getFullName(){
return mAlpha ++ mNumber;
}

public void setNumber(String number){
mNumber = number;
}

public String getTitle(){
return mTitle;
}

public void setTitle(String title){
mTitle = title;
}

@Override
public String toString(){
returnCourse {+
Id =+ mId +
,Alpha ='+ mAlpha +'\''+
,Number ='+ mNumber +'\''+
,Title ='+ mTitle +'\ ''+
'}';
}
}
 package edu.orangecoastcollege.cs273.occcoursefinder; 

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

class DBHelper扩展SQLiteOpenHelper {

private Context mContext;

//任务:定义数据库版本和名称(数据库包含多个表格)
static final String DATABASE_NAME =OCC;
private static final int DATABASE_VERSION = 1;

//任务:定义课程表的字段(列名)
public static final String COURSES_TABLE =Courses;
public static final String COURSES_KEY_FIELD_ID =_ id;
public static final String FIELD_ALPHA =alpha;
public static final String FIELD_NUMBER =number;
public static final String FIELD_TITLE =title;

//任务:为讲师定义领域(栏目名称)


//任务:定义领域表的领域(栏目名称)
private static final String OFFERINGS_TABLE =Offerings;
private static final String OFFERINGS_KEY_FIELD_ID =crn;
private static final String FIELD_SEMESTER_CODE =semester_code;
public static final String FIELD_COURSE_ID =course_id;

public DBHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
mContext = context;
}

@Override
public void onCreate(SQLiteDatabase数据库){
String createQuery =CREATE TABLE+ COURSES_TABLE +(
+ COURSES_KEY_FIELD_ID +INTEGER PRIMARY KEY AUTOINCREMENT,
+ FIELD_ALPHA +TEXT,
+ FIELD_NUMBER +TEXT,
+ FIELD_TITLE +TEXT+);
database.execSQL(createQuery);



createQuery =CREATE TABLE+ OFFERINGS_TABLE +(
+ OFFERINGS_KEY_FIELD_ID +INTEGER PRIMARY KEY AUTOINCREMENT,
+ FIELD_SEMESTER_CODE + INTEGER,
+ FIELD_COURSE_ID +INTEGER,

+FOREIGN KEY(+ FIELD_COURSE_ID +)REFERENCES

+ COURSES_TABLE +(+ COURSES_KEY_FIELD_ID +)+
);
database.execSQL(createQuery);
}

@Override
public void onUpgrade(SQLiteDatabase数据库,
int oldVersion,
int newVersion){
database.execSQL( DROP TABLE IF EXISTS+ COURSES_TABLE;

database.execSQL(DROP TABLE IF EXISTS+ OFFERINGS_TABLE);
onCreate(数据库);
}

// **********课程表操作:ADD,GETALL,EDIT,DELETE

public void addCourse(课程课程) ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(FIELD_ALPHA,course.getAlpha());
values.put(FIELD_NUMBER,course.getNumber());
values.put(FIELD_TITLE,course.getTitle());

db.insert(COURSES_TABLE,null,values);

//关闭数据库连接
db.close();
}

public ArrayList< Course> getAllCourses(){
ArrayList< Course> coursesList = new ArrayList<>();
SQLiteDatabase database = this.getReadableDatabase();
//光标游标= database.rawQuery(queryList,null);
Cursor cursor = database.query(
COURSES_TABLE,
new String [] {COURSES_KEY_FIELD_ID,FIELD_ALPHA,FIELD_NUMBER,FIELD_TITLE},
null,
null,
null,null,null,null);

//收集表中的每一行
if(cursor.moveToFirst()){
do {
course course =
new course(cursor) .getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3));
coursesList.add(课程);
} while(cursor.moveToNext());
}
return coursesList;
}

public void deleteCourse(课程){
SQLiteDatabase db = this.getWritableDatabase();

// DELETE THE TABLE ROW
db.delete(COURSES_TABLE,COURSES_KEY_FIELD_ID +=?,
new String [] {String.valueOf(course.getId()) });
db.close();
}

public void deleteAllCourses(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(COURSES_TABLE,null,null);
db.close();
}

public void updateCourse(课程){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(FIELD_ALPHA,course.getAlpha());
values.put(FIELD_NUMBER,course.getNumber());
values.put(FIELD_TITLE,course.getTitle());

db.update(COURSES_TABLE,values,COURSES_KEY_FIELD_ID +=?,
new String [] {String.valueOf(course.getId())});
db.close();
}

public课程getCourse(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(
COURSES_TABLE,
new String [] {COURSES_KEY_FIELD_ID,FIELD_ALPHA,FIELD_NUMBER,FIELD_TITLE},
COURSES_KEY_FIELD_ID +=?,
new String [] {String.valueOf(id)},
null,null,null,null);

if(cursor!= null)
cursor.moveToFirst();

课程课程=新课程(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3));

db.close();
返回课程;
}




// **********提供表操作:ADD,GETALL,EDIT,DELETE

public void addOffering(int crn,int semesterCode,int courseId){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(OFFERINGS_KEY_FIELD_ID,crn);
values.put(FIELD_SEMESTER_CODE,semesterCode);
values.put(FIELD_COURSE_ID,courseId);


db.insert(OFFERINGS_TABLE,null,values);

//关闭数据库连接
db.close();
}

public ArrayList< Offering> getAllOfferings(){
ArrayList< Offering> offeringList = new ArrayList<>();
SQLiteDatabase database = this.getReadableDatabase();
//光标游标= database.rawQuery(queryList,null);
Cursor cursor = database.query(
OFFERINGS_TABLE,
new String [] {OFFERINGS_KEY_FIELD_ID,FIELD_SEMESTER_CODE,FIELD_COURSE_ID},
null,
null,
null ,null,null,null);

//收集表中的每一行
if(cursor.moveToFirst()){
do {
Course course = getCourse(cursor.getInt(2) );
//讲师讲师= getInstructor(cursor.getInt(3));
提供产品=新产品(cursor.getInt(0),
cursor.getInt(1),course);

offeringList.add(offer);
} while(cursor.moveToNext());
}
return offeringList;
}

public void deleteOffering(提供产品){
SQLiteDatabase db = this.getWritableDatabase();

//删除表行
db.delete(OFFERINGS_TABLE,OFFERINGS_KEY_FIELD_ID +=?,
new String [] {String.valueOf(offering.getCRN()) });
db.close();
}

public void deleteAllOfferings(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(OFFERINGS_TABLE,null,null);
db.close();
}

public void updateOffering(提供产品){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(FIELD_SEMESTER_CODE,offering.getSemesterCode());
values.put(FIELD_COURSE_ID,offering.getCourse()。getId());


db.update(OFFERINGS_TABLE,values,OFFERINGS_KEY_FIELD_ID +=?,
new String [] {String.valueOf(offering.getCRN())});
db.close();
}

公开提供getOffering(int crn){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(
OFFERINGS_TABLE,
new String [] {OFFERINGS_KEY_FIELD_ID,FIELD_SEMESTER_CODE,FIELD_COURSE_ID},
OFFERINGS_KEY_FIELD_ID +=?,
new String [] {String.valueOf(crn)},
null,null,null,null);

if(cursor!= null)
cursor.moveToFirst();

课程课程= getCourse(cursor.getInt(2));
//讲师讲师= getInstructor(cursor.getInt(3));
提供产品=新产品(cursor.getInt(0),
cursor.getInt(1),course);


db.close();
退货;
}

public Cursor getAllLabelsAsCursor(){
String [] columns = new String [] {rowid AS _id,*}; //需要SimpleCursorAdapter的_id列
返回this.getWritableDatabase()。query(COURSES_TABLE,columns,null,null,null,null,null);
}



public boolean importCoursesFromCSV(String csvFileName){
AssetManager manager = mContext.getAssets();
InputStream inStream;
try {
inStream = manager.open(csvFileName);
} catch(IOException e){
e.printStackTrace();
返回false;
}

BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
字符串行;
try {
while((line = buffer.readLine())!= null){
String [] fields = line.split(,);
if(fields.length!= 4){
Log.d(OCC Course Finder,跳过错误的CSV行:+ Arrays.toString(fields));
继续;
}
int id = Integer.parseInt(fields [0] .trim());
String alpha = fields [1] .trim();
String number = fields [2] .trim();
String title = fields [3] .trim();
addCourse(新课程(id,alpha,number,title));
}
} catch(IOException e){
e.printStackTrace();
返回false;
}
返回true;
}



public boolean importOfferingsFromCSV(String csvFileName){
AssetManager am = mContext.getAssets();
InputStream inStream = null;
try {
inStream = am.open(csvFileName);
} catch(IOException e){
e.printStackTrace();
}

BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
字符串行;
try {
while((line = buffer.readLine())!= null){
String [] fields = line.split(,);
if(fields.length!= 4){
Log.d(OCC Course Finder,跳过错误的CSV行:+ Arrays.toString(fields));
继续;
}
int crn = Integer.parseInt(fields [0] .trim());
int semesterCode = Integer.parseInt(fields [1] .trim());
int courseId = Integer.parseInt(fields [2] .trim());

addOffering(crn,semesterCode,courseId);
}
} catch(IOException e){
e.printStackTrace();
返回false;
}
返回true;
}
}
 package edu.orangecoastcollege.cs273.occcoursefinder; 

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;

/ **
* Helper类为< code>提供< / code>提供自定义适配器名单。
* /
public class OfferingListAdapter extends ArrayAdapter< Offering> {

private Context mContext;
private List< Offering> mOfferingsList = new ArrayList<>();
private int mResourceId;

/ **
*创建一个新的< code> OfferingListAdapter< / code>给出mContext,资源ID和产品列表。
*
* @param c正在使用适配器的mContext(通常是一个活动)
* @param rId资源ID(通常是布局文件名)
* @param offering要显示的产品列表
* /
public OfferingListAdapter(Context c,int rId,List< Offering>产品){
super(c,rId,offering);
mContext = c;
mResourceId = rId;
mOfferingsList =产品;
}

/ **
*获取与布局关联的视图。
* @param pos列表中所选产品的位置。
* @param convertView转换后的视图。
* @param parent父级 - ArrayAdapter
* @return包含所有内容的新视图。
* /

@Override
public View getView(int pos,View convertView,ViewGroup parent)
{
final Offering selectedOffering = mOfferingsList.get( POS);
final课程selectedCourse = selectedOffering.getCourse();


LayoutInflater inflater =(LayoutInflater)mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
查看视图= inflater.inflate(mResourceId,null);
// LinearLayout offeringListLinearLayout =(LinearLayout)view.findViewById(R.id.offeringListLinearLayout);
TextView offeringListFullNameTextView =(TextView)view.findViewById(R.id.offeringListFullNameTextView);
Spinner spinner =(Spinner)convertView.findViewById(R.id.ok);
// TextView offeringListTitleTextView =(TextView)view.findViewById(R.id.offeringListTitleTextView);
// TextView offeringListCrnTextView =(TextView)view.findViewById(R.id.offeringListCrnTextView);

// TODO:引用offeringListCRNTextView并相应地设置文本。

// offeringListLinearLayout.setTag(selectedOffering);
offeringListFullNameTextView.setText(selectedCourse.getTitle());

// offeringListCrnTextView.setText(String.valueOf(selectedOffering.getCRN()));

String [] colors = {Red,Green,Blue};




返回视图;
}
}
 package edu.orangecoastcollege.cs273.occcoursefinder; 

/ **
*< code>提供< / code> class代表Orange Coast College的单一课程,
*包括其CRN(课程注册号),学期代码(带年份的数字和
*学期),< code>课程< /代码>它被映射到< code>讲师< / code>教学
*这个课程的提供。
*
* @author Michael Paulding
* /
公共类提供{
private int mCRN;
private int mSemesterCode;
私人课程mCourse;


公开发售(int CRN,int semesterCode,课程){
mCRN = CRN;
mSemesterCode = semesterCode;
mCourse = course;

}

公开发售(int semesterCode,课程){
mSemesterCode = semesterCode;
mCourse = course;
}

public int getCRN(){
return mCRN;
}

public int getSemesterCode(){
return mSemesterCode;
}

public String getSemesterName(){
switch(mSemesterCode)
{
case 201731:
return2017年秋季;
默认值:
返回;
}
}
public void setSemesterCode(int semesterCode){
mSemesterCode = semesterCode;
}

public course getCourse(){
return mCourse;
}

public void setCourse(课程){
mCourse = course;
}


@Override
public String toString(){
returnOffering {+
CRN =+ mCRN +
,SemesterCode =+ mSemesterCode +
,Course =+ mCourse +

'}';
}
}
  04   -   11   14  52  59 . 657   8011  -8011 / edu.orangecoastcollege.cs273.occcoursefinder E / AndroidRuntime:FATAL EXCEPTION :main 
进程:edu.orangecoastcollege.cs273.occcoursefinder,PID: 8011
java.lang.IllegalStateException:找不到方法viewOfferingDetails(查看)在父或祖先上下文 for android:onClick属性在视图 class android.widget.LinearLayout上定义id ' offeringListLinearLayout'
在android.view.View $ DeclaredOnClickListener.resolveMethod(View.java) : 4757
在android。 view.View $ DeclaredOnClickListener.onClick(View.java: 4716
在android.view.View.performClick(View.java: 5637
在android.view.View $ PerformClick.run(View.java: 22429
在android.os.Handler.handleCallback(Handler.java: 751
在android.os.Handler.dispatchMessage(Handler.java: 95
在android.os.Looper.loop(Looper.java: 154
at android.app.ActivityThread.main(ActivityThread.java: 6119
at java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java: 886
at com.android.internal.os.ZygoteInit。 main(ZygoteInit.java: 776
解决方案






这篇关于在列表视图上的旋转器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:15