Android基础(数据操作)

本文介绍了Android应用程序中数据的多种存储方式,并展示了如何将数据存储到文件中并在界面上进行显示。

一、把数据存储到文件并取数据显示在界面上

在Android应用程序中的存储数据的方式

1、文件

2、使用SharedPreferences保存数据

3、使用SQLite数据库

4、使用ContentProvider内容提供者

5、通过网络把数据存储到服务器上


1、保存数据到文件上

代码:

a、布局文件

/**********************************************************************
 * 
 * A cube 'state' is a vector with 40 entries, the first 20
 * are a permutation of {0,...,19} and describe which cubie is at
 * a certain position (regarding the input ordering). The first
 * twelve are for edges, the last eight for corners.
 * 
 * The last 20 entries are for the orientations, each describing
 * how often the cubie at a certain position has been turned
 * counterclockwise away from the correct orientation. Again the
 * first twelve are edges, the last eight are corners. The values
 * are 0 or 1 for edges and 0, 1 or 2 for corners.
 * 
 **********************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

//----------------------------------------------------------------------

typedef vector vi;

//----------------------------------------------------------------------

int applicableMoves[] = { 0, 262143, 259263, 74943, 74898 };

// TODO: Encode as strings, e.g. for U use "ABCDABCD"

int affectedCubies[][8] = {
  {  0,  1,  2,  3,  0,  1,  2,  3 },   // U
  {  4,  7,  6,  5,  4,  5,  6,  7 },   // D
  {  0,  9,  4,  8,  0,  3,  5,  4 },   // F
  {  2, 10,  6, 11,  2,  1,  7,  6 },   // B
  {  3, 11,  7,  9,  3,  2,  6,  5 },   // L
  {  1,  8,  5, 10,  1,  0,  4,  7 },   // R
};

vi applyMove ( int move, vi state ) {
  int turns = move % 3 + 1;
  int face = move / 3;
  while( turns-- ){
    vi oldState = state;
    for( int i=0; i<8; i++ ){
      int isCorner = i > 3;
      int target = affectedCubies[face][i] + isCorner*12;
      int killer = affectedCubies[face][(i&3)==3 ? i-3 : i+1] + isCorner*12;;
      int orientationDelta = (i<4) ? (face>1 && face<4) : (face<2) ? 0 : 2 - (i&1);
      state[target] = oldState[killer];
      //state[target+20] = (oldState[killer+20] + orientationDelta) % (2 + isCorner);
      state[target+20] = oldState[killer+20] + orientationDelta;
      if( !turns )
    state[target+20] %= 2 + isCorner;
    }
  }
  return state;
}

int inverse ( int move ) {
  return move + 2 - 2 * (move % 3);
}

//----------------------------------------------------------------------

int phase;

//----------------------------------------------------------------------

vi id ( vi state ) {
  
  //--- Phase 1: Edge orientations.
  if( phase < 2 )
    return vi( state.begin() + 20, state.begin() + 32 );
  
  //-- Phase 2: Corner orientations, E slice edges.
  if( phase < 3 ){
    vi result( state.begin() + 31, state.begin() + 40 );
    for( int e=0; e<12; e++ )
      result[0] |= (state[e] / 8) << e;
    return result;
  }
  
  //--- Phase 3: Edge slices M and S, corner tetrads, overall parity.
  if( phase < 4 ){
    vi result( 3 );
    for( int e=0; e<12; e++ )
      result[0] |= ((state[e] > 7) ? 2 : (state[e] & 1)) << (2*e);
    for( int c=0; c<8; c++ )
      result[1] |= ((state[c+12]-12) & 5) << (3*c);
    for( int i=12; i<20; i++ )
      for( int j=i+1; j<20; j++ )
	result[2] ^= state[i] > state[j];
    return result;
  }
  
  //--- Phase 4: The rest.
  return state;
}

//----------------------------------------------------------------------

int main ( int argc, char** argv ) {
  
  //--- Define the goal.
  string goal[] = { "UF", "UR", "UB", "UL", "DF", "DR", "DB", "DL", "FR", "FL", "BR", "BL",
		    "UFR", "URB", "UBL", "ULF", "DRF", "DFL", "DLB", "DBR" };
  
  //--- Prepare current (start) and goal state.
  vi currentState( 40 ), goalState( 40 );
  for( int i=0; i<20; i++ ){
    
    //--- Goal state.
    goalState[i] = i;
    
    //--- Current (start) state.
    string cubie = argv[i+1];
    while( (currentState[i] = find( goal, goal+20, cubie ) - goal) == 20){
      cubie = cubie.substr( 1 ) + cubie[0];
      currentState[i+20]++;
    }
  }
  
  //--- Dance the funky Thistlethwaite...
  while( ++phase < 5 ){
    
    //--- Compute ids for current and goal state, skip phase if equal.
    vi currentId = id( currentState ), goalId = id( goalState );
    if( currentId == goalId )
      continue;
    
    //--- Initialize the BFS queue.
    queue q;
    q.push( currentState );
    q.push( goalState );
    
    //--- Initialize the BFS tables.
    map predecessor;
    map direction, lastMove;
    direction[ currentId ] = 1;
    direction[ goalId ] = 2;
    
    //--- Dance the funky bidirectional BFS...
    while( 1 ){
      
      //--- Get state from queue, compute its ID and get its direction.
      vi oldState = q.front();
      q.pop();
      vi oldId = id( oldState );
      int& oldDir = direction[oldId];
      
      //--- Apply all applicable moves to it and handle the new state.
      for( int move=0; move<18; move++ ){
	if( applicableMoves[phase] & (1 << move) ){
	  
	  //--- Apply the move.
	  vi newState = applyMove( move, oldState );
	  vi newId = id( newState );
	  int& newDir = direction[newId];
	  
	  //--- Have we seen this state (id) from the other direction already?
	  //--- I.e. have we found a connection?
	  if( newDir  &&  newDir != oldDir ){
	    
	    //--- Make oldId represent the forwards and newId the backwards search state.
	    if( oldDir > 1 ){
	      swap( newId, oldId );
	      move = inverse( move );
	    }
	    
	    //--- Reconstruct the connecting algorithm.
	    vi algorithm( 1, move );
	    while( oldId != currentId ){
	      algorithm.insert( algorithm.begin(), lastMove[ oldId ] );
	      oldId = predecessor[ oldId ];
	    }
	    while( newId != goalId ){
	      algorithm.push_back( inverse( lastMove[ newId ] ));
	      newId = predecessor[ newId ];
	    }
	    
	    //--- Print and apply the algorithm.
	    for( int i=0; i<(int)algorithm.size(); i++ ){
	      cout << "UDFBLR"[algorithm[i]/3] << algorithm[i]%3+1;
	      currentState = applyMove( algorithm[i], currentState );
	    }
	    
	    //--- Jump to the next phase.
	    goto nextPhasePlease;
	  }
	  
	  //--- If we've never seen this state (id) before, visit it.
	  if( ! newDir ){
	    q.push( newState );
	    newDir = oldDir;
	    lastMove[ newId ] = move;
	    predecessor[ newId ] = oldId;
	  }
	}
      }
    }
  nextPhasePlease:
    ;
  }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">


<EditText

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:id="@+id/et_username"

    android:hint="请输入用户名"

/>

<EditText

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:id="@+id/et_pwd"

    android:inputType="textPassword"

    android:hint="请输入密码"

/>

<Button

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="保存"

    android:onClick="save"

/>

/**************************************************/

b、源代码  MainActivity.java

public class MainActivity extends Activity

{

    private EditText et_username;//做声明

    private EditText et_pwd;


    protected void onCreate(Bundle savedInstanceState)

    {

           //调用父类的构造方法

        super.onCreate(savedInstanceState);

        //设置当前Activity显示内容为main.xml布局

        setContentView(R.layout.activity_main);


        et_username=(EditText)findViewById(R.id.et_username);

        et_pwd=(EditText)findViewById(R.id.et_pwd);

        try{

            //从文件中读取数据

            File file=new File("/data/data/package01/files/info.text");

            FileReader fr=new FileReader(file);

            BufferedReader br=new BufferedReader(fr);

            String info=br.readLine();


            //把数据显示在输入框中

            if(!TextUtils.isEmpty(info))

            {

                String[] array=info.split("##");

                String username=array[0];


                String pwd=array[1];

                et_username.setText(username);

                et_pwd.setText(pwd);

            }

        }catch(Exception e){

            e.printStackTrace();

        }

    }

}

public void save(View view){

    String username=et_username.getText().toString().trim();

    String pwd=et_pwd.getText().toString().trim();


    if(TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)){

        Tost.makeText(this,"用户名或者密码不存在",Toast.LENGTH_SHORT).show();

        return;

    }else{

        //把用户名和密码保存到文件中

        FileOutputStream fos=this.openFileOutput("info.text",Context.MODE_PRIVATE);

        fos.write((username+"##"+pwd).getBytes());

        fos.close;

    }catch(Exception e){

        e.printStackTrace();

    }

}





































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值