【问题标题】:Listview and sharedprefsListview 和 sharedprefs
【发布时间】:2015-11-29 22:42:22
【问题描述】:

所以我目前正在开发一个可以在训练时使用的应用程序。我在这里想要实现的是记录我每天所做的事情,以便稍后我可以看到我的进展情况。..

这是我到目前为止所做的。

主要活动

公共类 Mandag 扩展 AppCompatActivity{

EditText O1,R1,S1,KG1;
Button Leggtil, logg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mandag);
    Leggtil = (Button)findViewById(R.id.leggtil);
    O1 = (EditText)findViewById(R.id.O1);
    R1 = (EditText)findViewById(R.id.R1);
    S1 = (EditText)findViewById(R.id.S1);
    KG1 = (EditText)findViewById(R.id.KG1);
    logg = (Button)findViewById(R.id.button);
    Leggtil.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences SF = getSharedPreferences("Trening", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = SF.edit();
            editor.putString("O1",O1.getText().toString());
            editor.putString("R1",R1.getText().toString());
            editor.putString("S1",S1.getText().toString());
            editor.putString("KG1",KG1.getText().toString());
            editor.commit();
            Toast.makeText(Mandag.this,"Lagt til i logg",Toast.LENGTH_LONG).show();


        }
    });

    logg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Mandag.this, Logg.class);
            startActivity(i);
        }
    });


}

主要活动布局

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Øvelse"
    android:id="@+id/ovelse"
    android:layout_weight="0.03"
    android:layout_row="0"
    android:layout_column="0" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Reps"
    android:id="@+id/Reps"
    android:layout_weight="0.03"
    android:layout_row="0"
    android:layout_column="1"
    android:layout_marginLeft="0dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Sets"
    android:id="@+id/Sets"
    android:layout_weight="0.03"
    android:layout_row="0"
    android:layout_column="2"
    android:layout_marginLeft="0dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="KG"
    android:id="@+id/KG"
    android:layout_weight="0.03"
    android:layout_row="0"
    android:layout_column="3"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp" />

<EditText
    android:layout_width="148dp"
    android:layout_height="wrap_content"
    android:id="@+id/O1"
    android:layout_row="1"
    android:layout_column="0" />

<EditText
    android:layout_width="62dp"
    android:layout_height="wrap_content"
    android:id="@+id/R1"
    android:layout_row="1"
    android:layout_column="1" />

<EditText
    android:layout_width="51dp"
    android:layout_height="wrap_content"
    android:id="@+id/S1"
    android:layout_row="1"
    android:layout_column="2" />

<EditText
    android:layout_width="51dp"
    android:layout_height="wrap_content"
    android:id="@+id/KG1"
    android:layout_row="1"
    android:layout_column="3" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="logg"
    android:id="@+id/button"
    android:layout_row="27"
    android:layout_column="0" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Leggtil"
    android:id="@+id/leggtil"
    android:layout_row="27"
    android:layout_column="3" />

记录活动

公共类 Logg 扩展 AppCompatActivity {

String O1, R1, S1, KG1;

private ArrayList<HashMap<String, String>> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logg);
    ListView listView=(ListView)findViewById(R.id.listView);
    list=new ArrayList<HashMap<String,String>>();

    SharedPreferences SF = getSharedPreferences("Trening", Context.MODE_PRIVATE);

    DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy");
    String date = df.format(Calendar.getInstance().getTime());

    HashMap<String,String> dates=new HashMap<String, String>();
    dates.put(FIRST_COLUMN, date.toString());

    list.add(dates);

    HashMap<String,String> form=new HashMap<String, String>();
    form.put(FIRST_COLUMN, "Øvelse");
    form.put(SECOND_COLUMN, "Reps");
    form.put(THIRD_COLUMN, "Sets");
    form.put(FOURTH_COLUMN, "KG");
    list.add(form);

    HashMap<String,String> temp=new HashMap<String, String>();
    temp.put(FIRST_COLUMN, O1 = SF.getString("O1",""));
    temp.put(SECOND_COLUMN,R1 = SF.getString("R1",""));
    temp.put(THIRD_COLUMN, S1 = SF.getString("S1",""));
    temp.put(FOURTH_COLUMN, KG1 = SF.getString("KG1",""));
    list.add(temp);


    Loggene adapter=new Loggene(this, list);
                    listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                    int pos = position + 1;
                    Toast.makeText(Logg.this, Integer.toString(pos) + " Clicked", Toast.LENGTH_SHORT).show();
                }

            });


}

日志列表视图布局

<ListView
    android:layout_width="match_parent"
    android:layout_height="617dp"
    android:id="@+id/listView" />

列表视图活动

公共类 Loggene 扩展 BaseAdapter{

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
    public Loggene(Activity activity, ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

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



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



        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null){

            convertView=inflater.inflate(R.layout.logg_item, null);

            txtFirst=(TextView) convertView.findViewById(R.id.ovelse);
            txtSecond=(TextView) convertView.findViewById(R.id.Reps);
            txtThird=(TextView) convertView.findViewById(R.id.Sets);
            txtFourth=(TextView) convertView.findViewById(R.id.KG);

        }

        HashMap<String, String> map=list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtThird.setText(map.get(THIRD_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));

        return convertView;
    }

}

列表视图布局

    <TextView
        android:id="@+id/Sets"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/Reps"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.5" />

    <TextView
        android:id="@+id/KG"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1" />

Listview 列类

公共类 Ovelser { 公共静态最终字符串 FIRST_COLUMN="First"; 公共静态最终字符串 SECOND_COLUMN="Second"; 公共静态最终字符串 THIRD_COLUMN="Third"; 公共静态最终字符串 FOURTH_COLUMN="Fourth"; }

结果

我在这里要做的是将输入文本从 Mainactivity 布局获取到列表视图并按列排序。这部分工作完美。我无法弄清楚的是如何添加更多“项目”。当我这样做时,SharedPrefs 中保存的数据(Trening)将被覆盖。那么如何添加更多数据呢?也许我应该这样做的另一种方法?如果是这样,请告诉!

【问题讨论】:

  • 你应该考虑使用数据库和ContentProvider,有很多关于这方面的教程

标签: java android listview sharedpreferences


【解决方案1】:

正如有贤所说,您的数据在放入同一个密钥时会被覆盖。因为您将每天重复这个压倒一切的过程,您将无法获得每日锻炼数据的准确日志。相反,使用数据库来存储您的数据。以下是共享首选项的工作原理:

(windowsphone.interoperabilitybridges.com)

正如您在此图中所见,它们 IsolatedStorageSettings 是基于您现在正在覆盖的键/值对。这就是编译器甚至知道要获取什么数据的方式,关键是:

(kb4dev.com)

同样,我建议您使用不同的键来防止覆盖,或者使用数据库。

【讨论】:

  • @Itszani 如果有效,如果您将我的答案标记为最佳答案,我将不胜感激。您应该会在“赞”按钮下看到一个复选标记,只需单击它即可。谢谢!
【解决方案2】:

SharedPreference 就像键和值,所以你总是把数据放在同一个键(O1,R1,S1,KG1)中。您存储的数据将被覆盖。如果您仍然想使用共享首选项,只需将值放在不同的键中(例如在键名后添加一个计数器)。并使用 getAll() 方法将它们显示在您的列表中。 您可以查看这些以获取有关共享偏好的更多信息:

http://developer.android.com/intl/es/reference/android/content/SharedPreferences.html

http://developer.android.com/intl/es/reference/android/content/SharedPreferences.Editor.html

使用 android SQLite 数据库来做这件事可能是一种选择,了解更多:

http://developer.android.com/intl/es/training/basics/data-storage/databases.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2011-05-23
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多