英文:
Can somebody show me why ListView isn't showing?
问题
以下是翻译后的内容:
以下项目是从在线课程中逐字复制的。它应该显示一个 ListView,但什么都没有显示出来。我对 Android Studio 还完全不熟,这只是我在其中的第二个项目。总共有 5 个文件,我将逐个显示它们的内容。我只需要另一双或两双眼睛来找出问题,因为我“对错误视而不见”。任何帮助将不胜感激。
activity_main.xml:
<androidx.constraintlayout.widget.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=".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);
}
}
OurAdapter.java:
package com.example.tadhg.uiuxadapterpractical;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class OurAdapter extends 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;
}
}
List_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<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>
strings.xml:
<resources>
<string name="app_name">UIUXAdapterPractical</string>
<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>Java 的描述</item>
<item>Swift 的描述</item>
<item>C# 的描述</item>
<item>SQL 的描述</item>
<item>Javascript 的描述</item>
<item>Jquery 的描述</item>
<item>C++ 的描述</item>
</string-array
<details>
<summary>英文:</summary>
The following project was copied verbatim from an online lesson. It is supposed to display a ListView but nothing shows up. I am brand new to Android Studio and this is only my second project in it. There are 5 files and I will display the contents of each of them below. I just need another pair of eyes or two to find the problem, because I'm "bug blind". Any help will be greatly appreciated.
> activity_main.xml
> <?xml version="1.0" encoding="utf-8"?>
> <androidx.constraintlayout.widget.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=".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);
>
> }
> }
>
> OurAdapter.java
>
> package com.example.tadhg.uiuxadapterpractical;
>
> import android.content.Context; import android.content.res.TypedArray;
> import android.view.LayoutInflater; import android.view.View; import
> android.view.ViewGroup; import android.widget.BaseAdapter; import
> android.widget.ImageView; import android.widget.TextView;
>
> public class OurAdapter extends 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;
> } }
>
>
> List_layout.xml
>
> <?xml version="1.0" encoding="utf-8"?>
> <androidx.constraintlayout.widget.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">
>
> <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>
>
>
> strings.xml
>
> <resources>
> <string name="app_name">UIUXAdapterPractical</string>
>
> <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>
</details>
# 答案1
**得分**: 1
你忘记将适配器附加到列表视图...你必须将你的适配器设置到你的`listview`中,然后你才能看到你的列表视图。
**使用以下代码更改你的代码:**
```java
@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);
final OurAdapter adapter = new OurAdapter(getApplicationContext(), titleList, descList, images);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
希望这能解决你的问题
英文:
you have missed attaching your adapter to listview...you must set your adapter to your listview
then only you will see your listview
change your code with following code
@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);
final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
hope it will solve your problem
答案2
得分: -1
在你的列表视图中,你有:
android:layout_width="0dp"
android:layout_height="0dp"
改为:
android:layout_width="match_parent"
android:layout_height="match_parent"
你正在强制尺寸为0,所以你看不到它。告诉我是否有效。
英文:
In your listview you have:
android:layout_width="0dp"
android:layout_height="0dp"
instead put
android:layout_width="match_parent"
android:layout_height="match_parent"
You're forcing the size to be 0 so you can't see it. Tell me if it works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论