Using Type_orientation to move quotes on the screen
I am making an app in android. I am trying to use Type_orientation to move
the quotes on the screen when the users move the phone. So when the user
move the phone to the left the next quote appears on the screen, and when
the users move the phone to the right the preivious quote appears on the
screen. I have put a limit that is 25. But my problem is that as soon as
the phone goes to position 25 all the quotes that are saved in database,
are appeared as a slide show. I only want to go to the next quote and not
to have all the quates appared as slideshow. What can I do? Can anyone
help me to solve this problem?
This is my class
package com.example.prova1;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class SlideQuote extends Activity implements SensorEventListener
{
//a TextView
private TextView quote;
private TextView author;
//the Sensor Manager
private SensorManager sManager;
float xLast,xCurrent;
int id;
int total;
float x;
boolean first=true;
String s1,s2;
Database quotedatabase = new Database(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_quote);
//get the TextView from the layout file
quote = (TextView) findViewById(R.id.textView3);
author = (TextView) findViewById(R.id.textView4);
id=1;
//get a hook to the sensor service
sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sManager.getSensorList(Sensor.TYPE_ORIENTATION).size()!=0){
Sensor s =sManager.getSensorList(Sensor.TYPE_ORIENTATION).get(o);
sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
total=quotedatabase.getQuotesCount();
quote.setText(s1);
author.setText(s2);
}
}
//when this Activity starts
@Override
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
callbacks defined in this class, and gather the sensor information as quick
as possible*/
sManager.registerListener(this,
sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL);
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener
sManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
@Override
public void onSensorChanged(SensorEvent event)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
if(first){
xLast=0;
}
else
{
xLast=xCurrent;
}
//else it will output the Roll, Pitch and Yawn values
xCurrent=event.values[2];
x=xCurrent-xLast;
if(-1<xCurrent& xCurrent<1)
{
}
else{
if(x<=5&x>=-5){
}
else{
if(x>25 ){
if(id==total)
{id=1;
}
else{
id++;
}
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
quote.setText(s1);
author.setText(s2);
}
else{
if(x<-25){
if(id==1)
{id=total;}
else{
id--;}
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
quote.setText(s1);
author.setText(s2);
}
}
}
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
sManager.unregisterListener(this);
Intent backIntent = new Intent(getApplication(), Quote.class);
finish();
startActivity(backIntent);
}
}
And I think that the proble is at this part
@Override
public void onSensorChanged(SensorEvent event)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
if(first){
xLast=0;
}
else
{
xLast=xCurrent;
}
//else it will output the Roll, Pitch and Yawn values
xCurrent=event.values[2];
x=xCurrent-xLast;
if(-1<xCurrent& xCurrent<1)
{
}
else{
if(x<=5&x>=-5){
}
else{
if(x>25 ){
if(id==total)
{id=1;
}
else{
id++;
}
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
quote.setText(s1);
author.setText(s2);
}
else{
if(x<-25){
if(id==1)
{id=total;}
else{
id--;}
s1= quotedatabase.getQuote(id);
s2= quotedatabase.getAuthor(id);
quote.setText(s1);
author.setText(s2);
}
}
}
}
}
Can anyone help me please?
No comments:
Post a Comment