-1

I am trying to use the ListView Component and I am stuck. I'm only trying to view a list in my activity. I searched the internet for a solution, but I don't know what is wrong in my code

activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.a05042.listviewproject.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:id="@+id/ListViewMain"
        android:background="@android:color/holo_blue_bright" />
</RelativeLayout>

activity_list.xml:

<?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="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textAlignment="center" />
</LinearLayout>

JAVA:

public class MainActivity extends AppCompatActivity {
    String[] myItems = {"Sony","Toshiba","Samsung","y","daniel","sold","sail"};

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

    public void MakeListView() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_list,myItems);
        ListView lv = (ListView)findViewById(R.id.ListViewMain);
        lv.setAdapter(adapter);
    }

}
Omar El Halabi
  • 2,118
  • 1
  • 18
  • 26
Daniel Mamary
  • 29
  • 1
  • 6

2 Answers2

1

The constructor you are using is

  /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) {
        this(context, resource, 0, Arrays.asList(objects));
    }

ArrayAdapter expects internally a layout with only a TextView. You have a TextView wrapped in a LinearLayout. You either change your layout to

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:textAlignment="center" />

or use the constructor that allows you to provide the id of the TextView to use

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 1
    i used your explanation and i create a new code and now i using my custom List and every thing working good thank you for your help . – Daniel Mamary Apr 09 '17 at 12:20
0

You are using Simple Listview

so you can change your adapter to

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, myItems );

or if you want Custom layout for item

then you have to make your own adapter

From here

Community
  • 1
  • 1
Elsunhoty
  • 1,609
  • 14
  • 27
  • Thank you guys i understand my problem with my adapter i using a simple_list_item1 for the beginning and i will try the constractor – Daniel Mamary Apr 09 '17 at 09:54