0

기본적으로 아니오를 추적하는 추적기 인 간단한 앱을 만들려고합니다. 대학생을위한 각 과목 별 수업 이 앱은 대학 시간표에 따라 다릅니다. 아이디어는 내가 main.java, Sublist.java 및 editcrap.java라는 3 가지 활동을 가지고 있습니다. main.java는 스플래시 화면으로 작동하여 하위 목록 활동을 시작합니다.내부 메모리에 사용자 입력 데이터 저장

하위 작업에서 사용자는 (TextView) (버튼) (Counter_TextView)를 서로에 대해 가로로 표시하는 레이아웃으로 표시됩니다. 이 중 7 개가 수직으로 정렬되어 있습니다.

메뉴 버튼을 클릭하면 : (Edit Subject Parameters)를 클릭하면 사용자 입력이있는 editcrap.java 활동이 해당 EditText 상자의 제목 이름을 묻는 해당 EditText 상자에서 취해집니다. 하위 작업의 (Counter_TextView)에 해당하는 총 클래스 수입니다. OK 버튼을 클릭하면 표시 및 조작을 위해 데이터가 하위 작업 활동으로 전달됩니다.

데이터를 저장하는 방법이 필요했습니다. 다음 번에 앱을 열면 이전 문자열과 아니요가 유지됩니다. 클래스 값의 이것은 내가 강제로 닫기 오류 또는 데이터 오류 없음 유지로 실행 해요. 여기에 내 코드가 있습니다 ... 누군가가 내게 와트 m 나 잘못 말해 주시겠습니까? 나는 이걸로 며칠 동안 고민했습니다 :) 기본적으로 2 개의 파일을 하나의 문자열과 다른 Numbers가 포함 된 Sublist.java 액티비티에서 읽거나 표시해야 할 필요가있는 앱이 필요합니다. 변경 사항은 앱에서 수행해야합니다. 사용자가 잘

//Sublist.java: 


package com.shuaib669.bunkrecord; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

public class Sublist extends Activity{ 

double[] no_of_classes = new double[7]; 
int count[]= new int[7]; 
double cutOff = 0.3; 
String[] newText = new String[7]; 
String[] newNum = new String[7]; 
String countString = null; 
TextView subject[] = new TextView[7]; 
TextView counter[] = new TextView[7]; //sub11 is counter text view label 
Button button[] = new Button[7];  //button1 is the increment button 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Assigning Views to objects. 
      subject[0] = (TextView) findViewById(R.id.textView1); 
      counter[0] = (TextView) findViewById(R.id.counter1); 
      button[0] = (Button) findViewById(R.id.button1); 

      subject[1] = (TextView) findViewById(R.id.textView2); 
      counter[1] = (TextView) findViewById(R.id.counter2); 
      button[1] = (Button) findViewById(R.id.button2); 

      subject[2] = (TextView) findViewById(R.id.textView3); 
      counter[2] = (TextView) findViewById(R.id.counter3); 
      button[2] = (Button) findViewById(R.id.button3); 

      subject[3] = (TextView) findViewById(R.id.textView4); 
      counter[3] = (TextView) findViewById(R.id.counter4); 
      button[3] = (Button) findViewById(R.id.button4); 

      subject[4] = (TextView) findViewById(R.id.textView5); 
      counter[4] = (TextView) findViewById(R.id.counter5); 
      button[4] = (Button) findViewById(R.id.button5); 

      subject[5] = (TextView) findViewById(R.id.textView6); 
      counter[5] = (TextView) findViewById(R.id.counter6); 
      button[5] = (Button) findViewById(R.id.button6); 

      subject[6] = (TextView) findViewById(R.id.textView7); 
      counter[6] = (TextView) findViewById(R.id.counter7); 
      button[6] = (Button) findViewById(R.id.button7); 

      try { 
     // open the file for reading 

     DataInputStream in= new DataInputStream(openFileInput(getFilesDir() + "/" + "subject.txt")); 
     // if file the available for reading 
     if (in!= null) { 
      // prepare the file for reading 
      String line; 
      int x=0; 
      // read every line of the file into the line-variable, on line at the time 
      while(in.readLine() != null) { 
       // do something with the strings from the file 

       line=DataInputStream.readUTF(in); 
       subject[x].setTextColor(Color.BLACK); 
       subject[x].setText(line); 
       x+=1; 

      } 

     } 

     // close the file again 
     in.close(); 
     } 
    catch (Exception e) { 
     e.printStackTrace();// do something if the myfilename.txt does not exits 
     } 

    button[0].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[0]>=(no_of_classes[0]*cutOff)){ 
       counter[0].setTextColor(Color.RED); 
       countString = "" +(++count[0]);     //Convert from int to String to set in your textview:: 
       counter[0].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[0]);     //Convert from int to String to set in your textview:: 
       counter[0].setText(countString); 
      } 


     } 
    }); 

    button[1].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[1]>=(no_of_classes[1]*cutOff)){ 
       counter[1].setTextColor(Color.RED); 
       countString = "" +(++count[1]);     //Convert from int to String to set in your textview:: 
       counter[1].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[1]);     //Convert from int to String to set in your textview:: 
       counter[1].setText(countString); 
      } 


     } 
    }); 

    button[2].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[2]>=(no_of_classes[2]*cutOff)){ 
       counter[2].setTextColor(Color.RED); 
       countString = "" +(++count[2]);     //Convert from int to String to set in your textview:: 
       counter[2].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[2]);     //Convert from int to String to set in your textview:: 
       counter[2].setText(countString); 
      } 


     } 
    }); 

    button[3].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[3]>=(no_of_classes[3]*cutOff)){ 
       counter[3].setTextColor(Color.RED); 
       countString = "" +(++count[3]);     //Convert from int to String to set in your textview:: 
       counter[3].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[3]);     //Convert from int to String to set in your textview:: 
       counter[3].setText(countString); 
      } 


     } 
    }); 

    button[4].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[4]>=(no_of_classes[4]*cutOff)){ 
       counter[4].setTextColor(Color.RED); 
       countString = "" +(++count[4]);     //Convert from int to String to set in your textview:: 
       counter[4].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[4]);     //Convert from int to String to set in your textview:: 
       counter[4].setText(countString); 
      } 


     } 
    }); 

    button[5].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[5]>=(no_of_classes[5]*cutOff)){ 
       counter[5].setTextColor(Color.RED); 
       countString = "" +(++count[5]);     //Convert from int to String to set in your textview:: 
       counter[5].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[5]);     //Convert from int to String to set in your textview:: 
       counter[5].setText(countString); 
      } 


     } 
    }); 

    button[6].setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(count[6]>=(no_of_classes[6]*cutOff)){ 
       counter[6].setTextColor(Color.RED); 
       countString = "" +(++count[6]);     //Convert from int to String to set in your textview:: 
       counter[6].setText(countString); 
      } 
      else{ 
       countString = "" +(++count[6]);     //Convert from int to String to set in your textview:: 
       counter[6].setText(countString); 
      } 


     } 
    }); 



} 

public boolean onCreateOptionsMenu(Menu menu){  // What the MENU button does. 
    super.onCreateOptionsMenu(menu); 
    MenuInflater castle = getMenuInflater(); 
    castle.inflate(R.menu.main_menu, menu); 
    return(true); 
} 


public boolean onOptionsItemSelected(MenuItem item){ // Opens Options of MENU. 

    switch(item.getItemId()){ 

    case R.id.editcrap: startActivityForResult((new Intent("com.shuaib669.bunkrecord.EDITCRAP")), 1); 
         return(true); 
    } 

return(false); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
// TODO Auto-generated method stub 
super.onActivityResult(requestCode, resultCode, data); 
switch(requestCode){ 

case 1: if(resultCode==Activity.RESULT_OK){ 

     newText = data.getStringArrayExtra("com.shuaib669.bunkrecord.thetext"); 
     newNum = data.getStringArrayExtra("com.shuaib669.bunkrecord.thenum"); 

     try { 
       // open myfilename.txt for writing 
     DataOutputStream out = new DataOutputStream(openFileOutput(getFilesDir() + "/" + "subject.txt", Context.MODE_PRIVATE));  
     //newNum = data.getIntArrayExtra("com.shuaib669.thenum"); 
     //for loop to setText in the TextViews of main.xml 
     for(int x=0;x<7;x++){ 



        subject[x].setTextColor(Color.BLACK); 
        subject[x].setText(newText[x]); 
        // write the contents on mySettings to the file 
        out.writeUTF(newText[x]); 

        try{ 
         no_of_classes[x]=Integer.parseInt(newNum[x]); 
         } 
         catch(Exception nfe){ 
          nfe.printStackTrace(); 
         }   
        // close the file 
        out.close(); 
        } 
     } 


     catch (Exception e) { 
        Log.i("Data Input Sample", "I/O Error"); //do something if an Exception occurs. 
       } 



      } 


break; 
}            


} 
    } 

//editcrap.java로 원본 파일에게 반영하기 :에서 onCreate 코드 다음

package com.shuaib669.bunkrecord; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 


public class editcrap extends Activity{ 

EditText sub[] = new EditText[7];   //list of subject text edit labels. 
Button parambutton1;           //OK buttons for edit list. 
EditText num[] = new EditText[7];   //list of objects of total no. of classes bunked edit text labels. (boinkers i know) 
String theText[] = new String[7]; 
String theNum[] = new String[7]; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.params); 

    sub[0] = (EditText) findViewById(R.id.peditText1);   //pedittext is the parameter menu edit text label 
    num[0] = (EditText) findViewById(R.id.pnumText1);   //EditText label for takin in total no. of classes for 1 subject 

    sub[1] = (EditText) findViewById(R.id.peditText2); 
    num[1] = (EditText) findViewById(R.id.pnumText2); 

    sub[2] = (EditText) findViewById(R.id.peditText3); 
    num[2] = (EditText) findViewById(R.id.pnumText3); 

    sub[3] = (EditText) findViewById(R.id.peditText4); 
    num[3] = (EditText) findViewById(R.id.pnumText4); 

    sub[4] = (EditText) findViewById(R.id.peditText5); 
    num[4] = (EditText) findViewById(R.id.pnumText5); 

    sub[5] = (EditText) findViewById(R.id.peditText6); 
    num[5] = (EditText) findViewById(R.id.pnumText6); 

    sub[6] = (EditText) findViewById(R.id.peditText7); 
    num[6] = (EditText) findViewById(R.id.pnumText7); 

    parambutton1 = (Button) findViewById(R.id.parambutton1); //pbutton1 is the ok button to accept the input. 

    parambutton1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 


      for(int x=0;x<7;x++){ 


        theText[x] = sub[x].getText().toString(); 
        theNum[x] = num[x].getText().toString(); 
        //theNum[x] = Integer.parseInt(num[x].getText().toString()); 

       } 



      Intent data = new Intent(editcrap.this, Sublist.class); 
      data.putExtra("com.shuaib669.bunkrecord.thetext", theText); 
      data.putExtra("com.shuaib669.bunkrecord.thenum", theNum); 
      setResult(Activity.RESULT_OK, data); 
      finish(); 

     } 
    }); 


} 




} 
+1

을 .and 무슨 일이야? –

+0

왜 SQLite 데이터베이스를 사용하지 않습니까? 또는 SharedPreference를 수행 할 수 있습니다. 이것이 정확히 무엇인지 알 수 있습니다. – Nicholas

+0

괜찮을거야 ... :)) 제안을 주셔서 감사합니다 :) – zanazaar

답변

2

사용하여 공유 환경 설정

public static final String PREFS_NAME = "MyPrefsFile"; 
private static final String PREF_USERNAME = "username"; 
private static final String PREF_PASSWORD = "password"; 

() (당신이 이전의 저장 환경 설정이있는 경우) 여기 방법

EtUserName=(EditText) findViewById(R.id.EditText01); 
EtPassword=(EditText) findViewById(R.id.EditText02); 

당신은 (여기에 환경 설정을 저장)

... 체크 박스를 클릭 이벤트에 다음 코드를

SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); 
String username = pref.getString(PREF_USERNAME, null); 
String password = pref.getString(PREF_PASSWORD, null); 

EtUserName.setText(username); 
EtPassword.setText(password); 

... EDITTEXT에 기본 설정 값을 가져 자세한 내용은 click here에 대한

   String us,pa; 
     us=EtUserName.getText().toString(); 
     pa=EtPassword.getText().toString(); 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); 
         getSharedPreferences(PREFS_NAME,MODE_PRIVATE) 
         .edit() 
         .putString(PREF_USERNAME, us) 
         .putString(PREF_PASSWORD, pa) 
         .commit(); 

왜 공유 사전을 사용하지 here

+0

덕분에 많은 사람 :) – zanazaar

+0

환영합니다 :) –

관련 문제