保存和重新加载ListView条目

时间:2013-12-04 17:52:33

标签: android android-intent android-listview sharedpreferences android-arrayadapter

我正在创建一个类似于记笔记应用程序的应用程序(对此仍然是新的)当我在listview中添加条目时,我希望它们在我通过主页按钮关闭我的应用程序并按下时保存在存储中。当我启动我的应用程序时,我希望加载以前的条目。我的代码在

之下
EditText forest;
TextView tv;
TextView date;
Button btn;
ListView txtlist;
public static ArrayList<String> list = new ArrayList<String>();
public static ArrayAdapter<String> add;
private NotesDataSource datasource;
String Lrg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_track);

    datasource = new NotesDataSource(this);

    txtlist = (ListView)findViewById(android.R.id.list);

    forest = (EditText)findViewById(R.id.trackedit);

    tv = (TextView)findViewById(R.id.tracktxt1);

    btn = (Button)findViewById(R.id.trackbtn);
    btn.setOnClickListener(this);

}     private void saveAndFinish(){

        forest = (EditText)findViewById(R.id.trackedit);
        String Lrg = forest.getText().toString();

         list.add(0, Lrg);
         KoreyzAdapter add = new KoreyzAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
         setListAdapter(add);
         add.notifyDataSetChanged();
            }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        saveAndFinish();
    }
    return false;
}
@Override
public void onBackPressed() {
    saveAndFinish();
}


public void onClick(View v) {

    String Lrg = forest.getText().toString();

    if(Lrg.trim().equals("")){
        Toast.makeText(this, "Please Enter an Argument Topic!", Toast.LENGTH_SHORT).show();
        return;
        }else{
     list.add(0, Lrg);
     KoreyzAdapter add = new KoreyzAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
     setListAdapter(add);
     add.notifyDataSetChanged();
     forest.setText("");
        }
}
static class ViewHolder{
  TextView gump;
  ImageView pic;
  TextView day;
  ImageView more;

  ViewHolder(View v){
      gump = (TextView) v.findViewById(R.id.inputLrgtxt);
      pic = (ImageView)v.findViewById(R.id.sexpic);
      day = (TextView)v.findViewById(R.id.holla);
  }

}
private class KoreyzAdapter extends ArrayAdapter<String> {

      private ArrayList<String> list;
      private Context context;

      public KoreyzAdapter(Context c, int textViewResourceId, ArrayList<String> list) {
          super(c, textViewResourceId, list);
          this.list = list;
          this.context = c;
      }


          public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder = null;
            long msTime = System.currentTimeMillis();
            Date date = new Date(msTime);
            SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");
            String biggie = sdf.format(date);

            if(row == null){

                 LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  row = inflator.inflate(R.layout.list_item, parent, false);
                  holder = new ViewHolder(row);
                  row.setTag(holder);

            }else{
                holder = (ViewHolder)row.getTag();
            }

                holder.gump.setText(list.get(position));
                holder.day.setText(biggie);

                  return row;
            }

2 个答案:

答案 0 :(得分:0)

请检查Android平台中的可用数据存储。您可以在完成之前将数据保存在存储中,并且在开始时可以获取您的条目并在屏幕上加载。

http://developer.android.com/guide/topics/data/data-storage.html

答案 1 :(得分:0)

您需要使用SQLite数据库或类似的东西来保存数据。

基本上你需要:

  1. 创建新项目时,将其保存到SQLite数据库
  2. 当(重新)启动您的应用时,您将从数据库中获取所有项目
  3. 初始化ListView时附加您提供商品列表的Adapter
  4. 例如,请参阅本教程:http://www.vogella.com/articles/AndroidSQLite/article.html