Listview和sharedprefs

时间:2015-11-29 22:42:22

标签: java android listview sharedpreferences

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

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

Mainactivity

  

公共类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列类

  

public class Ovelser {           public static final String FIRST_COLUMN =&#34; First&#34 ;;           public static final String SECOND_COLUMN =&#34; Second&#34 ;;           public static final String THIRD_COLUMN =&#34; Third&#34 ;;           public static final String FOURTH_COLUMN =&#34; Fourth&#34 ;;       }

结果

MainAct

Loggact

我在这里尝试做的是从Mainactivity布局获取输入文本到列表视图并按列排序。这部分很完美。我无法想象的是如何添加更多&#34;项目&#34;。当我这样做时,SharedPrefs中保存的数据(Trening)将被覆盖。我怎么能添加更多数据?还有另外一种方法我应该这样做吗?如果是这样请告诉我!

2 个答案:

答案 0 :(得分:1)

正如Youxian所说,当你放入相同的密钥时,你的数据会被覆盖。因为您每天都要重复此覆盖过程,所以您将无法获得每日锻炼数据的准确记录。而是使用数据库来存储您的数据。以下是共享首选项的工作原理:

enter image description here (windowsphone.interoperabilitybridges.com)

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

enter image description here (kb4dev.com)

因此,我建议您使用不同的密钥来防止覆盖,或者使用数据库。

答案 1 :(得分:0)

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