gpt4 book ai didi

java - Android 自定义适配器挂起,直到我向下滚动

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:13 26 4
gpt4 key购买 nike

我正在尝试使用 Android 填充 ListView ,而且我对此很陌生......还没有在这里看到该问题的确切解决方案。问题似乎出在我的自定义适配器中的 getView(...) 方法中。当我运行该应用程序时,它立即崩溃。当我调试时,列表填充得很好,直到它到达 View 的底部 - 然后它会挂起,直到我向下滚动,然后再次填充直到它到达底部......重复该过程直到完全填充。尽管这个问题似乎已经在这里得到解决,但在这种情况下,所有解决方案都不起作用。有 Android 专家有什么建议吗?

这是 ListView xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.heytow.challenge.heycontacts.ContactsActivity">

<ListView
android:layout_width="368dp"
android:layout_height="495dp"
android:id="@+id/customListView"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
</ListView>

和项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<ImageView
android:id="@+id/contact_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
android:contentDescription="Property Image" />

<LinearLayout
android:id="@+id/contact_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:orientation="vertical">

<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Street Address"
android:textSize="18sp"/>

<TextView
android:id="@+id/contact_company"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Description"
android:textSize="15sp"/>
</LinearLayout>

</RelativeLayout>

我的自定义适配器:

public class ContactAdapter extends ArrayAdapter<Contact> {
private final String TAG = ContactAdapter.class.getSimpleName();

private Context mContext;
private List<Contact> mContactList;


//constructor, call on creation
public ContactAdapter(Context context, int resource, ArrayList<Contact> objects) {
super(context, resource, objects);

this.mContext = context;
this.mContactList = objects;
}

@Override
public int getCount() {
return mContactList.size();
}


//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {

//get the property we are displaying
Contact contact = mContactList.get(position);

//get the inflater and inflate the XML layout for each item
LayoutInflater inflater;
View view = convertView;
if(view == null) {
ViewHolder holder = new ViewHolder();
if (contact.isFavorite() == true) {
inflater = ((Activity)mContext).getLayoutInflater();
view = inflater.inflate(R.layout.favorite_contact_layout, null);
} else {
inflater = ((Activity)mContext).getLayoutInflater();
view = inflater.inflate(R.layout.contact_layout, null);
}
}
TextView name = (TextView) view.findViewById(R.id.contact_name);
TextView company = (TextView) view.findViewById(R.id.contact_company);
ImageView image = (ImageView) view.findViewById(R.id.contact_image);

//set address and description
if(contact.getName() != null) {
name.setText(contact.getName());
}

if(contact.getCompanyName() != null) {
company.setText(contact.getCompanyName());
}


//get the image associated with this contact
ImageParser parser = new ImageParser();
AsyncTask<String, Void, Bitmap> urlImage = parser.execute(contact.getSmallImageURL());
try {
image.setImageBitmap(urlImage.get());
} catch (InterruptedException e) {
Log.e(TAG, "Error accessing image URL: " + e.getMessage());
} catch (ExecutionException e) {
Log.e(TAG, "Error accessing image URL: " + e.getMessage());
}
return view;

}
}

以及我的 MainActivity 类中的调用 onCreate。

ArrayAdapter<Contact> adapter = new ContactAdapter(ContactsActivity.this, 0, mContacts);
ListView listView = (ListView) findViewById(R.id.customListView);
listView.setAdapter(adapter);

我只是尝试从 arrayList 填充 ListView。任何帮助,将不胜感激。谢谢!

* 编辑*

这是新的 getView - 效果相同 - 它挂起...当数据不在 View 中时,它会显示“应用程序正在运行”并且 getView 不会继续,直到我滚动:

public View getView(int position, View convertView, ViewGroup parent) {
//get the property we are displaying
Contact contact = mContactList.get(position);
ViewHolder viewHolder;
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater;
if(convertView == null) {
viewHolder = new ViewHolder();
if (contact.isFavorite() == true) {
inflater = ((Activity)mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.favorite_contact_layout, parent, false);
} else {
inflater = ((Activity)mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.contact_layout, parent, false);
}
viewHolder.contactName = (TextView) convertView.findViewById(R.id.contact_name);
viewHolder.contactCompany = (TextView) convertView.findViewById(R.id.contact_company);
viewHolder.contactImage = (ImageView) convertView.findViewById(R.id.contact_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}


//set address and description
if(contact.getName() != null) {
viewHolder.contactName.setText(contact.getName());
}

if(contact.getCompanyName() != null) {
viewHolder.contactCompany.setText(contact.getCompanyName());
}


//get the image associated with this contact
ImageParser parser = new ImageParser();
AsyncTask<String, Void, Bitmap> urlImage = parser.execute(contact.getSmallImageURL());
try {
viewHolder.contactImage.setImageBitmap(urlImage.get());
} catch (InterruptedException e) {
Log.e(TAG, "Error accessing image URL: " + e.getMessage());
} catch (ExecutionException e) {
Log.e(TAG, "Error accessing image URL: " + e.getMessage());
}
return convertView;
}

最佳答案

ListView 重用 View 。看来您正在为 View 膨胀两种不同的布局。尝试通过合并两者来使用相同的布局,并仅设置必要部分的可见性。

关于java - Android 自定义适配器挂起,直到我向下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com