Android Studio中ListView声明后无法正常工作的问题求助
Hey there! I can see exactly why your ListView isn't working—you've correctly found the ListView in your layout, but you haven't connected it to your data with an adapter. Android requires an adapter to translate your data arrays into visible list items. Let's fix this step by step.
What's Missing?
You've got your data ready: mTitle, mDescription, and images arrays, but there's no code to tell the ListView how to display this information. That's the adapter's job—it acts as a middleman between your data and the UI.
Step 1: Add a Custom Adapter
Since you want to show images along with text, we'll create a custom ArrayAdapter to handle this. Add this class inside your view_rooms activity (or as a separate class if you prefer):
// Custom Adapter class to handle list items with image and text class CustomAdapter extends ArrayAdapter<String> { Context context; String rTitle[]; String rDescription[]; int rImgs[]; CustomAdapter(Context c, String title[], String description[], int imgs[]) { super(c, R.layout.custom_list_item, R.id.textViewTitle, title); this.context = c; this.rTitle = title; this.rDescription = description; this.rImgs = imgs; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row = layoutInflater.inflate(R.layout.custom_list_item, parent, false); ImageView images = row.findViewById(R.id.imageView); TextView myTitle = row.findViewById(R.id.textViewTitle); TextView myDescription = row.findViewById(R.id.textViewDescription); // Set data to views images.setImageResource(rImgs[position]); myTitle.setText(rTitle[position]); myDescription.setText(rDescription[position]); return row; } }
Step 2: Initialize the Adapter in onCreate
Right after you find the ListView, create an instance of your custom adapter and attach it to the ListView:
// Inside onCreate, after listView = findViewById(R.id.ListView); CustomAdapter adapter = new CustomAdapter(this, mTitle, mDescription, images); listView.setAdapter(adapter);
Step 3: Create the List Item Layout
You'll need a layout file for each list item (name it custom_list_item.xml) to define how the image and text are arranged:
<?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="wrap_content" android:padding="16dp" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/lights_open"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginStart="16dp"> <TextView android:id="@+id/textViewTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" android:text="Title"/> <TextView android:id="@+id/textViewDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:text="Description"/> </LinearLayout> </LinearLayout>
Full Corrected Code
Here's your complete activity code with all the fixes:
package com.example.animation; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class view_rooms extends AppCompatActivity { ListView listView; String mTitle[]={"Room1","Room2","Room3","Room4","Room5"}; String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"}; int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_rooms); listView=findViewById(R.id.ListView); // Create and set the custom adapter CustomAdapter adapter = new CustomAdapter(this, mTitle, mDescription, images); listView.setAdapter(adapter); } // Custom Adapter class to handle list items with image and text class CustomAdapter extends ArrayAdapter<String> { Context context; String rTitle[]; String rDescription[]; int rImgs[]; CustomAdapter(Context c, String title[], String description[], int imgs[]) { super(c, R.layout.custom_list_item, R.id.textViewTitle, title); this.context = c; this.rTitle = title; this.rDescription = description; this.rImgs = imgs; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row = layoutInflater.inflate(R.layout.custom_list_item, parent, false); ImageView images = row.findViewById(R.id.imageView); TextView myTitle = row.findViewById(R.id.textViewTitle); TextView myDescription = row.findViewById(R.id.textViewDescription); // Set data to views images.setImageResource(rImgs[position]); myTitle.setText(rTitle[position]); myDescription.setText(rDescription[position]); return row; } } }
Quick Note
Just a small tip: Android naming conventions recommend using lowercase for view IDs (like listView instead of ListView)—it's not breaking your code, but it's good practice to follow!
内容的提问来源于stack exchange,提问作者Alexa

