在自定义列表视图中保存编辑框值

时间:2013-03-15 09:20:30

标签: android listview

我有两个编辑框。输入的值必须放在自定义列表视图中才能正常工作...

但我希望永久保存这些值在列表视图中。如果手机关闭并再次运行应用程序,那么必须保存值..类似电话联系号码和名称必须保存.. 这里是代码

public class Editcard extends Activity{

    EditText cardname,cardDescription ;

    MyApplication app;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editcredit);  

app = ((MyApplication) getApplicationContext());
 cardname =(EditText)findViewById(R.id.cardash);

 cardDescription =(EditText)findViewById(R.id.editdescription);

Button save =(Button)findViewById(R.id.save);
    save.setOnClickListener(new OnClickListener() {

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

              app.carddata =cardname.getText().toString();

                System.out.println("Gotcardname"+app.carddata);

                app.descriptiondata =cardDescription.getText().toString();

                System.out.println("gotDescription"+app.descriptiondata);


             app.arryList.add(app.carddata);
             app.arryList1.add(app.descriptiondata);             

            Intent saveIntent =new Intent(Editcard.this,Newcard.class);

            startActivity(saveIntent);  

        }
    });

     }
}

public class MyApplication extends Application{

    ArrayList<String> arryList = new ArrayList<String>();  
     ArrayList<String> arryList1 = new ArrayList<String>();  

    String carddata,descriptiondata,=null;  
}

public class Newcard extends Activity {
     MyAdapter adapter;

    ListView listView;
    LayoutInflater lay;
    MyApplication app;

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

setContentView(R.layout.newcard);
app = ((MyApplication) getApplicationContext());

TextView newcard =(TextView)findViewById(R.id.newcard);
newcard.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Intent card = new Intent(Newcard.this,Editcreditcard.class);        
        startActivity(card);

    }
});

listView = (ListView) findViewById(R.id.cardlist);

adapter =new MyAdapter(this, app.arryList,app.arryList1);
listView.setAdapter(adapter);
}


public class MyAdapter extends BaseAdapter {

Context context = null;
ArrayList<String> items= null;
ArrayList<String> items1= null;


public MyAdapter(Newcard newcard, ArrayList<String> items,
        ArrayList<String> items1) {
    // TODO Auto-generated constructor stub

    this.items = items;
    this.items1 = items1;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return items; 

    //return items.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View layout = null;
    TextView produ = null;  
    TextView desc = null;

        if (convertView == null) {

        lay = LayoutInflater.from(getApplicationContext());
        layout = lay.inflate(R.layout.customlist, null);
    } else {
        layout = convertView;
    }
    produ = (TextView) layout.findViewById(R.id.card);
    produ.setText("" +app.arryList.get(position));

    desc= (TextView) layout.findViewById(R.id.descrip);
    desc.setText("" +app.arryList1.get(position));

    return layout;

}
}
}

1 个答案:

答案 0 :(得分:5)

use可以使用SharedPreferences,这将永久保存您的值,直到它们被更改(通过更改编辑框中的值)或用户重新安装(清除数据)应用程序。 Shared Preferences就像这样工作

// save string in sharedPreferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("some_key", string); // here string is the value you want to save
                    editor.commit();                    

// restore string in sharedPreferences 
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
string = settings.getString("some_key", "");