简体   繁体   English

尝试更新ListView中从SQLite检索的值时,应用程序崩溃

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

In Edit_WorkDetails Activity, it has a ListView where the data were actually retrieved from SQLite . Edit_WorkDetails活动中,它具有一个ListView ,实际上是从SQLite检索数据的位置。 When the list is clicked, it will intent to Edit_Details for edit. 单击该列表后,它将intentEdit_Details进行编辑。

This is how my Edit_WorkDetails Activity looked like. 这就是我的Edit_WorkDetails活动的样子。

在此处输入图片说明

When the save button in Edit_Details clicked, the app crashed. 单击Edit_Details中的保存按钮时,应用程序崩溃了。

Edit_WorkDetails 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 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 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();
    }

AND finally SearchResult.java 最后是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;
    }

}

Error logCat 错误的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) (CustomBaseAdapter.java:101)

 searchArrayList.set(m,obj);

(Edit_WorkDetails.java:141) (Edit_WorkDetails.java:141)

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

Why I will getting such error ? 为什么我会收到这样的错误? It is impossible to edit the retrieved value from SQLite to ListView ? 无法将SQLite的检索值编辑为ListView

Database model 数据库模型

 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) )" );

Noted that Twd_id is foreign key . 注意,Twd_id是foreign key

Here my workDetails table 这是我的workDetails表

在此处输入图片说明

Now I retrieved the workDetails table data in Edit_WorkDetails 现在,我检索到的workDetails在Edit_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);

    }

The following code you may refer to code above 您可能在上面的代码中引用了以下代码

You can update your listview data by doing something like customListAdapter.setListData(getListData()); 您可以通过执行诸如customListAdapter.setListData(getListData())之类的操作来更新listview数据。 customListAdapter.notifyDataSetChanged();basically when you call add() data is added but not refreshed, so try calling notifyondatasetchanged() if it does not work call myListView.invalidateViews(); customListAdapter.notifyDataSetChanged();基本上,当你调用add()的数据添加,但不会刷新,所以尽量调用notifyondatasetchanged()如果它不能正常工作呼叫myListView.invalidateViews(); it will redraw the listview 它将重绘列表视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试从数据库中删除数据并刷新 ListView 时,应用程序崩溃 - App is getting crashed when I'm trying to delete Data from database and refreshing ListView 尝试使用setOnClickListener时应用崩溃 - app getting crashed when trying to use setOnClickListener 使用从SQLite数据库检索的信息来创建一个有效的ListView活动 - Creating a working ListView activity with information retrieved from a SQLite Database 尝试从 TextView 获取值并将其发送到 firebase 参考,但应用程序正在崩溃 - Trying to get the value from the TextView and send it to the firebase reference but app is getting crashed 尝试从SQLite数据库填充ListView - Trying to populate listview from sqlite database Android:尝试将API中的数据加载到列表视图时应用崩溃 - Android: App crashing when trying to load Data from API into a listview 尝试调用BufferedReader.readLine()时应用崩溃 - app crashed when trying to call BufferedReader.readLine() 从SQLite数据库中删除项目并更新ListView - Delete item from SQLite Database and update ListView 刷新时listView随机崩溃 - listView crashed randomly when refresh 如果尝试从Android的Assets文件夹中获取位图图像,应用程序崩溃了吗? - App Crashed if trying to get Bitmap Image from Assets Folder in Android ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM