关闭expandView后,EditText消失

时间:2014-01-05 17:01:54

标签: android

我遇到的问题是我在展开视图中输入的文本未保存。当我关闭一个组打开另一个组时,第一个保存的所有值都消失了。任何人都可以告诉我该怎么做,所以它可以保存,不仅是因为我打开expandView时,还有当我重新启动它或恢复它时。我不知道如何使用onResume和onRestart函数。感谢

我也附上了代码:

    import android.content.Context;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseExpandableListAdapter
{
    private int counter;
    SharedPreferences getPrefs;
    private Context context;
    String[] parent= new String [4];
    String[][] child= new String [4][4];
    String [][] weights= new String [4][4];

    boolean KU=false;
    boolean MC=false;
    boolean TI=false;
    boolean C=false;
    boolean allEdited=false;

    EditText et1;
    EditText et2;
    EditText et3;
    EditText et4;

    public MyAdapter(Context context) 
    {
        this.context=context;
    }

    @Override
    public Object getChild(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) 
    {
        counter++;

        if (!allEdited)
        {
            et1= new EditText (context);
            et2= new EditText (context);
            et3= new EditText (context);
            et4= new EditText (context);

            child[groupPosition][0]=et1.getText().toString();       
            child[groupPosition][1]=et2.getText().toString();
            child[groupPosition][2]=et3.getText().toString();
            child[groupPosition][3]=et4.getText().toString();
        }

        if(!KU)
        {
            et1.setText(child[groupPosition][childPosition]);
            et1.setPadding(50, 10, 10, 10);
            et1.setTextSize(15);
            et1.setTextColor(Color.WHITE);
            et1.setFocusable(true);
            et1.setInputType(0x00000002);
            weights[groupPosition][0]=(et1.getText().toString());

        }
        if (!et1.getText().toString().equals(""))
        {
            KU=true;
        }
        if(KU)
        {
            et1.setText(weights[groupPosition][0]);
            et1.setFreezesText(true);
        }

        if (!MC)
        {
            et2.setText(child[groupPosition][childPosition]);
            et2.setPadding(50, 10, 10, 10);
            et2.setTextSize(15);
            et2.setTextColor(Color.WHITE);
            et2.setFocusable(true);
            et2.setInputType(0x00000002);
            weights[groupPosition][1]=(et2.getText().toString());
        }
        else if (!et2.getText().toString().equals(""))
        {
            MC=true;
        }
        weights[groupPosition][1]=(et2.getText().toString());

        if(MC)
        {
            et1.setText(weights[groupPosition][1]);
        }


        if (!TI)
        {
            et3.setText(child[groupPosition][childPosition]);
            et3.setPadding(50, 10, 10, 10);
            et3.setTextSize(15);
            et3.setTextColor(Color.WHITE);
            et3.setFocusable(true);
            et3.setInputType(0x00000002);
            weights[groupPosition][2]=(et3.getText().toString());
        }

        else if (!et3.getText().toString().equals(""))
        {
            TI=true;
        }
        weights[groupPosition][2]=(et3.getText().toString());

        if(TI)
        {
            et3.setText(weights[groupPosition][2]);
        }

        if (!C)
        {
            et4.setText(child[groupPosition][childPosition]);
            et4.setPadding(50, 10, 10, 10);
            et4.setTextSize(15);
            et4.setTextColor(Color.WHITE);
            et4.setFocusable(true);
            et4.setInputType(0x00000002);
            weights[groupPosition][3]=(et4.getText().toString());
        }

        else if (!et4.getText().toString().equals(""))
        {
            C=true;
        }
        weights[groupPosition][3]=(et1.getText().toString());

        if(C)
        {
            et4.setText(weights[groupPosition][3]);
        }


        if (KU && MC && TI && C)
        {
            allEdited=true;
        }

        if (counter==1)
            return et1;
        else if (counter==2)
            return et2;
        else if (counter==3)
            return et3;
        else if (counter==4)
            return et4;
        else
            counter=0; return et1;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return child[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public int getGroupCount()
    {
        return parent.length;
    }

    @Override
    public long getGroupId(int groupPosition) 
    {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
    {
        TextView tv= new TextView (context);
        this.parent[groupPosition]=tv.getText().toString();

        getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        String allCourses= getPrefs.getString("allClasses", "Please enter your course code");

        String[] courses= allCourses.split(",");

        for (int i=0;i<courses.length;i++)
        {
            this.parent[i]=courses[i];
        }
        tv.setText(this.parent[groupPosition]);
        tv.setPadding(50, 10, 10, 10);
        tv.setTextSize(20);
        tv.setTextColor(Color.GREEN);

        return tv;
    }

2 个答案:

答案 0 :(得分:1)

你应该使用自定义列表适配器,每当你在textview中输入内容时,都要有一个列表并将其分配给特定索引(位置)的列表

答案 1 :(得分:0)

正如@Kapil所说,你可以使用list来存储值,但是一旦活动被销毁,这些值就会被销毁。如果您想要永久存储,即即使重新启动应用程序也应保留这些值,则应使用共享首选项 SQLite数据库