Reputation: 75
My problem is that my listview just displays rectangle boxes with no options visible but when i click any item it displays list's text Below is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
below is my java file:
package com.example.mypc.contextmenuapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
ListView lv;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
String []arr=getResources().getStringArray(R.array.myarray);
adapter=new ArrayAdapter<String>
(getApplicationContext(),android.R.layout.simple_list_item_2
,android.R.id.text1,arr);
lv.setAdapter(adapter);
}
}
Upvotes: 0
Views: 86
Reputation: 1088
Change the line :-
adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_2
,android.R.id.text1,arr);
to
adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1
,android.R.id.text1,arr);
Differnce is that simple_list_item_1
has a single textview while the simple_list_item_2
has two textviews inside a subclass of RelativeLayout
.
Also the arrayadapter
does not fill multiple textview instances . You need to override getView()
for that.
This answer will make it more clear
Note: - Since the listView
is already match_parent
both width and height , there should be no need for align with bottom or end or right.
Try without specifying the textview resource id like:-
adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1
,arr)
Since without it , it uses the default textview for displaying each item. But if you want you own textview , create a layout with textview as the root view and set it id as above and refer it in the constructor with the textview resouceId.
But in your case you do not need it , so try using without it.
Hope this helps.
Upvotes: 1
Reputation: 874
In your image ,texts are very pallid. When you click one of them, the background of the item becomes dark and the white text appears.
On the other hand if you want items with one textview ,you should use ArrayAdapter
, otherwise you must extend ArrayAdapter
class. Here is an example:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Upvotes: 0