SQLite行ID不匹配ListView-ANDROID [英] sqlite row ids not matching listview - ANDROID

查看:85
本文介绍了SQLite行ID不匹配ListView-ANDROID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个la脚的问题,但是我是一个初学者,似乎不了解我的应用程序的行为.

This may turn out as a lame question, but I am a beginner and don't seem to understand the behavior of my app.

我有一个SQLite数据库,并且在主活动中有一个列表视图. 在列表视图中,我显示列表号(行号)和其他详细信息. 按下列表项后,将打开一个新活动,该活动在文本视图中显示详细信息,并具有2个用于删除和同步的图像按钮.

I have a SQLite data base, and a list view in the main activity. In the list view I display the List no.(row id) and other details. and on pressing the list item, a new activity opens up which displays the details in a textview and has 2 image buttons for delete and sync.

在删除任何条目之前,该应用程序都可以正常工作.

The app works just fine until I delete any entry.

一旦删除任何条目,列表视图上的行ID似乎就不会更新行号. (如果我删除第一行,则顶部的行将显示2(应为1,因为它是新的第一行).)

Once I delete any entry the row id on the listview doesn't seem to update the row number. (if i delete the first row, the row on top shows 2(should be 1, as it is the new first row)).

但是,当我显示记录的详细信息时,它是正确的. (列表中的第二项显示第2行)

However when I display the details of record it comes out correct. (the second item on the list show the row number 2)

另外,当我按列表视图中与我之前删除的位置相同的项时,应用程序崩溃(假设我删除了第一个条目,此后每当我单击列表视图中的第一个项以将其显示为应用程序崩溃)

Also when I press press an item in the list view whose position is same as the position i have deleted earlier the app crashes(Suppose i delete the first entry, after that whenever I click on the first Item on the list view to display it the app crashes)

我在应用崩溃时出错:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aakashmaroti.fillup/com.example.aakashmaroti.fillup.DisplayDetails}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

我的主要活动

package com.example.aakashmaroti.fillup;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class StartScreen extends ActionBarActivity implements View.OnClickListener {


    FavoriteList favList = new FavoriteList();

    TextView rid,ts,sd;
    ListView lv;
    Context context=this;
    Find db=new Find(this);
    List<FavoriteList> favoriteList;
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_screen);
        Button b = (Button) findViewById(R.id.AddButton);
        b.setOnClickListener(this);
        lv = (ListView) findViewById(R.id.listview);
        try
        {
            populateListViewFromDatabase();
        } catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "Oops there has been an error "+e.toString()+"", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                showsDialog(position);
            }
        });


    }


    @Override
    public void onResume()
    {
        super.onResume();
        try
        {
            populateListViewFromDatabase();
        } catch (Exception e)
        {
            Toast.makeText(getApplicationContext(), "Oops there has been an error "+e.toString()+"", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_start_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.AddButton:
                startActivity(new Intent(this,Form.class));
                break;

            default: break;
        }
    }

    public void populateListViewFromDatabase()throws Exception
    {
        Find info=new Find(this);
        try
        {
            info.open();
            SimpleCursorAdapter myCursorAdapter;
            myCursorAdapter = info.listUp();
            lv.setAdapter(myCursorAdapter);
            info.close();

        } catch (SQLException e)
        {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Oops there has been an error "+e.toString()+"", Toast.LENGTH_LONG).show();
        }
    }
    public void showsDialog(int pos)
    {

        pos++;
        String s = "";
        s += pos;
        Intent i = new Intent(getApplicationContext(), DisplayDetails.class);
        i.putExtra("position", s);
        startActivity(i);

        try
        {
            populateListViewFromDatabase();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

sqlite数据库管理类

The sqlite data base managing class

package com.example.aakashmaroti.fillup;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Aakash Maroti on 28-Dec-14.
 */
public class Find
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TIMESTAMP= "time_stamp";
    public static final String KEY_IS_SYNCED = "sync";
    public static final String KEY_NameOfCompany="name_of_company";



    private static final String DATABASE_NAME = "FillUpFormsDB";
    private static final String DATABASE_TABLE = "FillUpFormsTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;




    private static class DbHelper extends SQLiteOpenHelper
    {

        public DbHelper(Context context)
        {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
        }



        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                            KEY_IS_SYNCED +" TEXT NOT NULL, " +
                            KEY_TIMESTAMP +" TEXT NOT NULL, " +
                            KEY_NameOfCompany+" TEXT NOT NULL);"
            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
            onCreate(db);
        }     

    }  


    public Find(Context c)
    {
        ourContext = c;
    }

    public Find open()throws SQLException
    {
        ourHelper = new DbHelper(ourContext);
        ourDatabase=ourHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        ourHelper.close();
    }


    public long createEntry(String isSynced, String timeStamp, String nameOfCompany)
    {
        ContentValues cv=new ContentValues();
        cv.put(KEY_IS_SYNCED,isSynced);
        cv.put(KEY_TIMESTAMP,timeStamp);
        cv.put(KEY_NameOfCompany,nameOfCompany);        

        return ourDatabase.insert(DATABASE_TABLE,null,cv);

    }

    public SimpleCursorAdapter listUp()
    {

        String columns[]=new String[]{KEY_ROWID,KEY_TIMESTAMP,KEY_IS_SYNCED};
        Cursor c= ourDatabase.query(DATABASE_TABLE,columns,null,null,null,null,null);
        int toViewIDs[] = new int[]{R.id.rowno,R.id.timestamp,R.id.syncdetails};
        SimpleCursorAdapter CursorAdapter;
        CursorAdapter = new SimpleCursorAdapter(ourContext,R.layout.design_row,c,columns,toViewIDs,0);
        return CursorAdapter;
    }

    public String getDetails(long row)
    {
        String columns[]=new String[] {KEY_ROWID, KEY_IS_SYNCED, KEY_TIMESTAMP,KEY_NameOfCompany};
        String result="";        
        Cursor c= ourDatabase.query(DATABASE_TABLE,columns,KEY_ROWID+"="+row,null,null,null,null);
        if(c!=null)
        {
            String r;
            c.moveToFirst();
            r="\nRecord No. : "+c.getString(0)+"\n\nSync Status:"+c.getString(1)+"\n\nTime of Creation:\n"+c.getString(2)+"\n\nName of Company:\n"+c.getString(3);
            return r;
        }

        return result;
    }

    public void DeleteRow(int pos)
    {
        ourDatabase.delete(DATABASE_TABLE,KEY_ROWID+"="+pos,null);
    }
}

显示结果的新活动的类.

the class for the new activity that shows the result.

package com.example.aakashmaroti.fillup;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.SQLException;


public class DisplayDetails extends ActionBarActivity implements View.OnClickListener

{

    ImageButton ibSync;
    ImageButton ibDelete;
    TextView DispDetails;
    int pos;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();

        String value="1";
        if (extras != null) {
           value = extras.getString("position");
        }
        pos=Integer.parseInt(value);
        setContentView(R.layout.activity_display_details);
        ibSync=(ImageButton)findViewById(R.id.imageButtonSync);
        ibDelete=(ImageButton)findViewById(R.id.imageButtonDelete);
        DispDetails = (TextView)findViewById(R.id.textViewDisplayDetails);
        String s="";

        ibSync.setOnClickListener(this);
        ibDelete.setOnClickListener(this);

        Find info1=new Find(this);
        try
        {
            info1.open();
            s=info1.getDetails(pos);
            info1.close();

        } catch (SQLException e)
        {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Oops there has been an error "+e.toString()+"", Toast.LENGTH_LONG).show();
        }
        DispDetails.setText(s);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_display_details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v)
    {
        if(v.getId()==R.id.imageButtonSync)
        {
            Toast.makeText(getApplicationContext(), "This functionality has not yet been added", Toast.LENGTH_LONG).show();
        }
        if(v.getId()==R.id.imageButtonDelete)
        {   Find info1=new Find(this);
            try
            {
                info1.open();
                info1.DeleteRow(pos);
                info1.close();

            } catch (SQLException e)
            {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Oops there has been an error "+e.toString()+"", Toast.LENGTH_LONG).show();
            }


            Toast.makeText(getApplicationContext(), "This record has been successfully deleted", Toast.LENGTH_LONG).show();
            cancel();
        }
    }

    public void cancel()
    {
        this.finish();
        return;
    }
}

列表视图中单行的Xml文件

Xml file of a single row in the listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text=""
        android:textStyle="bold"
        android:clickable="true"
        android:id="@+id/rowno"/>   

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=""
        android:clickable="true"
        android:id="@+id/timestamp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textStyle="bold"
        android:text=""
        android:clickable="true"
        android:id="@+id/syncdetails"/>

</LinearLayout>

任何帮助将不胜感激.

先谢谢了.

推荐答案

是因为您要传递列表视图项的位置,所以它应该是数据库的行ID.

Its because you are passing the list view item position, it should be the database row id.

更改

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

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    TextView pos = (TextView) view.findViewById(R.id.rowno);
    showsDialog(pos.getText().toString());
}

您也说过

Once I delete any entry the row id on the listview doesn't seem to update the row number. (if i delete the first row, the row on top shows 2(should be 1, as it is the new first row)).

但这不是因为这是数据库行ID,而不是列表视图位置.

But it wouldn't because this is the database row id not the list view position.

这篇关于SQLite行ID不匹配ListView-ANDROID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆