Android共享首选项 - 删除单个项目

时间:2016-11-25 23:29:48

标签: android listview button methods sharedpreferences

首先 - 任何提示/帮助都表示赞赏。

对于这个问题 - 我正在创建一个应用程序,它不断向共享首选项文件添加位置(以及位置名称和日期)。这些位置将保存为位置日志,并在列表视图中显示给用户。用户可以点击任何位置,他们将被传递到选定的项目详细信息页面,在这里他们可以更改位置的名称,导航到该位置甚至删除位置。

问题是,当用户删除位置时,共享首选项文件已正确更新但程序崩溃。我不知道它为什么这样做,我尝试使用Toasts进行调试,但这并没有那么好用。

我附加了处理单项描述视图的java代码。处理删除功能的部分是" onClickDelete"方法。那里有些不对劲。

P.S - 我尝试使用editor.commit()和editor.apply(),两者都没有阻止程序崩溃。

   package com.android.ict.seneca.androidpocketguide;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Websites extends Activity
        implements AdapterView.OnItemClickListener{

    private final String CLASS_NAME = "Websites";
    private int request_Code = 1;
    DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
    String date;
    String location;
    String name;
    String favourite;
    int counter;
    List listName;
    ImageView image1;
    ImageView image2;
    String [] favo = new String [100];
    String [] unfavo = new String [100];
    int c1 = 0;
    int c2 = 0;
    SharedPreferences.Editor editor;

    private SharedPreferences savedState;

    private ListView listView;
    private List<RowItem> rowItems;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_websites);
        listName= new ArrayList();
        String app_name = "//sdcard//LocoLog.txt";
        //int counter = 0, flag = 0;
        String temp = "";
        savedState = getSharedPreferences( "quantitySaved", MODE_PRIVATE );
        editor = savedState.edit();
        rowItems = new ArrayList<RowItem>();
        counter = savedState.getInt("counter", -1 );

        //Toast.makeText(this,"Websites: " + counter,Toast.LENGTH_LONG).show();
        if(counter!=-1)
        {
            for (int i = 1; i <= counter; i++) {
                String na;
                String da;
                String lo;
                String fa;
                na = "name" + i;
                da = "date" + i;
                lo = "location" + i;
                fa = "favourite" + i;

                name = savedState.getString(na, "na");
                //name = "Name: " + name;
                date = savedState.getString(da, "da");
                //date = "Date: " + date;
                location = savedState.getString(lo, "lo");
                //location = "Location: " + location;
                favourite = savedState.getString(fa, "fa");

                //Toast.makeText(this, "name: " + name, Toast.LENGTH_LONG).show();

                if(favourite.equals("Yes"))
                {
                    favo[c1] = name + "]" + location + "]" + date + "]" + favourite + "]" + counter;
                    //Toast.makeText(this, "favo[" + c1 + "]: " + favo[c1], Toast.LENGTH_LONG).show();
                    c1++;
                }
                else if(favourite.equals("No"))
                {
                    unfavo[c2] = name + "]" + location + "]" + date + "]" + favourite + "]" + counter;
                    //Toast.makeText(this, "unfavo[" + c2 + "]: " + unfavo[c2], Toast.LENGTH_LONG).show();
                    c2++;
                }
                else
                {
                    Toast.makeText(this, "ERROR: Favourite value is wrong!",Toast.LENGTH_LONG).show();
                }


                //Toast.makeText(this, "The data: "+name + " " + location + " " + date,Toast.LENGTH_LONG).show();
                //listName.add(new RowItem(name, location, date, counter));
            }
        }
        else
        {
            Toast.makeText(this, "No locations saved yet",Toast.LENGTH_LONG).show();
        }

        int co;
        String[] separated1 = new String[5];
        String[] separated2 = new String[5];

        for(int i = 0; i < c1; i++)
        {
            separated1 = favo[i].split("]");
            co =  Integer.parseInt(String.valueOf(separated1[4]));
            Toast.makeText(this, "name: " + separated1[0] + " location: " + separated1[1] + " date: " + separated1[2] + " favourite: " + separated1[3] + " counter: " +separated1[4]  ,Toast.LENGTH_LONG).show();
            listName.add(new RowItem(separated1[0], separated1[1], separated1[2], separated1[3], co));
        }

        for(int i = 0; i < c2; i++)
        {
            separated2 = unfavo[i].split("]");
            co =  Integer.parseInt(String.valueOf(separated1[4]));
            Toast.makeText(this, "name: " + separated2[0] + " location: " + separated2[1] + " date: " + separated2[2] + " favourite: " + separated2[3] + " counter: " + separated1[4],Toast.LENGTH_LONG).show();
            listName.add(new RowItem(separated2[0], separated2[1], separated2[2], separated2[3], co));
        }

        listView = (ListView) findViewById(R.id.locationList);

        //image1 = (ImageView) findViewById(R.id.imageview1);
        //image1.setImageResource(R.drawable.pic1);

        listView.setAdapter( new CustomListAdapter(this, R.layout.list_item, listName ) );
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String s = String.valueOf(parent.getItemAtPosition(position));
        Intent myIntent = new Intent(view.getContext(), SingleListItem.class);
        myIntent.putExtra("product", s);
        String p = String.valueOf(position); //convert position from int to String
        myIntent.putExtra("position", p);
        startActivityForResult(myIntent, 0);
    }
    public void onClickDelete(View view)
    {
        editor.clear().commit();
        //String cities = String.valueOf(parent.getItemAtPosition(position));
        Toast.makeText(this, "All Locations Deleted", Toast.LENGTH_LONG).show();
        startActivityForResult(new Intent(this,Websites.class ),request_Code);

        //rowItems.remove(position);
    }

    public void onClickHome(View view)
    {
        Toast.makeText(this, "Home button pressed ", Toast.LENGTH_LONG).show();
        startActivityForResult(new Intent(this,MainActivity.class ),request_Code);

    }

    public void onStart() {
        super.onStart();
        Log.d(CLASS_NAME, "onStart invoked!");
    }

    public void onPause() {
        super.onPause();
        Log.d(CLASS_NAME, "onPause invoked!!");
    }

    public void onResume() {
        super.onResume();
        Log.d(CLASS_NAME, "onResume invoked!!");
    }

    public void onStop() {
        super.onStop();
        Log.d(CLASS_NAME, "onStop invoked!!!");
    }

    public void onDestroy() {
        super.onDestroy();
        Log.d(CLASS_NAME, "onDestroy INVOKED!!!");
    }

    public void onRestart() {
        super.onRestart();
        Log.d(CLASS_NAME, "onRestart invoked!!");
    }

}

堆栈:(这指向此行:counter = savedState.getInt(&#34; counter&#34;, - 1);

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.android.ict.seneca.androidpocketguide, PID: 6739
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.ict.seneca.androidpocketguide/com.android.ict.seneca.androidpocketguide.Websites}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
                      at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:242)
                      at com.android.ict.seneca.androidpocketguide.Websites.onCreate(Websites.java:78)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

0 个答案:

没有答案