Search code examples
sqlitelistviewcustom-adapter

Android - How to make Custom Adapter Display Data like Simple Array Adapter but with numbered rows?


I am busy with a book reader project for Android. I am using SQLite database. To get certain entries from certain chapters depending on switch/case, I am using an intent putextra/getextra which works just fine to get just entries and populate the standard array adapter. However, I need the items numbered.

I have created a custom adapter (EntryAdapter) to show the line numbers as follows:

  1. entry
  2. entry
  3. entry and so on... in the list view

The problem is that the customer adapter is passes the entire chapter to each list item view over and over. Without the customer adapter (and using a standard array adapter with the standard simple_list_item) it passes one line per list item as it is supposed to, however, I need the lines numbered... thus the custom adapter... How do I get the entries to pass properly... only one entry per list item while still having the lines numbered? Any help would be appreciated.

Here is a sample of the Activity that populates the listview...

public class ChapterActivity extends AppCompatActivity{

private ListView mListView;
private EntryAdapter mAdapter;
static List<String> mChapterEntries;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chapter_entries);
    this.mListView = (ListView) findViewById(R.id.listView);

    Integer chapterSelectedOne =  getIntent().getIntExtra("chapter", 0);
    final DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);

    switch (chapterSelectedOne) {

        case 0:
            databaseAccess.open();
            mChapterEntries = databaseAccess.getEntriesChapterOne();
            databaseAccess.close();
            mAdapter = new EntryAdapter(this, R.layout.entry_list_item, mChapterEntries);
            this.mListView.setAdapter(mAdapter);

            break;

        case 1:

            databaseAccess.open();
            mChapterEntries = databaseAccess.getEntriesChapterTwo();
            databaseAccess.close();
            mAdapter = new EntryAdapter(this, R.layout.entry_list_item, mChapterEntries);
            this.mListView.setAdapter(mAdapter);

            break;

        case 2:

            databaseAccess.open();
            mChapterEntries = databaseAccess.getEntriesChapterThree();
            databaseAccess.close();
            mAdapter = new EntryAdapter(this, R.layout.entry_list_item, mChapterEntries);
            this.mListView.setAdapter(mAdapter);

            break;

        case 3:

            databaseAccess.open();
            mChapterEntries = databaseAccess.getEntriesChapterFour();
            databaseAccess.close();
            mAdapter = new EntryAdapter(this, R.layout.entry_list_item, mChapterEntries);
            this.mListView.setAdapter(mAdapter);

            break;

This is the custom adapter "EntryAdapter"....

    class EntryAdapter extends ArrayAdapter<String> {

    public EntryAdapter(Context context, int layout, List<String> 
    mChapterEntries) {
        super(context, R.layout.entry_list_item, mChapterEntries);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull 
    ViewGroup parent) {
        View listItem = convertView;
        int pos = position +1;
        if(listItem == null)
            listItem = 
   LayoutInflater.from(getContext()).inflate(R.layout.entry_list_item, 
   parent, false);
        TextView entryTextView = (TextView) 
   listItem.findViewById(R.id.chapterEntries);
        verseTextView.setText(String.valueOf(pos)+ mChapterEntries);

        return listItem;
    }
}

Solution

  • I fixed it by using by using this:

    verseTextView.setText(String.valueOf(pos)+ mChapterEntries.get(position));

    instead of this:

    verseTextView.setText(String.valueOf(pos)+ mChapterEntries);

    Hope that helps someone else.