Zuenonentu
Zuenonentu

Reputation: 402

ListView in Android 2.2

I am developing an application for android 2.2, in which I would like to have a ListView with the possibility of onLongClickListener method. So far I have been using this example, but now I got stuck at getLoaderManager().initLoader(0, null, this); because my eclipse claims that it can't find the method. I've got this:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

public class SBNextends extends ListActivity implements LoaderCallbacks<Cursor>{

SimpleCursorAdapter adapter;

private TextView tView;

@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.sbn);
    tView= (TextView) findViewById(R.id.sbn);
    tView.setText("");
    for (int i = 0; i < MyDataLoader.getData.size(); i++) {
        tView.append(MyDataLoader.dataListONE.get(i));
    }
    String[] from = new String[MyDataLoader.dataListONE.size()];
            for(int i = 0; MyDataLoader.dataListONE.size()> i; i++) {
            from[i] = MyDataLoader.dataListONE.get(i);  
            }
    int[] toView = {android.R.id.text1};
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, toView);
    setListAdapter(adapter);

}

Any one got any ideas of why I can't use the getLoaderManager() method? I have imported the support libraries

Upvotes: 0

Views: 524

Answers (2)

edisonthk
edisonthk

Reputation: 1423

It is how I use LoaderCallbacks on v4 apps.

Instead of using ListActivity, use FragmentActivity. In this case, you have to instance your ListView by findViewById as it wouln't automatically instance your ListView.

public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{

    SimpleCursorAdapter mAdapter;   
    ListView listView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.list);


        String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
        int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

        // Create an empty adapter we will use to display the loaded data.
        // We pass null for the cursor, then update it in onLoadFinished()
        mAdapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, null,
                fromColumns, toViews, 0);
        listView.setAdapter(mAdapter);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getSupportLoaderManager().initLoader(0, null, this);

    }

Upvotes: 0

gunar
gunar

Reputation: 14710

Well, you need to be consistent with the base classes that you use: either from compatibility package, either from android.app one. In your case you're extending from android.app.ListActivity, so replace:

import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

with:

import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;

But in this case, you're not actually running against 2.2. So you better stick with compatibility package by extending from FragmentActivity and make your own FragmentListActivity.

Upvotes: 1

Related Questions