I am basically getting depression getting to fix this, because everything seems alright to me. So basically I am making an ArrayAdapter to make my ListView display items from another layout that I created. And everyting is working alright, but for some reason it is not displaying items. Please help. Here is my 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;
}
Here is my 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);
}
}
And here is my 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>
Here is my list_connections.xml(The layout used for listview adapter):
<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>
CodePudding user response:
The problem is with your getCount() method. Since your connectionsList is empty it returns 0. If you want to test with hardcoded data you can change it like below.
@Override
public int getCount() {
return 1; // or 2,5,10 any number of items you want
}