以下项目是从在线课程中逐字复制的。它应该显示一个列表视图,但是什么也没有显示。我是全新的安卓工作室,这只是我的第二个项目。共有5个文件,我将在下面显示每个文件的内容。我只需要另外一两双眼睛就能找到问题,因为我是“虫子盲”。任何帮助都将不胜感激。
活动\u main.xml<androidx.constraintlayout.widget.constraintlayoutxmlns: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=“匹配父项”android:layout_height=“匹配父项”tools:context=“.mainactivity”>
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.tadhg.uiuxadapterpractical;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView lv;
Context context;
TypedArray images;
String [] titleList;
String [] descList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
lv = (ListView) findViewById(R.id.listView);
}
}
我们的适配器.java
包com.example.tadhg.uiuxadapterpractical;
导入android.content.context;导入android.content.res.typedarray;导入android.view.layoutinflater;导入android.view.view;导入android.view.viewgroup;导入android.widget.baseadapter;导入android.widget.imageview;导入android.widget.textview;
公共类ouradapter扩展baseadapter{
private String [] titles;
private String [] desc;
private TypedArray imageID;
private Context context;
private static LayoutInflater layoutInflater;
//constructor
OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
titles = titleList;
desc = descList;
imageID = images;
context = adapterContext;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
private class ViewHolder {
TextView tv1, tv2;
ImageView img;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = new ViewHolder();
if (view == null) {
view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tv1.setText(titles[position]);
viewHolder.tv2.setText(desc[position]);
viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
return view;
} }
列表\u layout.xml
<androidx.constraintlayout.widget.constraintlayoutxmlns: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=“匹配父项”android:layout_height=“匹配父项”>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/cplus" />
<TextView
android:id="@+id/textView7"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView8"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/textView7"
tools:text="TextView" />
</androidx.constraintlayout.widget.constraintlayout>
字符串.xml
Uiux适配器实用
<string-array name="Languages">
<item>Java</item>
<item>Swift</item>
<item>C#</item>
<item>SQL</item>
<item>Javascript</item>
<item>Jquery</item>
<item>C++</item>
</string-array>
<string-array name="LanguageDesc">
<item>Desc. of Java</item>
<item>Desc. of Swift</item>
<item>Desc. of C#</item>
<item>Desc. of SQL</item>
<item>Desc. of Javascript</item>
<item>Desc. of Jquery</item>
<item>Desc. of C++</item>
</string-array>
<array name="LanguageImg">
<item>@drawable/java</item>
<item>@drawable/swift</item>
<item>@drawable/csharp</item>
<item>@drawable/sql</item>
<item>@drawable/js</item>
<item>@drawable/jquery</item>
<item>@drawable/cplus</item>
</array> </resources>
2条答案
按热度按时间xlpyo6sf1#
未能将适配器连接到listview…必须将适配器设置为
listview
只有你才能看到你的listview用以下代码更改代码
希望它能解决你的问题:)
zqdjd7g92#
在listview中,您有:
相反,把
你强制大小为0,这样你就看不到它了。告诉我是否有效