0

我正在尝试创建一个简单的应用程序,该应用程序基本上是跟踪不跟踪的跟踪器。每个科目的大学生课程。该应用程序特定于我的大学时间表。这个想法是我有3个活动:main.java,Sublist.java和editcrap.java。 main.java充当启动画面并启动Sublist活动。在内部存储器中存储用户输入数据

在子列表活动中,用户显示的布局显示(TextView)(Button)(Counter_TextView)水平相对于彼此。有7个这些垂直对齐。

当单击菜单按钮时出现(编辑主题参数),单击此按钮将使用户进行editcrap.java活动,其中在对应的EditText框中输入用户输入,为每个相应的(TextView)请求主题名称, Sublist活动中与(Counter_TextView)相对应的类的总数。在点击确定按钮时,数据被传递给子列表活动以进行显示和操作。

完成此操作后,我需要一种方法来存储数据,以便在下次打开应用程序时它将保留其以前的字符串,并且不会保留其以前的字符串。的类值。这是我正在运行的强制关闭错误或没有保留数据错误的地方。这里是我的代码...请某人告诉我wat m doin错了?我一直在努力挣扎几天:)我基本上需要应用程序来维护2个文件之一包含字符串和其他数字,需要读取并显示在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被反射:

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

为什么不使用预共享ferences? –

+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"; 

以下中的onCreate代码()方法

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

这里你偏好值EDITTEXT ...(如果你有以前保存首)

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); 

以下复选框Click事件代码...(这里保存首选项)

   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(); 

了解更多信息click here。而here

+0

非常感谢:) – zanazaar

+0

欢迎:) –