Calling swapCursor using a CustomCursor for GIngerbread - android

I am using a custom adapter that extends CursorAdapter along with a CursorLoader and a Fragment,I made sure to use everything from the v4 library...however the method swapCursor in my adapter does not seem to work below HoneyComb.
How can I implement the equivalent of this code for Gingerbread:
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// TODO Auto-generated method stub
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
adapter.swapCursor(null);
}
Thank you in advance for your valuable suggestions.

I figured it out, it was a simple mistake.
I was extending android.widget.CursorAdapter instead of android.support.v4.widget.CursorAdapter.

Related

Box2D how to detect touching elements

I am trying to build a game using Box2D.
My problem is: on each update of the engine, I need to check each element and update the state of the element according to the state of touching elements. So I really need a function to detect which element is touching which element.
How can I do that?
Edit:
Example-> i have 5 boxes and box1-box2-box3 are touching to each other and box4-box5 are touching each other. And without any change on collusions if state of box2 changes to blue, box1 and box3 should also become blue
You'll need to implement ContactListener in a class and use it's methods :
public class CollisionListener implements ContactListener {
#Override
public void beginContact(Contact contact) {
}
#Override
public void endContact(Contact arg0) {
// TODO Auto-generated method stub
}
#Override
public void postSolve(Contact arg0, ContactImpulse arg1) {
// TODO Auto-generated method stub
}
#Override
public void preSolve(Contact arg0, Manifold arg1) {
// TODO Auto-generated method stub
}
}
You can set the listener like this
world.setContactListener(new CollisionListener());
Every time two item contact, beginContact(Contact contact) will be called and you will get info in the Contact object about which two items are colliding. You can access them like this
if ( contact.getfixtureA.getBody().getUserData() == "element1" &&
contact.getfixtureB.getBody().getUserData()=="element2" ){
Colliding = true;
}

Applying adapter to ListFragment

I have a simple question.
just as we are able to apply adapter to ListActivity in the following way :
getListView().setAdapter(new myadapter());
the same does not work for ListFragment.
Can anybody update why this does not work apart from quoting from the api reference.
public class mylistfrag extends ListFragment
{
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListView lv=getListView();
ArrayAdapter<String> aa=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,((myfragshow)getActivity()).st);
lv.setAdapter(aa);
}
}
public class myfragshow extends FragmentActivity
{
String st[]={"a","b","C","d","e"};
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.myfragshow);
}
}
thanks
problem:
getListView()
when you are trying to call that method within your fragment it will call your activity's listview reference not your fragment's listview
here is the documentation:
Get the activity's list view widget.
instead directly use the setListAdapter to set the adapter of the fragment
sample:
setListAdapter((new myadapter()));
for a ListFragment you have to use ListFragment.setListAdapter() as if you use setadapter important initialization will be skipped.

Contacts are listing in one row of listview?

I make an app in which phone contact show in listview. but i want to convert it into widget. ie, I want to show contact list on the homescreen in a listview. I searched on google and found some example but none worked. any one has any link. I am not posting my code because it not worked. any help will be appriciated
Update :-
I am able to show list on homescreen. how to bind this listview to contact list
I tried to add this method in my viewfactory class
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
ids = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
aa.add(new ContactStock(info.name,info.phone));
}
phone.close();
//Collections.sort(aa);
adapt=new ContactListAdapter(MainActivity.this, aa);
//listcontact.setAdapter(new ContactListAdapter(MainActivity.this, aa));
//adapter = new ArrayAdapter<Info>(this, android.R.layout.simple_list_item_1);
listcontact.setAdapter(adapt);
}
But it doesn't work, it is working in my app but not in homescreen widget
Update 2 :-
I am showing contact list in widget but all contacts are showing in one row.My remote view class is
public class DialerViewFactory implements RemoteViewsFactory {
private static final int mCount = 2;
private List<Info> mWidgetItems = new ArrayList<Info>();
private Context mContext;
private int mAppWidgetId;
public DialerViewFactory(Context context,Intent intent){
mContext=context;
mAppWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mCount;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
#Override
public RemoteViews getViewAt(int position) {
// TODO Auto-generated method stub
//ContentResolver cr=mContext.getContentResolver();
//getnumber(cr);
RemoteViews rv=new RemoteViews(mContext.getPackageName(),R.layout.row);
rv.setTextViewText(R.id.textrow1, mWidgetItems.toString());
rv.setTextViewText(R.id.textrow2, "Kya");
Bundle extras = new Bundle();
extras.putInt(DialerWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.textrow1, fillInIntent);
return rv;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
ContentResolver cr1=mContext.getContentResolver();
getnumber(cr1);
}
#Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
}
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
mWidgetItems.add(info);
}
phone.close();
}
class Info {
public String name;
public String phone;
public String picture;
#Override
public String toString() {
return name;
}
}
}
What you are looking for is AppWidgets using Collections. This can accept a ListView as the widget for presenting your data.
This will basically consist of
an AppWidgetProvider, as in any widget
a RemoteViewsService / RemoteViewsFactory, responsible for
producing the views in your ListView, much like an Adapter would do.
Since you are not sharing your code, it's hard to say what is not working for you, but you can find working examples in the sdk samples. Note that the samples have apparently been removed from latest sdk versions, so you should probably download the samples for older sdk's, if I remember correctly. You can also browse the source here.
EDIT :
adapt=new ContactListAdapter(MainActivity.this, aa);
listcontact.setAdapter(adapt);
This will not work in app-widgets, for 2 reasons :
You don't have an Activity available. For widgets, you can use the Context provided as argument of the onUpdate callback in your AppWidgetProvider, although in your case it probably won't be needed.
You don't instantiate an Adapter for app-widgets. To bind data to your ListView on your homescreen, you call setRemoteAdapter on the main RemoteView inside your AppWidgetProvider (in the onUpdate method), like this :
Example :
// Setup the intent which points to the StackViewService which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Creating the main RemoteView and binding data to it.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
rv.setRemoteAdapter(R.id.listview, intent);
Basically, the RemoteViewsFactory's getViewAt method acts much like the getView in an Adapter would, you just have to copy and adapt the code of your adapter to make it work in the RemoteViewsFactory. The sample of StackWidget contains a RemoteViewsFactory where all the method are commented to let you know where to perform which tasks.
EDIT 2 :
#Override
public int getCount() {
// This method tells the factory how many rows it has to produce
// you should be returning the size of your data collection
// so in your case :
return mWidgetItems.size();
}
Also, in you getViewAt method you are setting the text as the toString() of the array (this is why you have all contacts in one row), whereas you should read the fields of the individual Info object for the position given by the method:
// get the current Info object by position :
Info currentInfo = mWidgetItems.get(position);
// then use that object to fill each row, like in getView for an Adapter, for example :
rv.setTextViewText(R.id.textrow1, currentInfo.phone);
rv.setTextViewText(R.id.textrow2, currentInfo.name);

startmanagingcursor() still needed?

I am using cursorLoader in my app.
My question is I am still using startmanagingcursor(cursor); method to manage my cursor in a particularactivity. Is this function still needed?
since this method is deprecated, may I know the exact alternative method.
Everyone said LoaderManager with Loader are the solution to my problem.
The real problem is I have implemented LoaderManager and Loader along with these below overridden methods:
#Override
public void onLoaderReset(Loader<Cursor> arg0)
{
adapter.swapCursor(null);
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor)
{
adapter.swapCursor(cursor);
}
#Override
public Loader<Cursor> onCreateLoader(int arg1, Bundle instanceState)
{
CursorLoader cursorLoader= new CursorLoader(getApplicationContext(), searchContentUri, null, selection, selecArgs, null);
return cursorLoader;
}
I wanna know that here where the query is being called?

Calling external method from asynctask and updating ProgressDialog with data of this external method

I have a general question:
Is it possible to update a ProgressDialog Message with data coming from a method, that is called by an AsyncTask
doInBackground(Void... arg0)
Purpose:
The method is generating and returning a file. Depending on the amount of data, this may last more or less time. I would like to inform the user about the progress, telling the current page number of the generating file (page number is a value within the external method).
any suggestions how to access the value and showing it in the ProgressDialog?
I tried already:
declaring page value public and static and access it from the
onProgressUpdate(
with the code below the page value is returned its initial value, however its not updating any idea to obtain the updated value?:
public class prepareFile extends AsyncTask<String, String, String> {
#Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
progressDialog.setMessage(getResources().getString(
R.string.CalcGatheringInformation)+"\n"+Filehandler.pages+" - "+values);
}
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
publishProgress(String.valueOf(Filehandler.pages));
prepareFiles();
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
}
}
Thanks,
You can refer to this link: A very simple example
http://eliasbland.wordpress.com/2011/03/11/an-example-of-how-to-run-a-background-task-and-report-progress-in-the-status-bar-using-asynctask-on-android/
onProgressUpdate is called after publishProgress and you do that only once, before you even start preparing file with prepareFiles - that's why it is not changing later and shows initial value.
I believe you should do something along
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
while(!allPagesPrepared()){
publishProgress(String.valueOf(Filehandler.pages));
preparePage();
}
return null;
}
or divide prepareFiles job in some other way and call publishProgress every few steps of your loading algorithm.

Categories

Resources