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 8524255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:38:13+00:00 2026-06-11T07:38:13+00:00

I’m trying to display string on the textview. I’m succesfully able to print it

  • 0

I’m trying to display string on the textview. I’m succesfully able to print it on the console from database, but I’m not able to figure out how to print all the strings on different different textviews. Here is my code:

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

EditText search;
Button insert;
TextView txt1, txt2, txt3, txt4, txt5;
DatabaseHandler db;
List<History> history;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DatabaseHandler(this);

    search = (EditText) findViewById(R.id.search_word);
    insert = (Button) findViewById(R.id.insert);
    txt1 = (TextView) findViewById(R.id.txt1);
    txt2 = (TextView) findViewById(R.id.txt2);
    txt3 = (TextView) findViewById(R.id.txt3);
    txt4 = (TextView) findViewById(R.id.txt4);
    txt5 = (TextView) findViewById(R.id.txt5);

    insert.setOnClickListener(this);

    history = db.getAllHistory();
}

public void onClick(View v) {
    db.addHistory(new History(search.getText().toString(), null));
    Toast.makeText(getApplicationContext(),
            "Inserted: " + search.getText().toString(), Toast.LENGTH_LONG)
            .show();
}

@Override
protected void onStart() {
    super.onStart();
    List<History> history = db.getAllHistory();
    for (History cn : history) {
        String log = "Search Strings: " + cn.getName();
        Log.d("Search Strings: ", log);
    }
}
}

This is my activity in which I’m bringing my all database value on onStart() function. Now here I have to set all the data coming from database on the textview. Here is my DabaseHandler class in which I’m taking out each row.

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "historyManager";

private static final String TABLE_HISTORY = "histories";

private static final String KEY_NAME = "history";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "("
            + KEY_NAME + " TEXT" + ")";
    db.execSQL(CREATE_HISTORY_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);

    onCreate(db);
}

void addHistory(History history) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, history.getName());

    db.insert(TABLE_HISTORY, null, values);
    db.close();
}

History getHistory(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_HISTORY, new String[] { KEY_NAME },
            "=?", new String[] { String.valueOf(id) }, null, null, null,
            null);
    if (cursor != null)
        cursor.moveToFirst();

    History history = new History(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));
    return history;
}

public List<History> getAllHistory() {
    List<History> historyList = new ArrayList<History>();

    String selectQuery = "SELECT  * FROM " + TABLE_HISTORY;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            History contact = new History();
            contact.setName(cursor.getString(0));
            historyList.add(contact);
        } while (cursor.moveToNext());
    }

    return historyList;
}

public int updateHistory(History history) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, history.getName());
    return db.update(TABLE_HISTORY, values, KEY_NAME + " = ?",
            new String[] { String.valueOf(history.getName()) });
}

public void deleteHistory(History history) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_HISTORY, KEY_NAME + " = ?",
            new String[] { String.valueOf(history.getName()) });
    db.close();
}

public int getHistoryCount() {
    String countQuery = "SELECT  * FROM " + TABLE_HISTORY;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}
}

Please help in getting data printed on the textview. On the Log.d I can see all my data coming, one after another. But I’m not able to print all the data.

  • 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-11T07:38:15+00:00Added an answer on June 11, 2026 at 7:38 am

    It’s answer for “Thank You for that. Can you tell me how to set the data which I have printed in MainActivity on onStart() method (Log.d(“”)). If you can give me the code for that, that will be much easier for me.”
    try this:

    List<String> listNames = new ArrayList<String>();//global variable
    
    List<History> history = db.getAllHistory();
        for (History cn : history) {
             listNames.add(cn.getName());
        }
    

    or, if have in History field date try is, after easy will sort:

    Map<String, Date> historyMap = new HashMap<String, Date>();
        List<History> history = db.getAllHistory();
            for (History cn : history) {
                 historyMap.put(cn.getName, cn.getDate);
            }
    
    • 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
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a view passing on information from a database: def serve_article(request, id): served_article
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.