how to call ArrayAdapter constructer from Fragment in Android - java

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

Related

How to apply ArrayAdapter parameters as programatically made layout?

For ArrayAdapter,
ArrayAdapter <String> arrayAdapter = new ArrayAdapter(Context , resId , viewId , String[])
We need to have resId and viewId but how can i use Custom programmatically made Android Views ??
Is there any way so that i can apply programatically made layouts in this ArrayAdapter ??
Thankyou !
I did not quite get the question, hope this helps:
How to get the view id
If you programatically created a view and want to get its id, you need to call this:
view.setId(View.generateViewId());
int id = view.getId();
Same goes for Layouts (classes that extend Viewgroup), because they are views (ViewGroup extends View).
Code example:
layout.setId(View.generateViewId());
view.setId(View.generateViewId());
ArrayAdapter<?> adapter = new ArrayAdapter(this, layout.getId(), view.getId());
How to apply an ArrayAdapter
Every object that extends AbsListView has a setAdapter()-Method, in that you can pass an ArrayAdapter.

ArrayAdapter with Json Parsing

i'm doing a system of news feed, so, i've a difficulty for adapter my ArrayAdapter insert value into ListView, with: "username, name, picture, content and others values" i'm newbie in Android Studio.
Searching the internet I found this function
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
But, i use ArrayList and ArrayAdapter:
ArrayList<Post> postList =
new JsonConverter<Post>().toArrayList(s, Post.class);
ArrayList<String> postador = new ArrayList<String>();
for(Post value: postList){
postador.add(value.id_postador);
postador.add(value.post_legenda);
if(value.post_foto != null) {
postador.add(value.post_foto);
}
postador.add(value.post_data);
postador.add(value.curtidas);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_list_item_1, postador
);
lvPost.setAdapter(adapter);
I tried it, but no success.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item, postador, new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
There a method convert ListAdapter in ArrayAdapter or adapter my ArrayAdapter for ListAdapter?
If you are a newbie to Android, I would like to recommend few things to you.
1). Start Using RecyclerView from the very beginning instead of ListView. It is more advanced, flexible and efficient version of ListView. You can find a sample demo for the RecyclerView here and here
2). If you still want to use ListView. Then you further have two options, either to use BaseAdapter or ArrayAdapter.
a). Base Adapter as the name suggests, is a base class for all the adapters. If you need more flexibility, you can go with it. You can find the demo sample here and here
b). On the other hand, ArrayAdapter is a concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.To use something other than TextViews (like in your case) for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want. You can find the demo sample here and here.
Hope it will help you in future and also I suppose you will get answer to your question in the specified demo samples. Thanks and have a good day.. :)

Retrieving ListView to Adaptor across classes

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

using ListAdapter inside a fragment class

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.

Add tab view android

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

Categories