Static Application Contex on android - android

I am Trying to inflate one ListView When clicking a member of another ListView Which is in a ListActivity. So when I try to set Adapter it asks me for getApplicationContext, so the ListView is in another activity and I am trying to call that that applicationContext from another the ListActivity which will inflate the ListView if you click it.
I am Doing this and I dont Know If I am right or wrong
public class FirstList extends ActionBarActivity{
public static ListView firstL;
public static ArrayList<Friend> friendSelected= new ArrayList<>();
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversation_list_layout);
context = getApplicationContext();
firstL= (ListView)findViewById(R.id.listView1);
}
}
Second Class
private class FriendSelectorAdapter extends ArrayAdapter<Friend> {
public ChatListAdapter(){
super(FirstList.context,R.layout.list_layout, FirstList.friendSelected);
}
In the second class there is a ListActivity which is displaying all my contacts and this adapter is meant to be called when one of the contacts is clicked so it will populate the listView of the first class.
Am I wrong what i am doing here? I would appreciate any answer, Thanks

Change constructor to some think like this(without any static in activity):
public ChatListAdapter(Context context, List<Friend> friendSelected){
super(context,R.layout.list_layout,friendSelected);
}

Related

Recycler view adapter not reflecting changes

So I have a recycler view adapter set up in my MainActivity. That adapter is hooked up to an arraylist of a custom object I made called group.
public static RecyclerView.Adapter groupsListAdapter;
public ArrayList<Group> myGroups;
//inside onCreate of Main Activity
groupsListAdapter = new MyAdapter(myGroups);
In a different Activity, I am updating the contents of this arraylist, first by passing it through the intent.
Intent groupCreationIntent = new Intent(MainActivity.this, NewGroupActivity.class);
groupCreationIntent.putExtra("groupsList",myGroups);
startActivity(groupCreationIntent);
And then updating it in the other activity.
private ArrayList<Group> groups;
//in oncreate method of other activity
groups = (ArrayList<Group>) getIntent().getSerializableExtra("groupsList");
When going back to the previous activity (the second activity is used for entering some information and syncing it with my database) I run this code.
groups.add(myGroup);
MainActivity.groupsListAdapter.notifyDataSetChanged();
finish();
The problem is that the list adapter is not reflecting the changes, nothing is appearing in my list once i return to the main activity. What am I doing wrong?
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Group> items;
public MyAdapter(List<Group> items) {
this.items = items;
}
// .. your implementation
// add and call this
public void add(Group item) {
this.items.add(item);
notifyItemInserted(items.size() - 1);
}
}
it's not related with your question, you should not use static field for Activity-to-Activity communication.

getting context of fragment in baseadapter

this has became some thing complicated for me since im not so much familiar with fragments but it might also be simple for some of you guys, here i had this part of code referring to an activity, when i changed the activity to fragment it says can not cast from context to ListViewActivity, can you please help me solve this:
#Override
public Filter getFilter() {
return ((ListViewActivity)mContext).new ListFilter();
}
obviously mContext is a context reference.i understand that inside the fragment should get context with getActivity(), but from outside ?thanks a lot.
I would construct a custom adapter similar like this:
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Fragment fragment, List<RowItem> items) {
this.context = fragment.getActivity();
this.rowItems = items;
}
}
And in your fragment, call the adapter like this:
CustomBaseAdapter adapter = new CustomBaseAdapter(this, items);
Now you can cast the context in your adapter to ListViewActivity, assuming the fragment is part of ListViewActivity.
Hope this helps!

Adapter for RecentCalls Activity

I'm using googles source code to build a recent calls look alike activity. I have to create an app with a custom view of the dialer, recent calls and contacts, so my first step was to create a custom dialer. Then, I created a call log, but the appearance wasn't nice enought so I get google's contacts app package to get the RecentCallsActivity and adapt to my app.
Now, I've got almost the app working, but I have some errors that I don't know how to solve. This is a extract of the code with the most relevant parts to try to solve this errors:
public class RecentCallsListActivity extends ListActivity implements View.OnCreateContextMenuListener {
...
RecentCallsAdapter mAdapter;
....
final class RecentCallsAdapter implements ViewTreeObserver.OnPreDrawListener, View.OnClickListener, Runnable {
...
#Override
protected void onCreate(Bundle state) {
super.onCreate(state);
mAdapter = new RecentCallsAdapter();
getListView().setOnCreateContextMenuListener(this);
setListAdapter(mAdapter); // The method SetListAdapter (ListAdapter) in the type ListActivity is not aplicable for the arguments (RecentCallsListActivity.RecentCallsAdapter)
mQueryHandler = new QueryHandler(this);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) {
AdapterView.AdapterContextMenuInfo menuInfo;
Cursor cursor = mAdapter.getItem(menuInfo.position); //The method getItem(int) is undefined for the type RecentCallsListActivity.RecentCallsAdapter
...
These are the 2 principal errors. In the original file, the mAdapter is declared the same way and there aren't these errors.
Also, there are other 2 errors:
mAdapter.isGroupHeader(menuInfo.position)
mAdapter.getGroupSize(menuInfo.position)
It must be some kind of declaration but I don't know why or what to do.
UPDATE
I've solved this extending a class to the adapter thanks to Eugene's answer. But now I have the following problem when implementing some drawables. I know i can't reference to the android.internal.R so the thing would be to find a solution for this:
protected void bindGroupView(View view, Context context, Cursor cursor, int groupSize,
boolean expanded) {
final RecentCallsListItemViews views = (RecentCallsListItemViews) view.getTag();
int groupIndicator = expanded
? com.android.internal.R.drawable.expander_ic_maximized //CANNOT BE RESOLVED
: com.android.internal.R.drawable.expander_ic_minimized; //CANNOT BE RESOLVED
views.groupIndicator.setImageResource(groupIndicator);
views.groupSize.setText("(" + groupSize + ")");
bindView(context, view, cursor);
}
Your adapter does not implement listadapter interface
Original adapter extends ResourceCursorAdapter, which already implements ListAdapter
final class RecentCallsAdapter extends ResourceCursorAdapter
implements Runnable, ViewTreeObserver.OnPreDrawListener, View.OnClickListener {
but your adapter does not.
final class RecentCallsAdapter implements ViewTreeObserver.OnPreDrawListener, View.OnClickListener, Runnable
You should either extend some class, either implement ListAdapter yourself
Other two errors come from same origin - you just don't have such methods, because You haven't implemented them, nor you have extended the class already having them.
Good luck with coding :)

Convert ActivityGroup to Fragment

I am trying to convert my entire project from using ActivityGroups to using Fragments.
Here is my old code :
SettingsActivityGroup
public class SettingsActivityGroup extends ActivityGroup
{
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static SettingsActivityGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
// Window focus changed listener
public OnActivityGroupViewChanged activityGroupViewChangedListener = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Allocate history
this.history = new ArrayList<View>();
// Set group
group = this;
// Start root (first) activity
Intent myIntent = new Intent(this, SettingsActivity.class); // Change to the first activity of your ActivityGroup
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ReplaceView("SettingsActivity", myIntent);
}
SettingsActivity
public class SettingsActivity extends Activity
{
String[] settingsLabels = {"Viderestilling", "Voicemail", "Vis nummer",
"Kø styring", "Optag samtaler", "Services" };
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
lv = (ListView) findViewById(R.id.SettingsLV);
lv.setTextFilterEnabled(true);
populateListView();
}
private void populateListView()
{
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.settings_items, R.id.settings_item_label, settingsLabels));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// SwitchActivity(position);
}
});
}
If I want to extend ListFragment instead of Activity - What do I need to change to make sure that everything still works ?
Have you looked at the Developer Guide article on Fragments? I think it very nearly describes your exact use case (an array-backed list in one fragment and the detail in another). And do check out the complete sample implementation in APIDemos. There's even a backward-compatible version in API 4+ Support Demos.

Setting Initially checked CheckedTextViews in ListView for choiceMode="multipleChoice"

I am having a really difficult time trying to work with android's ListView multipleChoice mode. Here is what I am trying to do:
I have a "players" button in a game setup screen. When this is clicked it opens another activity with a multipleChoice ListView of all the players in the database in CheckedTextViews. I have this working properly and when you click on a player they will be added or removed from the game via a query to the game_players table.
The problem I am having is in setting up the ListView so that the players that have already been added to the game get checked initially when the activity opens.
I have tried to do this by iterating over the entire list in the ListView activity but this doesn't work because the Views that are not currently visible can't be accessed to check.
So now I'm trying to do this in my extended SimpleCursorAdapter in bindView but I can't even get this simple code to work:
#Override
public void bindView(View _view, Context _context, Cursor _cursor) {
String name = c.getString(c.getColumnIndexOrThrow(from[0]));
this.player = (CheckedTextView)_view.findViewById(to[0]);
this.player.setText(name);
this.player.setChecked(true);
}
It correctly sets the player's name with setText(), but I can't get any of the boxes to check in bindView. Is there somewhere else I should be doing this or am I just doing it incorrectly?
Call setItemChecked() on the ListView for each checked position.
I had trouble with this as well and pieced together this solution. I thought about passing the ListView directly into the Adapter, but chose to create an interface instead, to avoid a circular reference between the ListView and the Adapter.
An interface to loosely couple the adapter to the list:
public interface CheckControl {
public void setChecked(int position, boolean value);
}
Custom Adapter:
private class MyAdapter extends CursorAdapter {
private CheckControl checkControl;
public MyAdapter(Context context, Cursor cursor, CheckControl checkControl) {
super(context, cursor, 0);
this.checkControl = checkControl;
...
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
...
checkControl.setChecked(cursor.getPosition(), cursor.getInt(COL_ENABLED) == 1);
...
}
}
And here's how I use these two elements. In my case I'm extending ListViewFragment, but the same could done in any other class that contains the ListView.
public class MyListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
....
CheckControl checkControl = new CheckControl() {
public void setItemChecked(int position, boolean checked) {
getListView().setItemChecked(position, checked);
}
};
setListAdapter(new MyAdapter(getActivity(), null, checkControl));
// Kick off the loader
getLoaderManager().initLoader(LOADER_1, null, this);
}
}

Categories

Resources