【发布时间】:2021-11-28 15:32:31
【问题描述】:
我基本上得到了解决这个问题的抑郁症,因为在我看来一切都很好。所以基本上我正在制作一个 ArrayAdapter 来让我的 ListView 显示来自我创建的另一个布局的项目。一切正常,但由于某种原因,它没有显示项目。请帮忙。 这是我的 MainActivity 类:
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 类:
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(用于 listview 适配器的布局):
<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>
【问题讨论】:
-
这是您拥有的所有代码还是为了简单起见删除了其中一些?因为我没有看到您在列表中添加了任何数据 if(query.moveToFirst()) { }
-
我添加它是为了测试目的,因为我还没有完成代码。
-
那么你的真实代码中有没有这块代码? if(query.moveToFirst()) { ...有代码吗.. }
-
不,那是从数据库加载连接。但我还没有写。我在 ConnectionsAdapter 中用硬编码的 txt 制作了一个。但由于某种原因,一个没有显示。我是不是搞砸了?
-
哦,检查答案
标签: java android android-studio android-listview