我正在做一个项目,我必须显示一个系的可用教授列表。我不想显示所有教授,而是让学生按部门搜索教授。结果,学生将登录,根据姓名、部门、课程 ID 或全部搜索教授。我正在使用自定义列表适配器。我注意到适配器对象不为空,但列表仍显示“没有可显示的记录。我认为问题与我在 Prof.Detail 类中设置适配器的方式有关。我在这里做错了什么?请参阅下面的代码:
教授类包com.mb.professor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.mb.professor.asynctasks.ProfessorAsyncTask;
/**
* Created by Gagouche on 7/25/13.
*/
public class Professors extends FragmentActivity {
private Button bSearch;
private EditText etSearchBy;
private EditText etCourseId;
private Spinner spSearchDecision;
private String searchValue = null;
private Professor[] _professor = null;
public Professors()
{
}
public Professors(Professor[] professor)
{
this._professor = professor;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.professor);
InitialVariables();
}
private void InitialVariables()
{
bSearch = (Button) this.findViewById(R.id.bSearch);
etSearchBy = (EditText) this.findViewById(R.id.etSearchByDepartment);
etCourseId = (EditText) this.findViewById(R.id.etSearchByCouseId);
spSearchDecision = (Spinner) this.findViewById(R.id.spOption);
bSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(searchValue == "Department")
{
ProfessorAsyncTask professor = new ProfessorAsyncTask();
professor.execute("");
FragmentManager mFragment = getSupportFragmentManager();
FragmentTransaction ft = mFragment.beginTransaction();
ProfessorDetail pDetail = new ProfessorDetail();
pDetail.setProfessorAdapter(_professor);
ft.replace(R.id.fprofessorDetail, pDetail);
ft.addToBackStack(null);
ft.commit();
}
}
});
spSearchDecision.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
searchValue = "By All";
break;
case 1:
searchValue = "Department";
break;
case 2:
searchValue = "CourseId";
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
searchValue = "By All";
}
});
}
}
教授详细课
package com.mb.professor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.mb.professor.adapter.*;
/**
* Created by Gagouche on 7/25/13.
*/
public class ProfessorDetail extends ListFragment {
private ProfessorAdapter adapter = null;
Professor[] prof = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setProfessorAdapter(Professor[] professor)
{
prof = professor;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String [] emptyAdapter = {};
if(prof == null)
{
ArrayAdapter<String> emptyListAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,emptyAdapter);
setListAdapter(emptyListAdapter);
} else
{
adapter = new ProfessorAdapter(getActivity(), R.layout.professor_row, prof);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.customizelayout, container, false);
}
}
课程详情类
package com.mb.professor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.mb.professor.adapter.CourseAdapter;
import java.util.List;
/**
* Created by Gagouche on 7/25/13.
*/
public class CourseDetail extends ListFragment {
private CourseAdapter adapter = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setCourseData(Course[] course)
{
adapter = new CourseAdapter(getActivity(), R.layout.course_row, course);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] course = {};
if(adapter == null)
{
ArrayAdapter<String> emptyAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1 , course );
setListAdapter(adapter);
} else
{
setListAdapter(adapter);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.customizelayout, container, false);
}
}
登录课程
package com.mb.professor;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Login extends Activity implements View.OnClickListener {
Button btnLogin;
EditText etUsername, etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
initializeVariables();
}
private void initializeVariables()
{
btnLogin = (Button) this.findViewById(R.id.btnLogin);
etUsername = (EditText) this.findViewById(R.id.etUserName);
etPassword = (EditText) this.findViewById(R.id.etPassword);
btnLogin.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
String uName = etUsername.getText().toString();
String pWord = etPassword.getText().toString();
if(uName.equals("college") && pWord.equals("florida"))
{
Intent intent = new Intent(this, Professors.class);
startActivity(intent);
}
}
}
AsyncTask 教授
package com.mb.professor.asynctasks;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import com.mb.professor.Course;
import com.mb.professor.Professor;
import com.mb.professor.ProfessorDetail;
import com.mb.professor.Professors;
import com.mb.professor.R;
/**
* Created by Gagouche on 8/1/13.
*/
public class ProfessorAsyncTask extends AsyncTask<String,Void,Professor[]> {
private FragmentActivity fActivity;
public ProfessorAsyncTask()
{
}
@Override
protected Professor[] doInBackground(String... params) {
Professor[] professor = new Professor[5];
professor[0] = new Professor("Finance", "John", "123", "Accounting");
professor[1] = new Professor("Accounting", "Cain", "124", "finance");
professor[2] = new Professor("Database", "Eugene", "125", "Music");
professor[3] = new Professor("Finance", "Seikei", "126", "Engineer");
professor[4] = new Professor("Finance", "Bojok", "127", "Math");
return professor;
}
@Override
protected void onPostExecute(Professor[] professors) {
super.onPostExecute(professors);
// ProfessorDetail professorDetailFragment = (ProfessorDetail)fActivity.getSupportFragmentManager().findFragmentById(R.id.fprofessorDetail);
// Professors prof = new Professors(professors);
}
}
课程异步任务
package com.mb.professor.asynctasks;
import android.app.FragmentTransaction;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import com.mb.professor.Course;
import com.mb.professor.CourseDetail;
import com.mb.professor.R;
import java.util.List;
/**
* Created by Gagouche on 8/1/13.
*/
public class CourseAsyncTask extends AsyncTask<String, Void, Course[]> {
private FragmentActivity mActivity;
private Course[] course = null;
public CourseAsyncTask(FragmentActivity activity)
{
this.mActivity = activity;
}
@Override
protected Course[] doInBackground(String... params) {
Course[] course = new Course[5];
course[0] = new Course("Finance", "John", "123");
course[1] = new Course("Accounting", "Cain", "124");
course[2] = new Course("Database", "Eugene", "125");
course[3] = new Course("Finance", "Seikei", "126");
course[4] = new Course("Finance", "Bojok", "127");
return course;
}
@Override
protected void onPostExecute(Course[] courses) {
super.onPostExecute(courses);
CourseDetail courseDetailFragment = (CourseDetail) mActivity.getSupportFragmentManager().findFragmentById(R.id.fCourseDetail);
courseDetailFragment.setCourseData(courses);
}
}
适配器教授
package com.mb.professor.adapter;
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.TextView;
import com.mb.professor.*;
import java.util.List;
/**
* Created by Gagouche on 8/1/13.
*/
public class ProfessorAdapter extends ArrayAdapter<Professor> {
private Context context;
private int layoutResourceId;
private Professor[] data = null;
public ProfessorAdapter(Context context, int layoutResourceId, Professor[] data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProfessorHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId,parent, false);
holder = new ProfessorHolder();
holder.tvFirstName = (TextView) row.findViewById(R.id.tvFirstName);
holder.tvLastName = (TextView) row.findViewById(R.id.tvLastName);
holder.tvCourse = (TextView) row.findViewById(R.id.tvCourseID);
holder.tvDepartment = (TextView) row.findViewById(R.id.tvDepartment);
row.setTag(holder);
} else
{
holder = (ProfessorHolder) row.getTag();
}
Professor item = data[position];
holder.tvFirstName.setText(item.getFirstName());
holder.tvLastName.setText(item.getLastName());
holder.tvDepartment.setText(item.getDepartmentID());
holder.tvCourse.setText(item.getCourseId());
return row;
}
static class ProfessorHolder
{
TextView tvFirstName;
TextView tvLastName;
TextView tvDepartment;
TextView tvCourse;
}
}
课程适配器
package com.mb.professor.adapter;
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.TextView;
import com.mb.professor.Course;
import com.mb.professor.R;
/**
* Created by Gagouche on 8/1/13.
*/
public class CourseAdapter extends ArrayAdapter<Course> {
private Context context;
private int layoutResourceId;
private Course[] data = null;
public CourseAdapter(Context context, int resource, Course[] data) {
super(context, resource, data);
this.context = context;
this.layoutResourceId = resource;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CourseHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CourseHolder();
holder.tvCourseInstructor = (TextView) row.findViewById(R.id.tvCourseInstructor);
holder.tvCourseId = (TextView) row.findViewById(R.id.tvCourse);
holder.tvCourseName = (TextView) row.findViewById(R.id.tvCourseName);
row.setTag(holder);
} else {
holder = (CourseHolder) row.getTag();
}
Course item = data[position];
holder.tvCourseId.setText(item.getCourseId());
holder.tvCourseName.setText(item.getCourseName());
holder.tvCourseInstructor.setText(item.getInstructorName());
return row;
}
static class CourseHolder
{
TextView tvCourseName;
TextView tvCourseInstructor;
TextView tvCourseId;
}
}
登录 XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textSize="30sp"
/>
<EditText
android:layout_width="200sp"
android:layout_height="wrap_content"
android:id="@+id/etUserName"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textSize="30sp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etPassword"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btnLogin"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</LinearLayout>
XML教授
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search For Professor"
android:id="@+id/textView"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="30sp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="search by Department"
android:id="@+id/etSearchByDepartment"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search by Course ID"
android:id="@+id/textView2"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="30sp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/etSearchByCouseId"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<Spinner
android:layout_width="321dp"
android:layout_height="wrap_content"
android:id="@+id/spOption"
android:entries="@array/searchOption"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="@+id/bSearch"
android:text="Search"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="com.mb.professor.ProfessorDetail"
android:id="@+id/fprofessorDetail"
android:layout_weight="1"
android:layout_gravity="center"/>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:name="com.mb.professor.CourseDetail"
android:id="@+id/fCourseDetail"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
教授行自定义 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:layout_marginLeft="10dp"
android:id="@id/tvFirstName"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:layout_marginLeft="10dp"
android:id="@id/tvFirstName"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_marginLeft="10dp"
android:id="@id/tvDepartment"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="New Text"
android:id="@id/tvCourseID"
android:layout_gravity="center_horizontal|top"/>
</LinearLayout>
自定义列表视图的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"/>
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Record to be displayed."
android:textSize="25sp"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
这是一个例外:
08-05 13:26:25.619 716-731/com.android.exchange D/ExchangeService:从电子邮件应用程序收到 deviceId:空 08-05 13:26:25.619 716-731/com.android.exchange D/ExchangeService:! !!deviceId 未知;停止自我并重试 08-05 13:26:27.242 1055-1055/com.mb.professor D/AndroidRuntime: 关闭 VM 08-05 13:26:27.242 1055-1055/com.mb.professor W/dalvikvm: threadid =1:线程以未捕获的异常退出(组=0x40a71930)08-05 13:26:27.302 1055-1055/com.mb.professor E/AndroidRuntime:致命异常:主要 java.lang.NullPointerException:存储 == java 处的 null .util.Arrays$ArrayList.(Arrays.java:38) at java.util.Arrays.asList(Arrays.java:154) at android.widget.ArrayAdapter.(ArrayAdapter.java:128) at com.mb.professor。 com.mb 上的 adapter.ProfessorAdapter.(ProfessorAdapter.java:23)。