我试图将已安装的应用程序列表显示为片段中的Gridview。
看来代码还可以(也许)。但是当我尝试测试时。
适配器类中的getView()
出现错误。
我该怎么做才能解决此问题?
public class GridAdapter extends BaseAdapter {
private Context context;
private List<TeachersModel> datas;
private boolean isShowDelete;
public GridAdapter(Context context, List<TeachersModel> datas){
this.context=context;
this.datas=datas;
}
//返回子项的个数
@Override
public int getCount() {
return (datas.size()+1);
}
//返回子项对应的对象
@Override
public Object getItem(int position) {
return datas.get(position);
}
//返回子项的下标
@Override
public long getItemId(int position) {
return position;
}
//返回子项视图
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view;
ViewHolder viewHolder;
if(convertView==null){
view = LayoutInflater.from(context).inflate(R.layout.model_teachers,null);
viewHolder=new ViewHolder();
viewHolder.animalImage=(ImageView)view.findViewById(R.id.iv);
viewHolder.animalName=(TextView)view.findViewById(R.id.tv);
viewHolder.deleteImage=(ImageView)view.findViewById(R.id.delete_markView);
view.setTag(viewHolder);//设置tag
}else{
view=convertView;
viewHolder= (ViewHolder) view.getTag();//由tag获取对象
}
if(position<datas.size()) {
Grid_item animal= (Grid_item) getItem(position);
viewHolder.animalName.setText(animal.getAnimal());
viewHolder.animalImage.setImageResource(animal.getImgId());
viewHolder.deleteImage.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);//根据标识位isShowDelete决定是否显示删除图片按钮
if (isShowDelete) {
viewHolder.deleteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datas.remove(position);
setIsShowDelete(false);
}
});
}
}else{
viewHolder.animalName.setText("点击添加");
viewHolder.animalImage.setImageResource(R.drawable.add);
viewHolder.deleteImage.setVisibility(View.GONE);
}
return view;
}
//创建ViewHolder类
class ViewHolder{
ImageView animalImage,deleteImage;
TextView animalName;
}
public void setIsShowDelete(boolean isShowDelete) {
this.isShowDelete = isShowDelete;
notifyDataSetChanged();
}
}
public class MainActivity extends AppCompatActivity {
Button addTeachers;
Button modTeachers;
private ListView listView;
private GridView gridView;
private ArrayList<TeachersModel> teachersModelArrayList;
private CustomAdapterTeacher customAdapterTeacher;
private DatabaseHelperTeacher databaseHelperTeacher;
//grid
private GridAdapter gridAdapter;
private List<Grid_item> datas = new ArrayList<Grid_item>();
private boolean isShowDelete;
public void addTeachersActivity(){
addTeachers= (Button)findViewById(R.id.btn_add_teacher);
addTeachers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent addTeachersr = new Intent(MainActivity.this, AddCourseTeachers.class);
startActivity(addTeachersr);
}
});
}
public void modTeachersActivity(){
modTeachers= (Button)findViewById(R.id.btn_teacher_modify);
modTeachers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent modTeachersr = new Intent(MainActivity.this, ModTeacher.class);
startActivity(modTeachersr);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addTeachersActivity();
modTeachersActivity();
gridView = (GridView) findViewById(R.id.teachers_lv);
databaseHelperTeacher = new DatabaseHelperTeacher(this);
teachersModelArrayList = databaseHelperTeacher.getAllTeachers();
customAdapterTeacher = new CustomAdapterTeacher(this,teachersModelArrayList);
gridView.setAdapter(customAdapterTeacher);
databaseHelperTeacher = new DatabaseHelperTeacher(this);
List<TeachersModel> teachersModelList = databaseHelperTeacher.getAllTeachers();
gridView = (GridView) findViewById(R.id.teachers_lv);
initDatas();
gridAdapter = new GridAdapter(this, teachersModelList);
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == parent.getChildCount() - 1) {
addDatas();
}
}
});
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (position < datas.size()) {
if (isShowDelete) {//删除图片显示时长按隐藏
isShowDelete = false;
gridAdapter.setIsShowDelete(isShowDelete);
} else {//删除图片隐藏式长按显示
isShowDelete = true;
gridAdapter.setIsShowDelete(isShowDelete);
}
}
return false;
}
});
}
private void addDatas() {
Grid_item animalAdd = new Grid_item("個人區域", R.drawable.selfarea);
datas.add(animalAdd);
gridAdapter.notifyDataSetChanged();
}
private void initDatas() {
Grid_item animal0 = new Grid_item("家", R.drawable.home);
Grid_item animal1 = new Grid_item("工廠", R.drawable.factory);
datas.add(animal0);
datas.add(animal1);
}
}
----------
package com.sayem.geeknot.sqlitecrud;
public class Grid_item {
public Grid_item(String animal, int imgId) {
this.animal = animal;
this.imgId = imgId;
}
private String animal;
private int imgId;
public String getAnimal() {
return animal;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
}
----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.sayem.geeknot.sqlitecrud.MainActivity"
tools:ignore="ExtraText">
<Button
android:id="@+id/btn_add_teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add" />
<Button
android:id="@+id/btn_teacher_modify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mod" />
<GridView
android:id="@+id/teachers_lv"
android:layout_width="381dp"
android:layout_height="326dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.543"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.942"
tools:ignore="MissingConstraints" />
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.sayem.geeknot.sqlitecrud.ModTeacher">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/inst_text"
android:textAlignment="center"
android:textColor="#919191"
android:textSize="20sp" />
<ListView
android:id="@+id/teachers_lvi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<TextView
android:id="@+id/teachers_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="@string/name"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/teachers_course"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="@string/course"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/teachers_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="@string/email"
android:layout_marginTop="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="@string/phone" />
<TextView
android:id="@+id/teachers_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/colorAccent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="4dip"
android:layout_marginTop="4dip"
android:gravity="center"
android:orientation="vertical"
tools:ignore="RtlHardcoded,UseCompoundDrawables">
<ImageView
android:id="@+id/iv"
android:layout_width="60dip"
android:layout_height="55dip"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/tv"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="@+id/delete_markView"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_gravity="right|top"
android:adjustViewBounds="true"
android:src="@drawable/delete"
android:visibility="gone"
tools:ignore="ContentDescription,RtlHardcoded" />
</LinearLayout>
这是错误日志
Process: com.sayem.geeknot.sqlitecrud, PID: 11255
java.lang.ClassCastException: com.sayem.geeknot.sqlitecrud.TeachersModel cannot be cast to com.sayem.geeknot.sqlitecrud.Grid_item
at com.sayem.geeknot.sqlitecrud.GridAdapter.getView(GridAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.GridView.onMeasure(GridView.java:1065)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
以此更改您的getView
和ViewHolder
代码:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.model_teachers,parent,false);
viewHolder=new ViewHolder(convertView);
convertView.setTag(viewHolder);//设置tag
}else{
viewHolder= (ViewHolder) convertView.getTag();//由tag获取对象
}
if(position<datas.size()) {
Grid_item animal= (Grid_item) getItem(position);
viewHolder.animalName.setText(animal.getAnimal());
viewHolder.animalImage.setImageResource(animal.getImgId());
viewHolder.deleteImage.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);//根据标识位isShowDelete决定是否显示删除图片按钮
if (isShowDelete) {
viewHolder.deleteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datas.remove(position);
setIsShowDelete(false);
}
});
}
}else{
viewHolder.animalName.setText("点击添加");
viewHolder.animalImage.setImageResource(R.drawable.add);
viewHolder.deleteImage.setVisibility(View.GONE);
}
return convertView;
}
//创建ViewHolder类
class ViewHolder{
ImageView animalImage,deleteImage;
TextView animalName;
public ViewHolder(View view)
{
view.animalImage=(ImageView)view.findViewById(R.id.iv);
view.animalName=(TextView)view.findViewById(R.id.tv);
view.deleteImage=(ImageView)view.findViewById(R.id.delete_markView);
}
}
您需要在ViewHolder
构造函数中找到视图,并且我也做了一些优化。
更新
看起来问题出在,您正在向列表类型的teachersModelList
发送GridAdapter
,并且在getView
内部将类型转换为Grid_item进行更改:
Grid_item animal= (Grid_item) getItem(position);
至:
TeachersModel animal= (TeachersModel) getItem(position);