保存在自定义列表视图编辑框的值 [英] save editbox values in the custom listview

查看:135
本文介绍了保存在自定义列表视图编辑框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个编辑框。该值在进入已放置在自定义列表视图此其工作罚款......

但我想,如果手机关闭在permanently.so的列表视图保存此值,并再次运行该应用程序,然后这些值必须保存..like电话联系号码和姓名必须保存..
我在这里ataching的code

 公共类Editcard延伸活动{    的EditText cardname,cardDescription;    所有MyApplication应用;@燮pressWarnings({未登记,rawtypes})
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.editcredit);应用=((所有MyApplication)getApplicationContext());
 cardname =(EditText上)findViewById(R.id.cardash); cardDescription =(EditText上)findViewById(R.id.editdescription);按钮保存=(按钮)findViewById(R.id.save);
    save.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根              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);            意图saveIntent =新意图(Editcard.this,Newcard.class);            startActivity(saveIntent);        }
    });     }
}公共类MyApplication的扩展应用{    ArrayList的<串GT; arryList =新的ArrayList<串GT;();
     ArrayList的<串GT; arryList1 =新的ArrayList<串GT;();    串carddata,descriptiondata,= NULL;
}公共类NEWCARD延伸活动{
     MyAdapter适配器;    ListView控件的ListView;
    LayoutInflater同寝。
    所有MyApplication应用;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);的setContentView(R.layout.newcard);
应用=((所有MyApplication)getApplicationContext());TextView的NEWCARD =(的TextView)findViewById(R.id.newcard);
newcard.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(查看为arg0){
        // TODO自动生成方法存根        意向卡=新意图(Newcard.this,Editcreditcard.class);
        startActivity(卡);    }
});ListView控件=(ListView控件)findViewById(R.id.cardlist);适配器=新MyAdapter(这一点,app.arryList,app.arryList1);
listView.setAdapter(适配器);
}
公共类MyAdapter延伸BaseAdapter {上下文的背景下= NULL;
ArrayList的<串GT;项=无效;
ArrayList的<串GT; items1 = NULL;
公共MyAdapter(NEWCARD NEWCARD,ArrayList的<串GT;的项目,
        ArrayList的<串GT; items1){
    // TODO自动生成构造函数存根    this.items =物品;
    this.items1 = items1;}@覆盖
公众诠释的getCount(){
    // TODO自动生成方法存根
    返回items.size();
}@覆盖
公共对象的getItem(INT位置){
    // TODO自动生成方法存根
    返回的物品;    //返回items.get(位置);
}@覆盖
众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    // TODO自动生成方法存根    查看布局= NULL;
    TextView中机生产线= NULL;
    TextView的递减= NULL;        如果(convertView == NULL){        躺在= LayoutInflater.from(getApplicationContext());
        布局= lay.inflate(R.layout.customlist,NULL);
    }其他{
        布局= convertView;
    }
    机生产线=(TextView中)layout.findViewById(R.id.card);
    produ.setText(+ app.arryList.get(位置));    DESC =(TextView中)layout.findViewById(R.id.descrip);
    desc.setText(+ app.arryList1.get(位置));    返回布局;}
}
}


解决方案

使用可以使用共享preferences ,这将永久保存您的值,直到他们被改变(通过改变在编辑框中的值)或用户重新安装(清除数据)的应用。 共享preferences 是这样工作的。

  //保存共享preferences串
共享preferences设置= preferenceManager.getDefaultShared preferences(getApplicationContext());
                    共享preferences.Editor编辑= settings.edit();
                    editor.putString(some_key字符串); //这里的字符串是要保存价值
                    editor.commit();//恢复共享preferences串
共享preferences设置= preferenceManager.getDefaultShared preferences(getApplicationContext());
串= settings.getString(some_key,);

am having a two editbox. the values enter in that has to placed in the custom listview its working fine for this...

but i want to save this values in the list view permanently.so that if the phones switches off and again run the app then the values have to be saved ..like phone contact number and name have to be saved.. here am ataching the code

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;

}
}
}

解决方案

use can use SharedPreferences, that will save your values permanently, untill they are changed(by changing values in editbox) or user reinstall (clear data) application. Shared Preferences works like this

// 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", "");

这篇关于保存在自定义列表视图编辑框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆