如果可能,我想使用共享首选项保存我的数据

时间:2016-11-21 16:19:41

标签: android sharedpreferences

在下面的代码中,我使用listView和Custom Array适配器创建一个存储会议的列表。(涉及的人员,描述,日期和时间)。我一直在寻找堆栈溢出并获得共享首选项。我不知道如何使用它。非常感谢任何帮助。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
    public function index($user = '') {
        echo "index!";
    }

    public function create() {
        echo 'create!';
    }
}

此处还有我的自定义适配器代码

public class MainActivity extends AppCompatActivity {
ArrayList<SearchResults> searchResults;
private FloatingActionButton add;
private FloatingActionButton clear;
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
final Context context = this;
String time1, people1, description1, mDate, hour, minute, MeridiemIndicator, dateNumber;
int Year, Month, Day, Hour, Minute;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    searchResults = new ArrayList<SearchResults>();
    setContentView(R.layout.activity_main);
    final ListView lv1 = (ListView) findViewById(R.id.ListView);
    final CustomAdapter myAdapter = new CustomAdapter(context, R.layout.custom_row_view, results);
    lv1.setAdapter(myAdapter);


    add = (FloatingActionButton) findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View view) {

            dialogMethod(lv1);
        }


    });

    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
            adapter.remove(results.get(position));
            adapter.notifyDataSetChanged();


        }
    });
    lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long l) {
            CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
            adapter.remove(results.get(position));
            adapter.notifyDataSetChanged();
            dialogMethod(lv1);

            return true;
        }


    });
    clear = (FloatingActionButton) findViewById(R.id.clear);
    clear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View view) {
            results.clear();
            CustomAdapter myAdapter = new CustomAdapter(context, R.layout.custom_row_view, results);
            lv1.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
        }


    });
}




public void dialogMethod(final ListView lv1) {
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.activity_add_meeting);
    dialog.setTitle("Meeting");

    //ingredient.getText().toString();

    final EditText people = (EditText) dialog.findViewById(R.id.people);
    if (people.getText().toString().length() == 0)
        people.setError("Required");
    final EditText description = (EditText) dialog.findViewById(R.id.description);
    if (description.getText().toString().length() == 0)
        description.setError("Required");
    FloatingActionButton save = (FloatingActionButton) dialog.findViewById(R.id.save);
    // if button is clicked, close the custom ingredient_dialog
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatePicker cal = (DatePicker) dialog.findViewById(R.id.calendar);
            Year = cal.getYear();
            Month = cal.getMonth();
            Day = cal.getDayOfMonth();
            Month = Month + 1;

            TimePicker time = (TimePicker) dialog.findViewById(R.id.clock);
            Hour = time.getHour();
            Minute = time.getMinute();

            dialog.dismiss();
            time1 = hour + ":" + minute + MeridiemIndicator;
            people1 = people.getText().toString();
            description1 = description.getText().toString();

            mDate = Year + "/" + Month + "/" + Day;
            dateNumber = Year + "";
            if (Month < 10) {
                dateNumber += "0" + Month;
            } else {
                dateNumber += Month;
            }
            if (Day < 10) {
                dateNumber += "0" + Day;
            } else {
                dateNumber += Day;
            }


            ArrayList<SearchResults> searchResults = GetSearchResults();
            lv1.setAdapter(new CustomAdapter(MainActivity.this, R.layout.custom_row_view, searchResults));


        }
    });
    dialog.show();


}
private ArrayList<SearchResults> GetSearchResults() {


    SearchResults sr1 = new SearchResults();
    sr1.setName("  Meeting With: " + people1);
    sr1.setTimeOfMeeting("  Starts At: " + time1);
    sr1.setDescription("  Details: " + description1);
    sr1.setDate("  Date: " + mDate);
    sr1.setDateNumber(dateNumber);
    results.add(sr1);
    Collections.sort(results, new Comparator<SearchResults>() {
        public int compare(SearchResults x, SearchResults y) {
            return Integer.parseInt(x.getDateNumber()) - Integer.parseInt(y.getDateNumber());
        }
    });


    return results;
  }
}

2 个答案:

答案 0 :(得分:0)

你可以制作JsonObject(或JsonArray)并将其保存为String( jsonObject.toString() jsonArray.toString()

JsonObject&amp;&amp;数组教程 - &gt; https://www.tutorialspoint.com/json/json_java_example.htm

然后只需获取或保存您的字符串

CREATE OR REPLACE FUNCTION IR.SRG(
    IR_item IN VARCHAR2,
    IR_comp     VARCHAR2,
    IR_locn     VARCHAR2,
    IR_Type     VARCHAR2,
    IR_fromdate DATE,
    IR_tilldate DATE,
  )
 RETURN NUMBER DETERMINISTIC
IS
  IR_qty NUMBER;
  myLocations sys.odcivarchar2list; --collection
BEGIN
  if IR_TYPE       = 'O' then
    IF IR_locn = 'ALL' THEN
       myLocations := SYS.ODCIVARCHAR2LIST('D2','D4','D5','D11');
    ELSE
       myLocations := SYS.ODCIVARCHAR2LIST('D2');
    END IF;
    SELECT SUM(QTY)
    INTO IR_qty
    FROM STOCK_LEDGER
    WHERE ITEM_CODE    = IR_item
    AND LOCATION_CODE IN
    (SELECT column_value FROM TABLE(myLocations)
  )
  AND DOCUMENTDATE <= IR_TILLDATE
  AND DOCUMENTDATE >= IR_FROMDATE;
END IF;
RETURN (NVL (IR_QTY, 0));
EXCEPTION
  WHEN ZERO_DIVIDE THEN
   RETURN 0;
END;

有很多方法

答案 1 :(得分:0)

在Android中,有很多方法可以在您的应用上存储数据:

  • SharedPreferences
  • 设备存储/ SD卡存储
  • SQLite的

根据数据类型,应选择数据的存储介质。

  • 图像文件应作为JPEG / PNG文件存储在设备存储中,如果需要,可从应用程序外部访问。在SharedePreferences / SQLite中存储编码为字节流的Image是没有意义的。

  • 一些必须以列表等形式显示的固定格式的数据(如你的)应该存储在SQL中,因为它提供了一种非常有序的存储数据的方式。

  • 其他数据,如用户帐户信息,本质上不可重复且为非结构化形式,应存储在设备的SharedPreferences中(至少我将此类数据存储在SharedPreferences中的帐户信息等)。

理论部分太多了。由于你有一个列表,每个列表项中显示的数据格式是相对结构化的,需要经常访问,在这种情况下,我建议最好使用数据库来保存数据,如CRUD SQLite中的操作很容易实现。要了解SQLite,请访问documentation网站上的Android Developers for SQLite。