gpt4 book ai didi

java - ListView 内容问题(viewHolder 没有更改内部 View )

转载 作者:行者123 更新时间:2023-12-01 10:01:48 25 4
gpt4 key购买 nike

我一直在寻找问题的答案,并且找到了类似的条目并修复了一些问题,但不是主要问题。当我运行代码时,所有缩略图的图像都是相同的,并且 TextView 中没有显示任何文本,即使 Log.D 显示我正在将 textView 更改为正确的文本和图像。我的 Activity 代码:

ListView lvMaterias;
String[] materiasNombre;
int[] thumbnails={R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui,
R.drawable.thumbnailanato,
R.drawable.thumbnailbioqui};
List<materiaRow> materiasObjetos= new ArrayList<materiaRow>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
//inicializar los arrays y todo
lvMaterias = (ListView)findViewById(R.id.lvMaterias);
materiasNombre=this.getResources().getStringArray(R.array.nombreMateria);
int i=0;
for (String nombre : materiasNombre){
Log.d("loop", nombre);
materiasObjetos.add(new materiaRow(thumbnails[i],nombre, "0%"));
}
lvMaterias.setAdapter(new materiasAdapter(getApplicationContext(), R.layout.rowmateria, materiasObjetos));
}

}

我的适配器类,getView 方法中的 log.d 显示我有正确的文本和图像,但 textView 没有发生更改:

public class materiasAdapter extends ArrayAdapter implements     View.OnClickListener{
private int layout;
public materiasAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
layout=resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
materiaHolder mH;
if (convertView==null){
mH= new materiaHolder();
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(layout, parent, false);
mH.thumbnail= (ImageView) convertView.findViewById(R.id.ivthumbnail);
mH.materia= (TextView) convertView.findViewById(R.id.tvmateria);
mH.porcentaje= (TextView) convertView.findViewById(R.id.tvporcentaje);
mH.favorito = (Button) convertView.findViewById(R.id.bfavoritos);
mH.favorito.setOnClickListener(this);
convertView.setTag(mH);
}
else {
mH= (materiaHolder) convertView.getTag();
}
materiaRow mR= (materiaRow) getItem(position);
mH.thumbnail.setImageResource(mR.getThumbnail());
mH.porcentaje.setText(mR.getPorcentaje());
Log.d("thumbnail", Integer.toString(mR.getThumbnail()));
mH.materia.setText(mR.getMateria());
Log.d("texto", mR.getMateria());
return convertView;
}

@Override
public void onClick(View v) {

}

class materiaHolder {
ImageView thumbnail;
TextView materia;
TextView porcentaje;
Button favorito;
}
}

根据请求的 XML(选择 Activity ):

<?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.example.quetzal.elite.Select">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>

<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lvMaterias" />

和 rowMateria(适配器类的资源):

<?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"
android:orientation="horizontal">

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/ivthumbnail" />

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp" >

<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/tvmateria" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/tvporcentaje" />
</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="fav"
android:id="@+id/bfavoritos" />

</LinearLayout>

预先感谢您的帮助。

最佳答案

就缩略图问题而言...在您的主 onCreate() 中,看起来您没有正确添加行。

int i=0;
for (String nombre : materiasNombre){
Log.d("loop", nombre);
materiasObjetos.add(new materiaRow(thumbnails[i],nombre, "0%"));
}

您需要增加 i,否则您将始终获得缩略图[0]。

关于java - ListView 内容问题(viewHolder 没有更改内部 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756540/

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