0

I am trying my hands on a hymnal app and I have used SQLiteAssetHelper and SimpleCursorAdapter. I have been able to load the title of the hymns in ListView using ListFragement. I am stuck with the implementation of onListItemClick in the ListFragment. Can someone, please, help me with it?. Thanks. Below is my code;

public class HymnsFragment extends ListFragment {

private Cursor hymns;
private DataBase db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new DataBase(getContext());
    hymns = db.getHymns();// you would not typically call this on the main thread

    ListAdapter adapter = new SimpleCursorAdapter(getContext(),
            android.R.layout.simple_list_item_2,
            hymns,
            new String[] {"_id","title"}, //table values
            new int[] {android.R.id.text1,android.R.id.text2});

    setListAdapter(adapter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hymns.close();
    db.close();
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // WHAT TO DO HERE TO GET VALUE FROM DATABASE TO DISPLAY IN TEXTVIEW?
}
} 

And my database;

public class DataBase extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "mydatabase";
private static final int DATABASE_VERSION = 1;

public DataBase(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public Cursor getHymns() {

    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String [] sqlSelect = {"0 _id","_id","title"};
    String sqlTables = "hymns";

    qb.setTables(sqlTables);
    Cursor c = qb.query(db, sqlSelect, null, null,
            null, null, null);

    c.moveToFirst();
    return c;

}
}

Thanks for helping

HazardPJ
  • 25
  • 7

1 Answers1

0

First, you need to get the selected item, then retrieve its datas by the Cursor object. Something as follows should get the expected behaviour:

@Override
public void onListItemClick(ListView listview, View view, int position, long id) {
    super.onListItemClick(listview, view, position, id);

    // get the item selected directly here
    Cursor c = (Cursor) listview.getAdapter().getItem(position);

    // get specific hymn text from database item result
    String hymnText = c.getString(c.getColumnIndex("hymn_text"));
    // or whatever you want to retrieve from database: authors, songs...
    String hymnAuthor = c.getString(c.getColumnIndex("hymn_author"));

    // do some stuff with the previous values as updating a TextView...
}

No need to do a request again, just use the existing Cursor.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • @ Fllo sorry to bother you again but my `TextView` seems not to work. This is what I did `TextView textView = (TextView) findViewById(R.id.textView1); textView.setText(hymnText); return;` What's the right thing to do? Thanks! – HazardPJ Jan 12 '16 at 21:16
  • Where do you call the textview? If it's in listfragment, you have to attach the view (layout) to find its id as `getView().findViewById(...)`. Then, post a logcat as `Log.v("", "HymnText is "+hymnText);` above the `setText()` method to see what returned `hymnText` in console. And what's the purpose of `return`? – Blo Jan 12 '16 at 21:47
  • @ Fllo I added the column name to `String [] sqlSelect = {"0 _id","_id","title"};` in my `DataBase.java` so it doesn't give me the `column -1` exception anymore. This is what is says now `java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference`. Please look at it for me. Thanks! – HazardPJ Jan 13 '16 at 12:48
  • As I can see @HazardPJ, your textview is not attached to the layout called in setContentView. You should create a TextView in xml inside hymn_text.xml and use id tag. Then, instead of create a new textview retrieve it by findViewById and you'll not have a nullpointerexception on this. – Blo Jan 15 '16 at 10:37
  • Ok @Fllo, I have done that but how exactly do I link `DisplayHymns.java` which is this code `public class DisplayHymns extends Activity { TextView textView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hymn_text); textView = (TextView)findViewById(R.id.hymnView); } }` to my `onListItemClick` code in `HymnsFragment.java`? – HazardPJ Jan 15 '16 at 11:13
  • @HazardPJ When you get the right string in `onListItemClick`, you can [pass it to the new activity with Bundle](http://stackoverflow.com/a/6707951/2668136). And retrieve it inside `onCreate` and use it into `setText()` – Blo Jan 15 '16 at 11:15
  • 1
    Does [this thread](http://stackoverflow.com/a/18146745/2668136) help you? And this you have [the official documentation](http://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent). – Blo Jan 15 '16 at 11:29
  • @HazardPJ I hope you'll get it! BTW, do a long-long conversation in comments are not very encouraged :oS We should remove the must unecessary comments ;) – Blo Jan 15 '16 at 11:39
  • Oh great, @HazardPJ! Glad to here that! Good luck for the next step. – Blo Jan 15 '16 at 12:43
  • Was wondering if I could take lessons from you when I'm in need. Will you be available? – HazardPJ Jan 15 '16 at 12:43
  • Yes, I can help. But there is also many devs in SO which can ;) Just post your questions @HazardPJ and you'll have many answers. I'll take a look on your future posts if I can. – Blo Jan 15 '16 at 12:59
  • Ok @Fllo. Thanks again! – HazardPJ Jan 15 '16 at 13:02