有人能向我展示为什么ListView没有显示吗?

huangapple go评论87阅读模式
英文:

Can somebody show me why ListView isn't showing?

问题

  1. 以下是翻译后的内容:
  2. 以下项目是从在线课程中逐字复制的。它应该显示一个 ListView,但什么都没有显示出来。我对 Android Studio 还完全不熟,这只是我在其中的第二个项目。总共有 5 个文件,我将逐个显示它们的内容。我只需要另一双或两双眼睛来找出问题,因为我“对错误视而不见”。任何帮助将不胜感激。
  3. activity_main.xml
  4. <androidx.constraintlayout.widget.ConstraintLayout
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. xmlns:tools="http://schemas.android.com/tools"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context=".MainActivity">
  11. <ListView
  12. android:id="@+id/listView"
  13. android:layout_width="0dp"
  14. android:layout_height="0dp"
  15. android:layout_marginStart="16dp"
  16. android:layout_marginTop="16dp"
  17. android:layout_marginEnd="16dp"
  18. android:layout_marginBottom="16dp"
  19. android:visibility="visible"
  20. app:layout_constraintBottom_toBottomOf="parent"
  21. app:layout_constraintEnd_toEndOf="parent"
  22. app:layout_constraintStart_toStartOf="parent"
  23. app:layout_constraintTop_toTopOf="parent" />
  24. </androidx.constraintlayout.widget.ConstraintLayout>
  25. MainActivity.java
  26. package com.example.tadhg.uiuxadapterpractical;
  27. import androidx.appcompat.app.AppCompatActivity;
  28. import android.content.Context;
  29. import android.content.res.TypedArray;
  30. import android.os.Bundle;
  31. import android.widget.ListView;
  32. public class MainActivity extends AppCompatActivity {
  33. ListView lv;
  34. Context context;
  35. TypedArray images;
  36. String [] titleList;
  37. String [] descList;
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. context = this;
  43. titleList = getResources().getStringArray(R.array.Languages);
  44. descList = getResources().getStringArray(R.array.LanguageDesc);
  45. images = getResources().obtainTypedArray(R.array.LanguageImg);
  46. lv = (ListView) findViewById(R.id.listView);
  47. }
  48. }
  49. OurAdapter.java
  50. package com.example.tadhg.uiuxadapterpractical;
  51. import android.content.Context;
  52. import android.content.res.TypedArray;
  53. import android.view.LayoutInflater;
  54. import android.view.View;
  55. import android.view.ViewGroup;
  56. import android.widget.BaseAdapter;
  57. import android.widget.ImageView;
  58. import android.widget.TextView;
  59. public class OurAdapter extends BaseAdapter {
  60. private String [] titles;
  61. private String [] desc;
  62. private TypedArray imageID;
  63. private Context context;
  64. private static LayoutInflater layoutInflater;
  65. //constructor
  66. OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
  67. titles = titleList;
  68. desc = descList;
  69. imageID = images;
  70. context = adapterContext;
  71. layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  72. }
  73. @Override
  74. public int getCount() {
  75. return titles.length;
  76. }
  77. @Override
  78. public Object getItem(int i) {
  79. return i;
  80. }
  81. @Override
  82. public long getItemId(int i) {
  83. return i;
  84. }
  85. private class ViewHolder {
  86. TextView tv1, tv2;
  87. ImageView img;
  88. }
  89. @Override
  90. public View getView(int position, View view, ViewGroup viewGroup) {
  91. ViewHolder viewHolder = new ViewHolder();
  92. if (view == null) {
  93. view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
  94. viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
  95. viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
  96. viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
  97. view.setTag(viewHolder);
  98. } else {
  99. viewHolder = (ViewHolder) view.getTag();
  100. }
  101. viewHolder.tv1.setText(titles[position]);
  102. viewHolder.tv2.setText(desc[position]);
  103. viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
  104. return view;
  105. }
  106. }
  107. List_layout.xml
  108. <?xml version="1.0" encoding="utf-8"?>
  109. <androidx.constraintlayout.widget.ConstraintLayout
  110. xmlns:android="http://schemas.android.com/apk/res/android"
  111. xmlns:app="http://schemas.android.com/apk/res-auto"
  112. xmlns:tools="http://schemas.android.com/tools"
  113. android:layout_width="match_parent"
  114. android:layout_height="match_parent">
  115. <ImageView
  116. android:id="@+id/imageView2"
  117. android:layout_width="wrap_content"
  118. android:layout_height="wrap_content"
  119. android:layout_marginStart="16dp"
  120. android:layout_marginTop="8dp"
  121. app:layout_constraintStart_toStartOf="parent"
  122. app:layout_constraintTop_toTopOf="parent"
  123. app:srcCompat="@drawable/cplus" />
  124. <TextView
  125. android:id="@+id/textView7"
  126. android:layout_width="0dp"
  127. android:layout_height="32dp"
  128. android:layout_marginStart="16dp"
  129. android:layout_marginTop="8dp"
  130. android:layout_marginEnd="8dp"
  131. android:text="TextView"
  132. android:textSize="24sp"
  133. app:layout_constraintEnd_toEndOf="parent"
  134. app:layout_constraintStart_toEndOf="@+id/imageView2"
  135. app:layout_constraintTop_toTopOf="parent" />
  136. <TextView
  137. android:id="@+id/textView8"
  138. android:layout_width="0dp"
  139. android:layout_height="24dp"
  140. android:layout_marginStart="16dp"
  141. android:layout_marginTop="8dp"
  142. android:layout_marginEnd="8dp"
  143. android:text="TextView"
  144. android:textSize="18sp"
  145. app:layout_constraintEnd_toEndOf="parent"
  146. app:layout_constraintStart_toEndOf="@+id/imageView2"
  147. app:layout_constraintTop_toBottomOf="@+id/textView7"
  148. tools:text="TextView" />
  149. </androidx.constraintlayout.widget.ConstraintLayout>
  150. strings.xml
  151. <resources>
  152. <string name="app_name">UIUXAdapterPractical</string>
  153. <string-array name="Languages">
  154. <item>Java</item>
  155. <item>Swift</item>
  156. <item>C#</item>
  157. <item>SQL</item>
  158. <item>Javascript</item>
  159. <item>Jquery</item>
  160. <item>C++</item>
  161. </string-array>
  162. <string-array name="LanguageDesc">
  163. <item>Java 的描述</item>
  164. <item>Swift 的描述</item>
  165. <item>C# 的描述</item>
  166. <item>SQL 的描述</item>
  167. <item>Javascript 的描述</item>
  168. <item>Jquery 的描述</item>
  169. <item>C++ 的描述</item>
  170. </string-array
  171. <details>
  172. <summary>英文:</summary>
  173. 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&#39;m &quot;bug blind&quot;. Any help will be greatly appreciated.
  174. &gt; activity_main.xml
  175. &gt; &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  176. &gt; &lt;androidx.constraintlayout.widget.ConstraintLayout
  177. &gt; xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  178. &gt; xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  179. &gt; xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  180. &gt; android:layout_width=&quot;match_parent&quot;
  181. &gt; android:layout_height=&quot;match_parent&quot;
  182. &gt; tools:context=&quot;.MainActivity&quot;&gt;
  183. &gt;
  184. &gt; &lt;ListView
  185. &gt; android:id=&quot;@+id/listView&quot;
  186. &gt; android:layout_width=&quot;0dp&quot;
  187. &gt; android:layout_height=&quot;0dp&quot;
  188. &gt; android:layout_marginStart=&quot;16dp&quot;
  189. &gt; android:layout_marginTop=&quot;16dp&quot;
  190. &gt; android:layout_marginEnd=&quot;16dp&quot;
  191. &gt; android:layout_marginBottom=&quot;16dp&quot;
  192. &gt; android:visibility=&quot;visible&quot;
  193. &gt; app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  194. &gt; app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  195. &gt; app:layout_constraintStart_toStartOf=&quot;parent&quot;
  196. &gt; app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt; &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  197. &gt;
  198. &gt; MainActivity.java
  199. &gt;
  200. &gt; package com.example.tadhg.uiuxadapterpractical;
  201. &gt;
  202. &gt; import androidx.appcompat.app.AppCompatActivity;
  203. &gt;
  204. &gt; import android.content.Context;
  205. &gt; import android.content.res.TypedArray;
  206. &gt; import android.os.Bundle;
  207. &gt; import android.widget.ListView;
  208. &gt;
  209. &gt; public class MainActivity extends AppCompatActivity {
  210. &gt;
  211. &gt; ListView lv;
  212. &gt; Context context;
  213. &gt;
  214. &gt; TypedArray images;
  215. &gt; String [] titleList;
  216. &gt; String [] descList;
  217. &gt;
  218. &gt; @Override
  219. &gt; protected void onCreate(Bundle savedInstanceState) {
  220. &gt; super.onCreate(savedInstanceState);
  221. &gt; setContentView(R.layout.activity_main);
  222. &gt;
  223. &gt; context = this;
  224. &gt;
  225. &gt; titleList = getResources().getStringArray(R.array.Languages);
  226. &gt; descList =
  227. &gt; getResources().getStringArray(R.array.LanguageDesc);
  228. &gt; images =
  229. &gt; getResources().obtainTypedArray(R.array.LanguageImg);
  230. &gt;
  231. &gt; lv = (ListView) findViewById(R.id.listView);
  232. &gt;
  233. &gt; }
  234. &gt; }
  235. &gt;
  236. &gt; OurAdapter.java
  237. &gt;
  238. &gt; package com.example.tadhg.uiuxadapterpractical;
  239. &gt;
  240. &gt; import android.content.Context; import android.content.res.TypedArray;
  241. &gt; import android.view.LayoutInflater; import android.view.View; import
  242. &gt; android.view.ViewGroup; import android.widget.BaseAdapter; import
  243. &gt; android.widget.ImageView; import android.widget.TextView;
  244. &gt;
  245. &gt; public class OurAdapter extends BaseAdapter {
  246. &gt;
  247. &gt; private String [] titles;
  248. &gt; private String [] desc;
  249. &gt; private TypedArray imageID;
  250. &gt;
  251. &gt; private Context context;
  252. &gt; private static LayoutInflater layoutInflater;
  253. &gt;
  254. &gt; //constructor
  255. &gt; OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
  256. &gt; titles = titleList;
  257. &gt; desc = descList;
  258. &gt; imageID = images;
  259. &gt; context = adapterContext;
  260. &gt;
  261. &gt; layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  262. &gt; }
  263. &gt;
  264. &gt; @Override
  265. &gt; public int getCount() {
  266. &gt; return titles.length;
  267. &gt; }
  268. &gt;
  269. &gt; @Override
  270. &gt; public Object getItem(int i) {
  271. &gt; return i;
  272. &gt; }
  273. &gt;
  274. &gt; @Override
  275. &gt; public long getItemId(int i) {
  276. &gt; return i;
  277. &gt; }
  278. &gt;
  279. &gt; private class ViewHolder {
  280. &gt; TextView tv1, tv2;
  281. &gt; ImageView img;
  282. &gt; }
  283. &gt;
  284. &gt; @Override
  285. &gt; public View getView(int position, View view, ViewGroup viewGroup) {
  286. &gt; ViewHolder viewHolder = new ViewHolder();
  287. &gt;
  288. &gt; if (view == null) {
  289. &gt; view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
  290. &gt; viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
  291. &gt; viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
  292. &gt; viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
  293. &gt;
  294. &gt; view.setTag(viewHolder);
  295. &gt; } else {
  296. &gt; viewHolder = (ViewHolder) view.getTag();
  297. &gt; }
  298. &gt;
  299. &gt; viewHolder.tv1.setText(titles[position]);
  300. &gt; viewHolder.tv2.setText(desc[position]);
  301. &gt; viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
  302. &gt;
  303. &gt; return view;
  304. &gt; } }
  305. &gt;
  306. &gt;
  307. &gt; List_layout.xml
  308. &gt;
  309. &gt; &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  310. &gt; &lt;androidx.constraintlayout.widget.ConstraintLayout
  311. &gt; xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  312. &gt; xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  313. &gt; xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  314. &gt; android:layout_width=&quot;match_parent&quot;
  315. &gt; android:layout_height=&quot;match_parent&quot;&gt;
  316. &gt;
  317. &gt; &lt;ImageView
  318. &gt; android:id=&quot;@+id/imageView2&quot;
  319. &gt; android:layout_width=&quot;wrap_content&quot;
  320. &gt; android:layout_height=&quot;wrap_content&quot;
  321. &gt; android:layout_marginStart=&quot;16dp&quot;
  322. &gt; android:layout_marginTop=&quot;8dp&quot;
  323. &gt; app:layout_constraintStart_toStartOf=&quot;parent&quot;
  324. &gt; app:layout_constraintTop_toTopOf=&quot;parent&quot;
  325. &gt; app:srcCompat=&quot;@drawable/cplus&quot; /&gt;
  326. &gt;
  327. &gt; &lt;TextView
  328. &gt; android:id=&quot;@+id/textView7&quot;
  329. &gt; android:layout_width=&quot;0dp&quot;
  330. &gt; android:layout_height=&quot;32dp&quot;
  331. &gt; android:layout_marginStart=&quot;16dp&quot;
  332. &gt; android:layout_marginTop=&quot;8dp&quot;
  333. &gt; android:layout_marginEnd=&quot;8dp&quot;
  334. &gt; android:text=&quot;TextView&quot;
  335. &gt; android:textSize=&quot;24sp&quot;
  336. &gt; app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  337. &gt; app:layout_constraintStart_toEndOf=&quot;@+id/imageView2&quot;
  338. &gt; app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  339. &gt;
  340. &gt; &lt;TextView
  341. &gt; android:id=&quot;@+id/textView8&quot;
  342. &gt; android:layout_width=&quot;0dp&quot;
  343. &gt; android:layout_height=&quot;24dp&quot;
  344. &gt; android:layout_marginStart=&quot;16dp&quot;
  345. &gt; android:layout_marginTop=&quot;8dp&quot;
  346. &gt; android:layout_marginEnd=&quot;8dp&quot;
  347. &gt; android:text=&quot;TextView&quot;
  348. &gt; android:textSize=&quot;18sp&quot;
  349. &gt; app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  350. &gt; app:layout_constraintStart_toEndOf=&quot;@+id/imageView2&quot;
  351. &gt; app:layout_constraintTop_toBottomOf=&quot;@+id/textView7&quot;
  352. &gt; tools:text=&quot;TextView&quot; /&gt;
  353. &gt;
  354. &gt; &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  355. &gt;
  356. &gt;
  357. &gt; strings.xml
  358. &gt;
  359. &gt; &lt;resources&gt;
  360. &gt; &lt;string name=&quot;app_name&quot;&gt;UIUXAdapterPractical&lt;/string&gt;
  361. &gt;
  362. &gt; &lt;string-array name=&quot;Languages&quot;&gt;
  363. &gt; &lt;item&gt;Java&lt;/item&gt;
  364. &gt; &lt;item&gt;Swift&lt;/item&gt;
  365. &gt; &lt;item&gt;C#&lt;/item&gt;
  366. &gt; &lt;item&gt;SQL&lt;/item&gt;
  367. &gt; &lt;item&gt;Javascript&lt;/item&gt;
  368. &gt; &lt;item&gt;Jquery&lt;/item&gt;
  369. &gt; &lt;item&gt;C++&lt;/item&gt;
  370. &gt; &lt;/string-array&gt;
  371. &gt;
  372. &gt; &lt;string-array name=&quot;LanguageDesc&quot;&gt;
  373. &gt; &lt;item&gt;Desc. of Java&lt;/item&gt;
  374. &gt; &lt;item&gt;Desc. of Swift&lt;/item&gt;
  375. &gt; &lt;item&gt;Desc. of C#&lt;/item&gt;
  376. &gt; &lt;item&gt;Desc. of SQL&lt;/item&gt;
  377. &gt; &lt;item&gt;Desc. of Javascript&lt;/item&gt;
  378. &gt; &lt;item&gt;Desc. of Jquery&lt;/item&gt;
  379. &gt; &lt;item&gt;Desc. of C++&lt;/item&gt;
  380. &gt; &lt;/string-array&gt;
  381. &gt;
  382. &gt; &lt;array name=&quot;LanguageImg&quot;&gt;
  383. &gt; &lt;item&gt;@drawable/java&lt;/item&gt;
  384. &gt; &lt;item&gt;@drawable/swift&lt;/item&gt;
  385. &gt; &lt;item&gt;@drawable/csharp&lt;/item&gt;
  386. &gt; &lt;item&gt;@drawable/sql&lt;/item&gt;
  387. &gt; &lt;item&gt;@drawable/js&lt;/item&gt;
  388. &gt; &lt;item&gt;@drawable/jquery&lt;/item&gt;
  389. &gt; &lt;item&gt;@drawable/cplus&lt;/item&gt;
  390. &gt; &lt;/array&gt; &lt;/resources&gt;
  391. </details>
  392. # 答案1
  393. **得分**: 1
  394. 你忘记将适配器附加到列表视图...你必须将你的适配器设置到你的`listview`中,然后你才能看到你的列表视图。
  395. **使用以下代码更改你的代码:**
  396. ```java
  397. @Override
  398. protected void onCreate(Bundle savedInstanceState) {
  399. super.onCreate(savedInstanceState);
  400. setContentView(R.layout.activity_main);
  401. context = this;
  402. titleList = getResources().getStringArray(R.array.Languages);
  403. descList = getResources().getStringArray(R.array.LanguageDesc);
  404. images = getResources().obtainTypedArray(R.array.LanguageImg);
  405. final OurAdapter adapter = new OurAdapter(getApplicationContext(), titleList, descList, images);
  406. lv = (ListView) findViewById(R.id.listView);
  407. lv.setAdapter(adapter);
  408. }

希望这能解决你的问题 有人能向我展示为什么ListView没有显示吗?

英文:

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

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. context = this;
  6. titleList = getResources().getStringArray(R.array.Languages);
  7. descList =
  8. getResources().getStringArray(R.array.LanguageDesc);
  9. images =
  10. getResources().obtainTypedArray(R.array.LanguageImg);
  11. final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
  12. lv = (ListView) findViewById(R.id.listView);
  13. lv.setAdapter(adapter);
  14. }

hope it will solve your problem 有人能向我展示为什么ListView没有显示吗?

答案2

得分: -1

在你的列表视图中,你有:

  1. android:layout_width="0dp"
  2. android:layout_height="0dp"

改为:

  1. android:layout_width="match_parent"
  2. android:layout_height="match_parent"

你正在强制尺寸为0,所以你看不到它。告诉我是否有效。

英文:

In your listview you have:

  1. android:layout_width=&quot;0dp&quot;
  2. android:layout_height=&quot;0dp&quot;

instead put

  1. android:layout_width=&quot;match_parent&quot;
  2. android:layout_height=&quot;match_parent&quot;

You're forcing the size to be 0 so you can't see it. Tell me if it works

huangapple
  • 本文由 发表于 2020年9月7日 16:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63773929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定