开发者

how to change the text of the texview in android

开发者 https://www.devze.com 2023-04-11 03:48 出处:网络
Hi i am making simple quote application which display quotes. I have the quotes in sqlite database. I have two button \"next\" and \"previous\"

Hi i am making simple quote application which display quotes. I have the quotes in sqlite database. I have two button "next" and "previous" when user hit next the next quote should display and and when hit previous the previous quotes should display.Database is working fine but i am stuck at how to change the text of textV开发者_C百科iew.I tried some things but they are not working properly. Any suggestion or help? quotes like jokes,proverbs etc.


firstly fetch all the quotes and store into the Arraylist or any equivalent object now use one counter object which will track the current position and initialize it with 0 if you get the quote more than equals to 1

now on the next button check first counter value is equals to the arraylist size then do nothing otherwise increase by 1 and get the next quote from the arraylist

if(counter==-1)
    return;

if(counter==array.size()-1)
   return

counter++;
yourtextview.setText(array.get(counter));

do the same things into the previous, in this counter-- instead of counter++

Edit without arraylist object and counter object

check the cursor using moveNext() and movePrevious() like for next check

   if(cursor.moveNext())
       yourtextview.setText(coursor.getString(yourcolumnindex));

and in previous

   if(cursor.movePrevious())
       yourtextview.setText(coursor.getString(yourcolumnindex));


Take a look at http://developer.android.com/reference/android/widget/TextView.html

Use onClickListeners on your buttons. When the user presses the button you fetch the appropriate text from the database and display it for example by using setText(CharSequence text)


To change the text of textview:

textview.setText("your text");

if you are getting any exception in changing the text then try to do it with the help of handler.


I m tring this it is working but when i click next the previous text does not disappear.what to do?

           public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView)findViewById(R.id.text);
    next=(Button)findViewById(R.id.next);
    back=(Button)findViewById(R.id.back);
 next.setOnClickListener(this);
 back.setOnClickListener(this);

    try {
        db=new DbH(this);
    } catch (IOException e2) {

        e2.printStackTrace();
    }


        try {
            db.createdatabase();
        } catch (IOException e) {

            e.printStackTrace();
        }

    db.opendatabase();
     cur=db.data();
     cur.moveToFirst();


 //int i=0;

    tv.append(cur.getString(1));

 cur.moveToNext();

// cur.close();

} @Override public void onClick(View v) {

switch(R.id.next)
{
case R.id.next :
    cur.moveToNext();
    tv.append(""+cur.getString(1));
break;
case R.id.back:
{
    cur.moveToPrevious();
    tv.append(""+cur.getString(1));
    break;
}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号