Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:43:01+00:00 2026-06-09T00:43:01+00:00

I wrote the following classes to create a list view with custom list rows.

  • 0

I wrote the following classes to create a list view with custom list rows. I use the Android Support Library v4 for the project and the ActionBarSherlock library to integrate an actionbar for older devices.

public class CustomListActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Sherlock___Theme_DarkActionBar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_list);
    }
}

…

public class ListFragment extends SherlockListFragment implements LoaderCallbacks<Cursor> {
    private Activity mActivity;
    private CursorAdapter mAdapter;
    // Query parameter as members ...
    private String mFromColumns;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEmptyText("No data to display");
        mActivity = getActivity();
        // Query parameters are stored in members here ...
        mFromColumns = { "_id", "name" };
        mAdapter = new CustomCursorAdapter(mActivity, null, 0);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle extras) {
        return new CursorLoader(mActivity, mUri, mFromColumns, mSelection, mSelectionArgs, sortOrder);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}

…

public class CustomCursorAdapter extends CursorAdapter {
    private LayoutInflater mInflater;

    public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView listItem = (TextView)view.findViewById(R.id.name);
//      TextView listItem = (TextView)view.findViewById(android.R.id.text1);
        String text = cursor.getString(cursor.getColumnIndex("name"));
        listItem.setText(text);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mInflater.inflate(R.layout.list_item, parent, false);
//      return mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    }
}

The list view successfully loads and displays the data rows, when I use the list item and layout provided by the framework (uncommented lines in CustomCursorAdapter). However, when I swap the lines to use my custom layout and list item, findViewById returns null. Here are the xml files.

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.example.app.fragment.ListFragment"
    android:id="@+id/list_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</fragment>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >
    <TextView 
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

I am pretty sure, the problem are how I link the xml files or how they are build. I hope you can see the error. Actually, I do not understand how list_item.xml is used as the row layout for the list view. The only reference I set to fragment_list.xml is via setContentView(R.layout.fragment_list);. And this xml file does not contain any container element such as <ListView android:id="@android:id/list" /> (which I saw in other setups).

Additionally:

I would like to know if it is still recommended to use the ViewHolder pattern in a CursorAdapter or if this behavior is built-in already. I saw it several times in other examples but those inherited from BaseAdapter or SimpleCursorAdapter.


Lessons learned:

While I was trying to solve the problem, I had the right setting for the XML files somewhen. But another thing hindered me from gettings things running. Here is what you need to be aware of.
If you want to display multiple information in your row layout it is important that you include the associated column names in the fromColumn parameter of your CursorLoader. Otherwise, you will run into an exception:

java.lang.IllegalStateException: get field slot from row 0 col -1 failed

Every column name in fromColumns …

String[] fromColumns = { "_id", "name", "comments" }

… is related to what you ask the cursor for. The _id column is mandatory to use the cursor as an iterator.

String name = cursor.getString(cursor.getColumnIndex("name"));
String comment = cursor.getString(cursor.getColumnIndex("comment"));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:43:03+00:00Added an answer on June 9, 2026 at 12:43 am

    Your code works in both cases for me (as is and when I change line comments). The only thing: I’ve added public empty constructor to ListFragment.

    And it is still recommended to use ViewHolder pattern with CursorAdapter.

    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.name = (TextView) view.findViewById(R.id.name);
    
            view.setTag(holder);
        }
    
        String text = cursor.getString(cursor.getColumnIndex("name"));
        holder.name.setText(text);
    }
    
    private static class ViewHolder {
        TextView name;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote following code to create a linkbutton programmatically, but its showing like lable
I have an index.php with the following content: include_once('includes/auth.php'); include_once('classes/class.DatabaseQuery.php'); include_once('classes/class.Project.php'); include_once('classes/class.User.php'); $project =
My question is: How do I create an Android stand-alone test project for an
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following: <html> <head> </head> <body> <input type=date /> </body> </html> Just
I wrote the following code for text file encryption and decryption. The encryption process
I wrote the following pipeline: for i in `ls c*.txt | sort -V`; do
I wrote the following loop since I have to repeat the same/similar code 25
I wrote the following sample code to see how ARC works @property (nonatomic, weak)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.