1
votes

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'm "bug blind". Any help will be greatly appreciated.

activity_main.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" tools:context=".MainActivity">

<ListView
    android:id="@+id/listView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.tadhg.uiuxadapterpractical;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

  ListView lv;
  Context context;

  TypedArray images;
  String [] titleList;
  String [] descList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      context = this;

      titleList = getResources().getStringArray(R.array.Languages);
      descList = 
 getResources().getStringArray(R.array.LanguageDesc);
      images = 
 getResources().obtainTypedArray(R.array.LanguageImg);

      lv = (ListView) findViewById(R.id.listView);

    }
}

OurAdapter.java

package com.example.tadhg.uiuxadapterpractical;

import android.content.Context; import android.content.res.TypedArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;

public class OurAdapter extends BaseAdapter {

private String [] titles;
private String [] desc;
private TypedArray imageID;

private Context context;
private static LayoutInflater layoutInflater;

//constructor
OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
    titles = titleList;
    desc = descList;
    imageID = images;
    context = adapterContext;

    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return titles.length;
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}

private class ViewHolder {
    TextView tv1, tv2;
    ImageView img;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    ViewHolder viewHolder = new ViewHolder();

    if (view == null) {
        view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
        viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
        viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
        viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);

        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    viewHolder.tv1.setText(titles[position]);
    viewHolder.tv2.setText(desc[position]);
    viewHolder.img.setImageResource(imageID.getResourceId(position, 0));

    return view;
} }

List_layout.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">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/cplus" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="0dp"
    android:layout_height="32dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageView2"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="0dp"
    android:layout_height="24dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/imageView2"
    app:layout_constraintTop_toBottomOf="@+id/textView7"
    tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

UIUXAdapterPractical
<string-array name="Languages">
    <item>Java</item>
    <item>Swift</item>
    <item>C#</item>
    <item>SQL</item>
    <item>Javascript</item>
    <item>Jquery</item>
    <item>C++</item>
</string-array>

<string-array name="LanguageDesc">
    <item>Desc. of Java</item>
    <item>Desc. of Swift</item>
    <item>Desc. of C#</item>
    <item>Desc. of SQL</item>
    <item>Desc. of Javascript</item>
    <item>Desc. of Jquery</item>
    <item>Desc. of C++</item>
</string-array>

<array name="LanguageImg">
    <item>@drawable/java</item>
    <item>@drawable/swift</item>
    <item>@drawable/csharp</item>
    <item>@drawable/sql</item>
    <item>@drawable/js</item>
    <item>@drawable/jquery</item>
    <item>@drawable/cplus</item>
</array> </resources>
2

2 Answers

2
votes

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;

    titleList = getResources().getStringArray(R.array.Languages);
    descList =
            getResources().getStringArray(R.array.LanguageDesc);
    images =
            getResources().obtainTypedArray(R.array.LanguageImg);
    final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
    lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(adapter);

}

hope it will solve your problem :)

0
votes

In your listview you have:

android:layout_width="0dp"
android:layout_height="0dp"

instead put

android:layout_width="match_parent"
android:layout_height="match_parent"

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