簡體   English   中英

嘗試更新ListView中從SQLite檢索的值時,應用程序崩潰

[英]App crashed when trying to update the retrieved value from SQLite in ListView

Edit_WorkDetails活動中,它具有一個ListView ,實際上是從SQLite檢索數據的位置。 單擊該列表后,它將intentEdit_Details進行編輯。

這就是我的Edit_WorkDetails活動的樣子。

在此處輸入圖片說明

單擊Edit_Details中的保存按鈕時,應用程序崩潰了。

Edit_WorkDetails

  listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                mClickedPosition=position; // update
                // Get the cursor, positioned to the corresponding listview_item_row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this listview_item_row in the database.
                ID =
                        cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                Intent intent = new Intent(getActivity(), Edit_Details.class);
                intent.putExtra("ID", ID);
                startActivityForResult(intent, PROJECT_REQUEST_CODE);


            }
        });

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Edit_Details and populate ListView 
        if (resultCode == Activity.RESULT_OK) {
            if(requestCode==PROJECT_REQUEST_CODE) {
                ReceiveProject = data.getStringExtra("project1");
                ReceiveDescription = data.getStringExtra("description");
                ReceiveProgress = data.getIntExtra("progress", 0);
                ReceiveTimeIn = data.getStringExtra("timeIn");
                ReceiveTimeOut = data.getStringExtra("timeOut");
                if (mClickedPosition == -1) {  // if icon clicked
                    if (objCustomBaseAdapter != null)
                        objCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);

                } else {  // update list
                    if (objCustomBaseAdapter != null)
                        objCustomBaseAdapter.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);

                }
            }

        }

    }

Edit_Details.java

  save.setOnClickListener(new View.OnClickListener() {  // return values to Edit_WorkDetails and update the list
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                project1 = Project2.getSelectedItem().toString();
                description = Description.getText().toString();
                progress = seekBar.getProgress();
                returnIntent.putExtra("project1", project1);
                returnIntent.putExtra("description", description);
                returnIntent.putExtra("progress", progress);
                Toast.makeText(getApplicationContext(), progress + "", Toast.LENGTH_LONG).show();
                returnIntent.putExtra("timeIn", timeIn);
                returnIntent.putExtra("timeOut", timeOut);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();

            }


        });

    }

CustomBaseAdapter

 public void changeItem(int mPosition,String Project,String Description,int Percentage,String TimeIn,String TimeOut)
    {
        SearchResults obj = new SearchResults();
        obj.setProject(Project);
        obj.setDescription(" Work Description : " + Description);
        obj.setProgress(" Progress : " + Percentage);
        obj.setTimeIn(" Time In : " + TimeIn);
        obj.setTimeOut(" Time Out : " + TimeOut);
        searchArrayList.set(m,obj);
        this. notifyDataSetChanged();
    }

最后是SearchResult.java

public class SearchResults {

    private String weather = "";
    private String date = "";
    private String status = "";
    private String timeIn="";
    private String timeOut="";
    private String project="";
    private String description="";
    private String progress="";

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getWeather() {
        return weather;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return date;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void setTimeIn(String timeIn) {
        this.timeIn = timeIn;
    }

    public String getTimeIn() {
        return timeIn;
    }

    public void setTimeOut(String timeOut){
       this.timeOut=timeOut;
    }

    public String getTimeOut()
    {
        return timeOut;
    }

    public void setProject(String project){
        this.project=project;
    }

    public String getProject()
    {
        return project;
    }

    public void setProgress(String progress){
        this.progress=progress;
    }

    public String getProgress()
    {
        return progress;
    }

    public void setDescription(String description){
        this.description=description;
    }

    public String getDescription()
    {
        return description;
    }

}

錯誤的logCat

  Process: com.example.project.myapplication, PID: 2467
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196609, result=-1, data=Intent { (has extras) }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.ActivityB}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)

(CustomBaseAdapter.java:101)

 searchArrayList.set(m,obj);

(Edit_WorkDetails.java:141)

 objCustomBaseAdapter.changeItem(mClickedPosition, ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);

為什么我會收到這樣的錯誤? 無法將SQLite的檢索值編輯為ListView

數據庫模型

 db.execSQL("create table " + TABLE_WORKDETAILS + "( " + ID1 + " INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT, Percentage Text, TimeIn DATETIME, TimeOut DATETIME, Twd_id INTEGER, FOREIGN KEY(Twd_id) REFERENCES "+TABLE_INFO+"(_id) )" );

注意,Twd_id是foreign key

這是我的workDetails表

在此處輸入圖片說明

現在,我檢索到的workDetails在Edit_WorkDetails表數據

Edit_WorkDetails

Bundle bundle = this.getArguments();
        if(getArguments()!=null)
        {
            ID=bundle.getLong("ID");  // ID holds value 1
            BuildEditDetails(ID);
        }

 public void BuildEditDetails(long ID)
    {
         final long id=ID;
         sqlcon.open();
        Cursor cursor=sqlcon.readData(id);

        String[] columns=new String[]{MyDatabaseHelper.Project,MyDatabaseHelper.WorkDescription,MyDatabaseHelper.Percentage
                ,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut};

        int[] to=new int[]
                {
                        R.id.Project,R.id.Description,R.id.Percentage,R.id.in,R.id.out
                };
        dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.retrieve_details, cursor, columns, to, 0);

        listViewUpdate.setAdapter(dataAdapter);

    }

您可能在上面的代碼中引用了以下代碼

您可以通過執行諸如customListAdapter.setListData(getListData())之類的操作來更新listview數據。 customListAdapter.notifyDataSetChanged();基本上,當你調用add()的數據添加,但不會刷新,所以盡量調用notifyondatasetchanged()如果它不能正常工作呼叫myListView.invalidateViews(); 它將重繪列表視圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM