al23dev
al23dev

Reputation: 855

simple cursor adapter query

I have created a SimpleCursorAdapter. I would like a way to compare two rows. lets say the columns are called realData pracData. If i want to compare two columns how would I write the statement to achieve this.

Below is my code for my SimpleCursorAdapter

  db = new DBAdapter(this);
    db.open();

 //   db.insertContact("how do you print hello world", "print 'hello world';", "print hello", "hello", 1);

  //  db.insertContact("what is x=2 and y=5", "5", "7", "2", 2);
    db.close();

    /*
     *  Open the same SQLite database
     *  and read all it's content.
     */
    db = new DBAdapter(this);
     db.open();

    Cursor cursor = db.getAllContacts();
    startManagingCursor(cursor);

    String[] from = new String[]{DBAdapter.question, DBAdapter.possibleAnsOne};

    int[] to = new int[]{R.id.label, R.id.TextView01};

    SimpleCursorAdapter cursorAdapter =
        new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

    listContent.setAdapter(cursorAdapter);

    db.close();

This is from the ArrayAdapter

if (s.startsWith("how do you print hello world") || s.startsWith("iPhone")
                || s.startsWith("Solaris")) {
            imageView.setImageResource(R.drawable.plus);

I want to do something similar but compare two rows in my database.

public class MyAdapter extends SimpleCursorAdapter  {
private Context context;
private int layout;
    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.layout = layout;
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
          // do your magic inhere :) the cursor will be at the correct row position
        System.out.println("please select a cursor");
        // int nameCol = c.getColumnIndex(People.NAME);
       //  ListView listContent = (ListView)findViewById(R.id.contentlist);
       //     ImageView imageView = (ImageView)findViewById(R.id.ImageView01);
         int nameCol = c.getColumnIndex(DBAdapter.question);
         System.out.println("What is the column index" +nameCol);
    }
}

Upvotes: 1

Views: 642

Answers (1)

JustDanyul
JustDanyul

Reputation: 14044

Extending the SimpleCursorAdapter and overriding bindView should work. I usually extend CursorAdapter, but try something à la

public class MyAdapter extends SimpleCursorAdapter {

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
          TextView label = view.findViewById(R.id.yourid);
          // and then do something with cursor.getString(columnindex);
    }
}

Upvotes: 1

Related Questions