What's wrong with this listview statement? - android

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);

Related

AutoCompleteTextView: provide values in XML?

Is it possible to provide values to the AutoCompleteTextView in XML / via resources, therefore without setting an adapter in code?
I want to use the AutoCompleteTextView as part of an exposed dropdown menu, so all values shall be shown at once and no filtering shall happen. Also all values to be shown are known at compile time.
I think you can do that by using the list via Resource and implementing it in AutoCompleteTextView passing by the adapter.
See the example here :
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
Resources res = getResources();
private static final String[] COUNTRIES = res.getStringArray(R.array.planets_array);
}
Hope this could help you or any one else .
Yes its possible. There is a method showDropDown() which you can call.
Try something like
autoCompleteTextView.showDropDown()
Reference :
https://developer.android.com/reference/android/widget/AutoCompleteTextView.html#showDropDown()

Requesting logic for listview fragment

I'm making an activity with a listView fragment in it. I am having a button click on the MainActivity load items for an rss feed that populate the listview fragment.
My question is, how should I go about setting it up? I already have the code for it.
My plan now is to have the button click on MainActivity point to my method in my RSSFragment which extends ListFragment. I'm confused about some things like which activity to pass here:
#Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
lvRss.setAdapter(adapter);
progressDialog.dismiss();
}
}
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
I know it's a long question.
Thanks.
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
If you are in Activity the use this or Activity.this . IF you are in Fragment the use getActivity().
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
Use setListAdapter method setting Adapter to ListFragment

Android adapter notifyDataSetChanged() doesn't clear previous list

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.

ListAdapter error

Could someone tell me what is wrong with this piece of code?
It is supposed to be a ListView from an XML file that is then referred to in Java.
Alas, it crashes my application every time it enters the Menu class.
public class Menu extends ListActivity {
String Name_for_classes[] = {"- 1-9 Tabels -", "- 10-19 Tabels -", "- 20-29 Tabels -" };
String Tabel_classes[] = {"First", "Second", "Third"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.menu, Name_for_classes));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
}
Okay, let's assume you have your ListView in an XML file called my_listview.xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview.xml);
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> yourAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Name_for_classes);
list.setAdapter(yourAdapter);
}
I'm not sure what is in R.layout.menu, but guessing by the naming its the activity layout. This should be used with setContentView(R.layout.menu) in onCreate. The layout that is passed into the ArrayAdapter is the TextView you are using to populate the listview.
You have forgotten to call the setContentView in your onCreate method, so your listview is not referenced yet.
When you use setContentView, it is equivalent as sayin 'for this activity I want to use the template 'myTemplate.xml'
After that, you have to 'linked' your java ListView attribute to the listview declared in your template.

nullpointer on ListView (not using ListActivity)

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).

Categories

Resources