ListView 不显示项目的问题,而一切似乎都很好
Problem with the ListView not displaying items, while everything seems alright
我基本上是为了解决这个问题而感到沮丧,因为我觉得一切都很好。所以基本上我正在制作一个 ArrayAdapter 来让我的 ListView 显示来自我创建的另一个布局的项目。一切都工作正常,但由于某种原因它没有显示项目。请帮忙。
这是我的 MainActivity class:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
ListView listView = (ListView) findViewById(R.id.connectionsList);
Intent intent = new Intent(this, CreateActivity.class);
database = getBaseContext().openOrCreateDatabase("database.db", MODE_PRIVATE, null);
database.execSQL("CREATE TABLE IF NOT EXISTS connections(name TEXT, hostname TEXT, username TEXT, password TEXT, domain TEXT, localdir TEXT, remotedir TEXT, retry INTEGER, timeout INTEGER, port INTEGER);");
Log.d(TAG, "onCreate: SQL WORKED");
View.OnClickListener fabListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent);
}
};
fab.setOnClickListener(fabListener);
ConnectionsAdapter adapter = new ConnectionsAdapter(this, R.layout.list_connections, getConnections());
listView.setAdapter(adapter);
}
private List<Connections> getConnections() {
List<Connections> connections = new ArrayList<>();
Cursor query = database.rawQuery("SELECT * FROM connections;", null);
if(query.moveToFirst()) {
}
return connections;
}
这是我的 ConnectionsAdapter class:
public class ConnectionsAdapter extends ArrayAdapter<Connections> {
private int resourceLayout;
private Context mContext;
private List<Connections> connectionsList;
public ConnectionsAdapter(@NonNull Context context, int resource, @NonNull List<Connections> objects) {
super(context, resource, objects);
this.resourceLayout = resource;
this.mContext = context;
this.connectionsList = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
View.OnClickListener buttonListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
}
};
if(view == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(mContext);
view = inflater.inflate(resourceLayout, null);
}
if(view != null) {
TextView status = (TextView) view.findViewById(R.id.status);
TextView name = (TextView) view.findViewById(R.id.name);
TextView size = (TextView) view.findViewById(R.id.size);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
Button button = (Button) view.findViewById(R.id.details);
status.setText(Color.GREEN + "ONLINE");
name.setText("FIRST ONE");
imageView.setImageDrawable(view.getResources().getDrawable(R.drawable.hard_drive_icon));
size.setText("232/465 GB");
button.setOnClickListener(buttonListener);
}
return view;
}
@Override
public int getCount() {
return super.getCount();
}
@Nullable
@Override
public Connections getItem(int position) {
return super.getItem(position);
}
}
这是我的 MainActivity.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"
android:background="@color/black">
<ListView
android:id="@+id/connectionsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/click_this_to_add_new_connection"
android:src="@drawable/fab_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.954"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976"
tools:ignore="SpeakableTextPresentCheck,RedundantDescriptionCheck" /></androidx.constraintlayout.widget.ConstraintLayout>
这是我的list_connections.xml(用于列表视图适配器的布局):
<androidx.appcompat.widget.LinearLayoutCompat 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"
android:background="#000000">
<TextView
android:id="@+id/status"
android:layout_width="60dp"
android:layout_height="100dp"
android:gravity="center_horizontal|center_vertical"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:text="Offline" />
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="100dp"
android:contentDescription="@string/just_an_icon_for_decoration"
app:srcCompat="@drawable/hard_drive_icon" />
<TextView
android:id="@+id/name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:text="Name" />
<TextView
android:id="@+id/size"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:text="SIZE" />
<Button
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/details" /></androidx.appcompat.widget.LinearLayoutCompat>
问题出在您的 getCount() 方法上。由于您的 connectionsList 是空的 returns 0。如果您想使用硬编码数据进行测试,您可以像下面这样更改它。
@Override
public int getCount() {
return 1; // or 2,5,10 any number of items you want
}
我基本上是为了解决这个问题而感到沮丧,因为我觉得一切都很好。所以基本上我正在制作一个 ArrayAdapter 来让我的 ListView 显示来自我创建的另一个布局的项目。一切都工作正常,但由于某种原因它没有显示项目。请帮忙。 这是我的 MainActivity class:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
ListView listView = (ListView) findViewById(R.id.connectionsList);
Intent intent = new Intent(this, CreateActivity.class);
database = getBaseContext().openOrCreateDatabase("database.db", MODE_PRIVATE, null);
database.execSQL("CREATE TABLE IF NOT EXISTS connections(name TEXT, hostname TEXT, username TEXT, password TEXT, domain TEXT, localdir TEXT, remotedir TEXT, retry INTEGER, timeout INTEGER, port INTEGER);");
Log.d(TAG, "onCreate: SQL WORKED");
View.OnClickListener fabListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent);
}
};
fab.setOnClickListener(fabListener);
ConnectionsAdapter adapter = new ConnectionsAdapter(this, R.layout.list_connections, getConnections());
listView.setAdapter(adapter);
}
private List<Connections> getConnections() {
List<Connections> connections = new ArrayList<>();
Cursor query = database.rawQuery("SELECT * FROM connections;", null);
if(query.moveToFirst()) {
}
return connections;
}
这是我的 ConnectionsAdapter class:
public class ConnectionsAdapter extends ArrayAdapter<Connections> {
private int resourceLayout;
private Context mContext;
private List<Connections> connectionsList;
public ConnectionsAdapter(@NonNull Context context, int resource, @NonNull List<Connections> objects) {
super(context, resource, objects);
this.resourceLayout = resource;
this.mContext = context;
this.connectionsList = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
View.OnClickListener buttonListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
}
};
if(view == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(mContext);
view = inflater.inflate(resourceLayout, null);
}
if(view != null) {
TextView status = (TextView) view.findViewById(R.id.status);
TextView name = (TextView) view.findViewById(R.id.name);
TextView size = (TextView) view.findViewById(R.id.size);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
Button button = (Button) view.findViewById(R.id.details);
status.setText(Color.GREEN + "ONLINE");
name.setText("FIRST ONE");
imageView.setImageDrawable(view.getResources().getDrawable(R.drawable.hard_drive_icon));
size.setText("232/465 GB");
button.setOnClickListener(buttonListener);
}
return view;
}
@Override
public int getCount() {
return super.getCount();
}
@Nullable
@Override
public Connections getItem(int position) {
return super.getItem(position);
}
}
这是我的 MainActivity.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"
android:background="@color/black">
<ListView
android:id="@+id/connectionsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/click_this_to_add_new_connection"
android:src="@drawable/fab_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.954"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976"
tools:ignore="SpeakableTextPresentCheck,RedundantDescriptionCheck" /></androidx.constraintlayout.widget.ConstraintLayout>
这是我的list_connections.xml(用于列表视图适配器的布局):
<androidx.appcompat.widget.LinearLayoutCompat 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"
android:background="#000000">
<TextView
android:id="@+id/status"
android:layout_width="60dp"
android:layout_height="100dp"
android:gravity="center_horizontal|center_vertical"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:text="Offline" />
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="100dp"
android:contentDescription="@string/just_an_icon_for_decoration"
app:srcCompat="@drawable/hard_drive_icon" />
<TextView
android:id="@+id/name"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:text="Name" />
<TextView
android:id="@+id/size"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:text="SIZE" />
<Button
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/details" /></androidx.appcompat.widget.LinearLayoutCompat>
问题出在您的 getCount() 方法上。由于您的 connectionsList 是空的 returns 0。如果您想使用硬编码数据进行测试,您可以像下面这样更改它。
@Override
public int getCount() {
return 1; // or 2,5,10 any number of items you want
}