ListView android: I can't see my list items - android

I'm trying to show a ListView in my android application. (I know that I'm not extending of ListActivity, it can be the error?) My code:
public class Home extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
startLocation();
createMenuTabs();
final String[] datos = new String[]{"Lista 1","Lista 2","Lista 3","Lista 4","Lista 5"};
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datos);
ListView urban_lines_list = (ListView)findViewById(R.id.urban_lines_list);
urban_lines_list.setAdapter(adaptador);
}
}
The XML is:
<LinearLayout android:id="#+id/Urbano"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/urban_lines"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/list_of_lines"/>
<ListView android:id="#+id/urban_lines_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Thanks for the help!

Your TextView is filling the whole screen, simply change its height to wrap_content.
<TextView android:id="#+id/urban_lines"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/list_of_lines"/>

Related

Android spinner vertical offset changed

I'm using a Spinner below a title (TextView). It is initially set to View.GONE and when the title is clicked, Spinner is set to View.VISIBLE and the popup window is shown using performClick() below the title, which is what I want.
But I asynchronously update the BaseAdapter to add more items to the Spinner when it is still VISIBLE. After the update the Spinner is moved upwards and is overlaying on the title. How can I fix this?
I have used android:dropDownVerticalOffset, but shows the same behaviour after update.
My layout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/some_other_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#null"
android:overlapAnchor="true"
android:spinnerMode="dropdown"
android:visibility="gone"></android.support.v7.widget.AppCompatSpinner>
</FrameLayout>
</LinearLayout>
Ok.
I Tried running you problem with some slight adjustments on my mobile, and I find no issues.
To simulate your asynchronous addition of items, I added a button.It's OnClick will add items to the adapter.
Next, Instead of extending BaseAdapter, I just used an ArrayAdapter.
And, I just added weights to the LinearLayout to help me with the layout look.
Here Is The Layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title"
android:textSize="20dp"
android:gravity="center"
android:textColor="#android:color/black"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<LinearLayout
android:id="#+id/some_other_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#null"
android:overlapAnchor="true"
android:spinnerMode="dropdown"></android.support.v7.widget.AppCompatSpinner>
</FrameLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Load Adapter"
android:id="#+id/button"
/>
</LinearLayout>
And Here Is the Code for the Activity :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Spinner spinner;
Button button;
#Override
protected void onResume() {
super.onResume();
spinner = (Spinner) findViewById(R.id.spinner);
button = (Button) findViewById(R.id.button);
List<String> list = new ArrayList<>();
list.add(getRandomStringInRange('A','Z',5));
ArrayAdapter<String> adapter= new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner.getAdapter();
adapter.add(getRandomStringInRange('A','Z',5));
}
});
}
public int getRandomNumberInRange(int lower,int higher){
int range = higher-lower;
range=(int)(range*Math.random());
return lower + range;
}
public String getRandomStringInRange(char lower,char higher,int length){
String str ="";
for(int i=0;i<length;i++)
str+=(char)(getRandomNumberInRange(lower,higher));
return str;
}
}
I did not find the spinner overlapping the title or it moving at all.
It's working fine.
If you want,I'll send you screenshots.
Please tell me if you are facing any other issues
I couldn't really find a solution for this. But solved by setting fixed height for the spinner as mentioned in this solution.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try {
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
// Get private mPopup member variable and try cast to ListPopupWindow
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);
// Set popupWindow height to 500px
popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
// silently fail...
}

Set empy view on ListFragment

I've got a list fragment with a custom adapter, like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomArrayAdapter adapt = new CustomArrayAdapter(getActivity(), DummyContent.ITEMS);
setListAdapter(adapt);
}
The problem is that I want to set a view in case of no items in my list. How I could do it? My java class is extending ListFragment. Thank you!
If you are using custom layout in your ListFragment then you can just create a view inside your xml file with this id(layout taken from the docs):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
There is also a helper method in ListFragment called setEmptyText that will display the text when your listview is empty.
I've finally solved my problem in other way. In the class "EsameListActivity":
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If there are items in the list
if(DummyContent.ITEMS.size() != 0)
// Show the lsit
setContentView(R.layout.activity_esame_list);
else
// Show a view with a message error
setContentView(R.layout.activity_empty_list);
}
So it solves my problem!

Show static data on listview

I want to show some data on listview in android,I am using setlistadapter for this,but when I get the list "getlist" in oncreate it gives me null pointer exception,
pls help me in this regard
Thanks
Here Is my source code:
public class WOWActivity extends ListActivity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wow);
String[] newsFeed = getResources().getStringArray(R.array.Feeds);
listView = getListView();
this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wow, menu);
return true;
}
}
Activity_wow:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".WOWActivity" >
<ListView
android:id = "#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false" >
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
List_wow:
<?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"
android:orientation="vertical" >
<TextView android:id="#+id/url"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
String Value:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Feeds">
<item >Apples versus Oranges </item>
</string-array>
</resources>
Change by this.
public class WOWActivity extends ListActivity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
String[] newsFeed = getResources().getStringArray(R.array.Feeds);
this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));
}
EDIT:
Remove this from TextView
android:visibility="gone"
just change the following in WOWActivity.java
change listView = getListView(); to listView = (ListView)findViewById(R.id.list);
You needn't extends your class from ListActivity, just get listView as:
listView = (ListView)findViewById(R.id.list);
Next set your adapter to ListView
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_wow ,R.id.url, newsFeed));

The ListView does not scroll. How do I fix it?

I have a ListView inside a Linearlayout.
The listview has a custom cursoradapter.
Everything works fine, except the ListView does not scroll.
Any suggestion more than welcome!! Thanks. maurizio
Here is the XML of the MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is the relevant piece of java code.
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor tabellaCursor=null;
private ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] to = new int[] {R.id.name_entry};
db = new DatabaseHelper(this);
tabellaCursor=db.getWritableDatabase().rawQuery("SELECT _id, colonna1, colo nna2, colonna3 FROM tabella ORDER BY _id", null);
ListAdapter adapter=new MyAdapter(this, R.layout.list_example_entry, tabe llaCursor, new String[]{"colonna1"},to);
ListView lt = (ListView)findViewById(R.id.list);
lt.setAdapter(adapter);
Button addbtn=(Button)findViewById(R.id.buttonAdd);
addbtn.setOnClickListener(new OnClickListener()
{public void onClick(View v){add(); }
});
}
Your ListViews Layout param is "wrap_content". it will expand as you add new items to litview. therefore it won't scroll. If you set it to "match_parent" it will sure start scrolling.And dont forget that a view does scroll only if its contents(childs view what ever)
size is bigger than it.
Add more list items that exceeds height. then you can scroll.
Please use below code, it will solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/buttonAdd"/>
</RelativeLayout>

android-actionbar in a ListActivity

I'm trying to make the android-actionbar plugin working in my ListActivity,
Here's my Class :
public class DisciplinesController extends ListActivity {
private DisciplinesModel dbHelper;
private Cursor cursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_action);
ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
dbHelper = new DisciplinesModel(this);
dbHelper.open();
if (dbHelper.fetchDisciplineCount() == 0) {
dbHelper.fetch();
}
cursor = dbHelper.fetchAllDisciplines();
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this, R.id.list,
cursor, new String[] { "name" }, new int[] { R.id.discipline_name});
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
}
and my XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.markupartist.android.widget.ActionBar
android:id="#+id/actionbar" style="#style/ActionBar" />
<LinearLayout android:id="#+id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/discipline_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical" android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
</LinearLayout>
I always have this exception :
Your content must have a ListView whose id attribute is 'android.R.id.list'
Do you have an idea ??
Thanks !
In your XML layout you must include a ListView element with the #android:id/list id.
<ListView
android:id="#android:id/list"
...
/>
Basically, you need to rip out the section LinearLayout into a new file (e.g. list_item.xml). Replace this with a listview in your list_action.xml
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
andrdoi:layout_height="wrap_content" />
Then change R.id.list in your SimpleCursorAdapter constructor to R.layout.list_item.

Categories

Resources