I've created a layout in XML and it includes a ListView. I can use the ListView just find inside my AsyncTask. In onCreate, I did: listView = (ListView)findViewById(R.id.listView1);
And in onPostExecute of my AsyncTask, I did: listView.setAdapter(new OfferAdapter(Main.this, offers)); THIS WORKS JUST FINE.
But if I try listView.setDivider(null) in onCreate, then the app crashes with a nullpointer there.
How am I supposed to grab hold of my ListView when I'm not using ListActivity?
If findViewById is returning null, I'd make sure you are calling it after setContentView, and that you are using the correct ID.
One option you have is to set the divider in xml, ie:
android:divider="#drawable/icon".
If you want more control, verify that you are following this syntax in your activity:
public class DividerExampleActivity extends Activity {
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
listView.setDivider(null);
}
}
I've examined the ListView.java source code, and setting it to null should be fine.
Another option might be to make yourself a very thin, transparent divider in xml (say 0.5dp).
Related
I use this Pull-to-Refresh, it works, but after the installation can not call standard functions for listView, such as:
public static RefreshableListView lv = (RefreshableListView) findViewById(R.id.messageListView); //this work
lv.setOnItemLongClickListener(longClickListener); //don`t work
lv.setSelection(listViewCountPosition); //don`t work
Parcelable state = lv.onSaveInstanceState(); //don`t work
lv.onRestoreInstanceState(state); //don`t work
Compiler writes error.
RefreshableListView implements FrameLayout ( not listview). Listview is added as a child to the FrameLayout. You have to call getListView() for list view instance.
something like this..
public RefreshableListView lv = (RefreshableListView) findViewById(R.id.messageListView);
lv.getListView().setOnItemLongClickListener(longClickListener);
When I copy a code like this,
private ListView lstview1; <--there is a yellow line beneth it. why?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ListView lstview1=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,listView1);
setListAdapter(adapter);
The last statement shows that The method setListAdapter(ArrayAdapter) is undefined for the type SecActivity
what's matter?
It is because already you have declared lstview1 varible globally and after that on onCreate() method you are also declared it again..
So first change:
lstview1=(ListView)findViewById(R.id.listView1);
And after that if your Activity is extends with only Activity then set adapter to ListView like:
lstview1.setAdapter(adapter);
OR
if it is extends with ListActivity then use
setListAdapter(adapter);
Thanks!!
You have to setAdapeter like this..setListAdapter() is not a method in the Activity it a method in the ListActivity
lstview1.setAdapter(adapter)
The yellow line probably says something like that your variable lstview1 isn't used. That is because this line
ListView lstview1=(ListView)findViewById(R.id.listView1);
makes a new variable in another context. You probably meant
lstview1=(ListView)findViewById(R.id.listView1);
Use the following code:
lstview1.setAdapter(adapter);
instead of :
setListAdapter(adapter);
I am new to android and i am confused about something. I have created a simple ListView, here is my code:
public class MainActivity extends ListActivity {
ArrayList<String> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listItems = new ArrayList<String>();
for(int i=0;i<20;i++){
listItems.add("List Item #"+i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
}
}
This correctly displays the list items on the screen, but here is my question: I have no xml files, no layouts in my project and i do not use setContentView function here. So why is this code working? How can it be displayed even though there is no xml files or layouts?
Thanks
You are using android.R.layout.simple_list_item_1 as the layout, which provides a TextView for you to work with. This is a layout provided by Android for simple lists of text.
If you want to make a more complex layout, you will/can define your own layout.
Android ships with default containers for list items.
As the name suggests, android.R.layout.simple_list_item_1 belongs to the android.R package and not to your app's resources.
I saw that there were many problems regarding notifyDataSetChanged(). I've looked through many of them and none of the solutions worked for me.
I am using ArrayList to set my list, and after my ArrayList is updated, i ran notifyDataSetChanged(). The new list is added onto the previous one.
lets say first list is a,b and new list is a,b,c. what i get in the end is a,b,a,b,c.
and each time updating this happens again with the new list.
I've tried other such as invalidate(), adapter clear(), refreshDrawableState() etc and nothing worked.
Thank you in advance.
Here is the simplified code, a note that changing MainActivity extends Activity to ListActivity crashes the program even after i change the code in .xml file.
public class MainActivity extends Activity
{
ArrayList<String> names = new ArrayList<String>();
ArrayAdapter arrayAdapter = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1 = (ListView) findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
lv1.setAdapter(arrayAdapter);
}
//code here to edit the ArrayList names.
public void onResume()
{
super.onResume(); // Always call the superclass method first
//the activity need to be updated everytime it resumes.
arrayAdapter.notifyDataSetChanged();
}
}
Seems like you are not properly updating the array list. Please try logging the array list contents after updating.
I guess my previous question wasn't clear enough ( Android: failed to setContentView when switching to ListActivity ), so I explain as follows.
In my app I have two listactivities which uses two different listviews:
public class Activity1 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview1);
}
public class Activity2 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview2);
}
}
As required by android, listview must have an ID which is exactly "#android:id/list". If I set the listview in both listview1 and listview2 with the same ID, then they will end up using the same format of listview, which is not what I want. But if I set one of the IDs to be sth like "#+id/listview2", android gave me the error:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
How do I handle this dilema?
btw: my listview is sort of complicated, customed list with image icons and text, so in my code, I also extended the ListAdapter:
this.mListAdapter = new myListAdapter(this,
R.layout.listview1, mTiebaInfo);
setListAdapter(this.mListAdapter);
Don't use ListActivities. Use Activities and have a ListView in the each xml which you can name however you want.