I'm having trouble using a List Adapter inside a Fragment class.
ListAdapter adapter = new SimpleAdapter(
getActivity().getBaseContext(),
itemsList,
R.layout.list_item,
new String[] {
TAG_VENDORNAME,
TAG_PRICE
},
new int[] {
R.id.vendorName, R.id.price
}
);
setListAdapter(adapter);
Error message is on setListAdapter:
setListAdapter(adapter) is undefined for the type
SearchActivity.SearchFragment.GetResults
GetResults is a class i've created for the AsyncToken Api call.
I thought setListAdapter was a library inside the ListAdapter class?
Make sure that your fragment extends ListFragment class, not just Fragment. setListAdapter() method is defined on ListFragment class. Then you need to call this method on the instance of ListFragment class.
Related
This is my method in CheckActivity.java
public void check5(View view) {
lv = (ListView) findViewById(R.id.listView1);
modelItems[5] = new Model("Item", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
This how I call method in non-activity class
CheckActivity mActivity = new CheckActivity();
mActivity.check5();
But this is the error I got.
I alt-entered and android studio create new method in CheckActivity.java
public void check5()
{
lv = (ListView) findViewById(R.id.listView1);
modelItems[5] = new Model("Item", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
But my code will not work with (View view) how do I fix that?
Edit - Some told me that this is bad idea to do. So if this is impossible how do I copy my method into non-activity class?
You need to pass Context of CheckActivity Activity in your non Activity Class and then you call Activity method from Non Activity Class by casting Context in Activity name.
((CheckActivity)context).check5(view);
pass view with just like you have called member function , so you can access variables of class CheckActvity declared as public .
I want to load a listview in my fragment and the problem with the adapter is the error - Cannot resolve constructor ArrayAdapter.
This is the onCreateView method in my class :
public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {
#Override
public void onCreateView() {
tv1 = (TextView) fragment.findViewById("xtv1");
tv2 = (TextView) fragment.findViewById("xtv2");
lv=(ListView) fragment.findViewById("xdevices") ;
mydevices = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidGUIModifier.this,android.R.layout.simple_list_item_1,mydevices);// the problem here : Cannot resolve constructor ArrayAdapter
lv.setAdapter(adapter);
}
}
Obtain the Context from the fragment and just pass it to the ArrayAdapter :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(fragment.getContext(), android.R.layout.simple_list_item_1, mydevices);
If it is inside a fragment, you need to pass context for the first argument usually like this I think?
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mydevices);
If you are using AndroidGUIModifier as your fragment, can you try extending the Fragment class?
public class AndroidGUIModifier extends Fragment implements IMyComponentGUIModifier, IFragmentEvents {
I am trying to add items to a ListView/Adapter from my MainActivity and use it across ListHelper.
I have it declared in Registrant.
public static List<Registrant> searchList = new ArrayList<Registrant>();
And then I add to it from MainActivity..
Registrant.searchList.add(registrant);
Then I try to retrieve this into a listview from the ListHelper.
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
adapter = new CustomListAdapter(this, Registrant.searchList);
adapter.notifyDataSetChanged();
But no luck, it is a blank screen?
Any ideas would be helpful.. thank you!
Instead of creating object of CustomListAdapter class for updating data in ListView data-source. create a method in CustomListAdapter class for adding data in List which is used by Adapter as data-source :
In CustomListAdapter class add following method:
public void addItems(){
arrList.addAll(Registrant.searchList);
this.notifyDataSetChanged();
}
Now call addItems method when want to update data in ListView:
listView.setAdapter(adapter);
adapter.addItems();
I have made a simple contact apps and now I wish to add tab view to this app. I am following the tutorial here. Below is part of the source code for my MainActivity.jave:
public class MainActivity extends ListActivity {
private ListView contactListView;
private CursorAdapter contactListViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // get ListView id
contactListView.setOnItemClickListener(viewContactListListener);
String[] from = new String[] { "familyName" }; // built an String array
// named "from"
int[] to = new int[] { R.id.contactTextView };
contactListViewAdapter = new SimpleCursorAdapter(MainActivity.this,
R.layout.contact_list_item, null, from, to);
setListAdapter(contactListViewAdapter); // set adapter
}
I tried to change "public class MainActivity extends ListActivity" to "public class MainActivity extends TabActivity implements OnTabChangeListener{". However, I get the error "The method getListView() is undefined for the type MainActivity" and other similar errors. I need suggestions on how to fix this. Thanks for any help.
ListActivity provides some help methods to manage ListView so if you want to change your parent activity from ListActivity to TabActivity you need to handle ListActivity behaviour yourself.
Basically you need to get and store somewhere the ListView object. Something like mListView = (ListView) findViewById(android.R.id.list) on your onCreate method and then implement missing method
ListView getListView()
{
return mListView;
}
Also setting your adapter will be a bit different. Instead of calling setListAdapter(contactListViewAdapter); // set adapter there should be contactListView.setAdapter(contactListViewAdapter);
I am trying to add a two-column ListView to my android app. When I created the project, I selected the option Fragment which creates that beautiful navigation by sliding to left and right. So my MainActivity extends FragmentActivity.
my problem is when I am trying to add AddayAdapter to ListView, the construstor of ArrayAdapter looks for a context object and I dont know to pass that.
here is the code where I get error
StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.row , list);
listview.setAdapter(adapter);
When I pass "this", it passes FragmentActivity because the activity extends FragmentActivity. but constructor need a "Context" object.
it gives this error message
"The constructor MainActivity.StableArrayAdapter(MainActivity.DummySectionFragment, int, ArrayList<String>) is undefined"
How can I pass Context object?
You are setting the adapter inside a fragment. Use
StableArrayAdapter adapter = new StableArrayAdapter(this.getActivity(), R.layout.row , list);
StableArrayAdapter adapter = new StableArrayAdapter(getContext(), R.layout.row , list);
listview.setAdapter(adapter);
Did you try passing something like this from your Fragment. Try either getContext() or getApplicationContext().