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

  • SEARCH
  • Home
  • 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 7756783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:54:41+00:00 2026-06-01T12:54:41+00:00

I’m trying to implement a custom SimpleCursorAdapter in order to switch out layouts in

  • 0

I’m trying to implement a custom SimpleCursorAdapter in order to switch out layouts in a ListView but I’m getting very random results while scrolling.

My issue here is that when I scroll up and down, the ListView seemingly by random, mix up the layouts. For instance, a row can have the listview_item_row layout at first but when scrolling in and out of the screen it can be replaced by listview_item_reply_row and back again.
I can’t say I’ve really understood how newView works. I have successfully been able to use bindView to determine if I’m to hide an image in the layout or not but new View is veiled in darkness to me about its implementation and why the list scrolling behaves the way it does.

My goal is to have a list with x amount of items. Depending on if the item is a reply or a new message I want to load a specific layout on that row. Depending on if the row has an image or not I want to show/hide the imageview in the row layout.

What I have omitted in the code are the imports and row layouts. I’m trying to implement this by using Fragments and SimpleCursorAdapter in the v4 support package. The row layouts for the ListFragment are visibly different but contain the same widgets.

The ListView layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text_feed_header_random"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:padding="4dp"
    android:text="Allmänt"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFF" />

<!--
The frame layout is here since we will be showing either
the empty view or the list view.

-->

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/text_feed_header_random"
    android:layout_above="@+id/footer" >

    <!--
         Here is the list. Since we are using a ListActivity, we
         have to call it "@android:id/list" so ListActivity will
         find it

    -->

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

    <!-- Here is the view to show if the list is emtpy -->

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No items."
        android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>

<LinearLayout
    android:id="@+id/footer"
    style="@android:style/ButtonBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_random_post"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Gör ett inlägg!" />

    <Button
        android:id="@+id/button_random_refresh"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Refresh list!" />
</LinearLayout>
</RelativeLayout>

A condensed Fragment using the layout above:

public class RandomFragment extends ListFragment implements LOG {
private DatabaseHelper mDbHelper;
private KarenfeedCursorAdapter mAdapter;
private Cursor mCursor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "RANDOMFRAGMENT START!");

    mDbHelper = new DatabaseHelper(getActivity());
    mDbHelper.open();
    mDbHelper.setTable(Posts.TABLE_RANDOM_POSTS);

    //TODO: Replace SimpleCursorAdapter with a FragmentList instead...
    mCursor = mDbHelper.getAllPostsSortedCursor();
    String[] columns = {    Posts.COLUMN_ID,        Posts.COLUMN_CREATED,   Posts.COLUMN_USER,  Posts.COLUMN_COMMENT };
    int[] to = {            R.id.imageItemPhoto,    R.id.textItemDate,      R.id.textItemUser,  R.id.textItemComment };
    int flags = 0;
    mAdapter = new FeedCursorAdapter(getActivity(), R.layout.listview_item_row, mCursor, columns, to, flags);
    this.setListAdapter(mAdapter);
    initFeedList(); // This call in the end executes mCursor = mDbHelper.getAllPostsSorted(); mAdapter.changeCursor(mCursor); mAdapter.notifyDataSetChanged();
}
}

The SimpleCursorAdapter that the ListFragment connects to:

public class FeedCursorAdapter extends SimpleCursorAdapter implements LOG {
private Context mContext;
private int mLayout;

public FeedCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
    super(context, layout, c, from, to, flags);
    // TODO Auto-generated constructor stub
    mContext = context;
    mLayout = layout;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view;
    int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
    int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
    Log.d(TAG, "id: " +id+ " parentId: " +parentId);
    int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
    if(id == parentId) {
        view = inflater.inflate(R.layout.listview_item_row, parent, false);
    } else {
        view = inflater.inflate(R.layout.listview_item_reply_row, parent, false);
    }
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    Log.d(TAG, "bindView()");
    int id = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_ID));
    int parentId = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_PARENT_ID));
    int hasImage = cursor.getInt(cursor.getColumnIndex(Posts.COLUMN_IMAGE));
    String date = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_CREATED));
    String user = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_USER));
    String comment = cursor.getString(cursor.getColumnIndex(Posts.COLUMN_COMMENT));
    TextView dateView = (TextView) view.findViewById(R.id.textItemDate);
    TextView userView = (TextView) view.findViewById(R.id.textItemUser);
    TextView commentView = (TextView) view.findViewById(R.id.textItemComment);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageItemPhoto);

    dateView.setText(date);
    userView.setText(user);
    commentView.setText(comment);
    if(hasImage == 0) {
        imageView.setVisibility(ImageView.GONE);
    } else {
        String bitmapPath = Environment.getExternalStorageDirectory().getPath() + "/feed/" + id + "_thumb.jpg";
        Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
        imageView.setImageDrawable(bitmapDrawable);
        imageView.setVisibility(ImageView.VISIBLE);
    }
}
}
  • 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-01T12:54:42+00:00Added an answer on June 1, 2026 at 12:54 pm

    after reading your question and the code i think you should know that whenever you scroll a listview up or down it communicates with the adapter to populate the new items that came in to focus and for the case of different row for different data you should make a arraylist that will contain the name or id of the items whose backgrounds are different and then you start making there background according to your need on every call to adapter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.