Question

Below I am using a simple cursor adapter to insert data from an SQLite database into a textview. Instead of passing all the columns into seperate labels I would like them all concatenated. How can I bind the strings together? (i.e get rid of label2 and have both the columns from the database displaying as one string in label)

private void fillData() {
  Cursor  cursor = getContentResolver().query(MyTaskContentProvider.CONTENT_URI, null, null, null,
            sort);

String[] from = new String[] { TaskTable.COLUMN_DESCRIPTION, TaskTable.COLUMN_DATE};

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

getLoaderManager().initLoader(0, null, this);

adapter = new SimpleCursorAdapter(this, R.layout.task_row, cursor, from,
    to, 0);

 adapter.setViewBinder(new CustomViewBinder());

 setListAdapter(adapter);

  }

  private class CustomViewBinder implements ViewBinder {

@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

final int descCol = cursor.getColumnIndex(TaskTable.COLUMN_DESCRIPTION);    
    if (columnIndex == descCol)
    {
        int is_desc = cursor.getInt(columnIndex);
        if(is_desc != 1)
        {

            TextView tv1 = (TextView) view.findViewById(R.id.label);
            view.setVisibility(View.GONE);
            tv1.append(cursor.getString(1));
            view.setVisibility(View.VISIBLE);
        }
        return true;
    }
    else
    {

        view.setVisibility(View.VISIBLE);

    return false;
}


 }
}
Was it helpful?

Solution

I believe this is the answer to the question you're asking.

You're looking for the concat method.

For instance:

String concatenatedString = new String().concat(string1+" "+string2);

Alternatively:

String string1 = "This";
String string2 = "is a concatenated string";
string1 = string1.concat(" " + string2);

string1 would now be "This is a concatenated string"

I believe in your instance you would want to do:

String from = new String().concat(TaskTable.COLUMN_DESCRIPTION + " " + TaskTable.COLUMN_DATE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top