将listview内容保存到android中的sd-card

时间:2012-02-18 15:44:28

标签: android listview save sd-card

我正在制作一个需要将listview项目保存到SD卡的android应用程序。我正在使用动态列表视图,用户可以从中删除项目并添加项目。如果应用程序被销毁或者用户点击后退按钮,我希望listview保存项目。基本上当活动被破坏时。请提前帮助和谢谢!到目前为止我正在使用此代码:

public class NotesActivity extends ListActivity implements OnClickListener {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
Button AddItemToListView, AddItemToListViewButton, CancelButton, DeleteButton,CancelButton2, DeleteAllButton;
LinearLayout AddItemToListViewLinearLayout, DeleteItemFromListViewLinearLayout, DeleteAllItemsFromListViewLinearLayout;
public int DeleteIndexNumber;
  static final String[] COUNTRIES = new String[] {
      "Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
      };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes);
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
        DeleteIndexNumber = position;
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.VISIBLE);
  }
    });
  }
@Override
public boolean onCreateOptionsMenu(Menu meny) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.listviewmenubuttons, meny);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.AddItemToListView:
        AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
        CancelButton = (Button)findViewById(R.id.CancelButton);
        DeleteButton = (Button)findViewById(R.id.DeleteButton);
        CancelButton.setOnClickListener(this);
        DeleteButton.setOnClickListener(this);
        AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
        AddItemToListViewButton.setOnClickListener(this);
        AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
        break;
    case R.id.DeleteAllNotes:
        DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
        DeleteAllItemsFromListViewLinearLayout.setVisibility(View.VISIBLE);
        CancelButton2 = (Button)findViewById(R.id.CancelButton2);
        DeleteAllButton = (Button)findViewById(R.id.DeleteAllButton);
        CancelButton2.setOnClickListener(this);
        DeleteAllButton.setOnClickListener(this);
        break;
        }
    return true;
}
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.AddItemToListViewButton:
        AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
        myList.add(AddItemToListViewEditText.getText().toString());
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
        AddItemToListViewEditText.setText("");
        AddItemToListViewEditText.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        AddItemToListViewLinearLayout.setVisibility(View.GONE);
        break;
    case R.id.CancelButton:
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.DeleteButton:
        myList.remove(DeleteIndexNumber);
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.DeleteAllButton:
        myList.removeAll(myList);
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
        DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
        DeleteAllItemsFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.CancelButton2:
        DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
        DeleteAllItemsFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以查看数据存储的官方指南。由于ListView内容很大,您可以将其存储到SQLite数据库中。该指南的链接如下: -

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