1

我在按钮的单击事件上创建数据库,当我在下一个触发选择查询时,Activity两个相同的记录显示在ListView. 看不懂怎么回事??请给我建议。我有以下代码:

ListActivity.java

  package com.example.timerdemo2;

        import java.util.ArrayList;

        import android.R.menu;
        import android.os.Bundle;
        import android.provider.ContactsContract.CommonDataKinds.Phone;
        import android.annotation.SuppressLint;
        import android.app.Activity;
        import android.content.Intent;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.view.ContextMenu;
        import android.view.Menu;
        import android.view.MenuInflater;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.ContextMenu.ContextMenuInfo;
        import android.view.View.OnClickListener;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.ListView;
        import android.widget.Toast;
        import android.widget.AdapterView.AdapterContextMenuInfo;

        public class ListActivity extends Activity {
            ListView lv;
            ArrayList<String> items = new ArrayList<String>();
            ArrayList<String> items_id = new ArrayList<String>();
            dbhelper dh;
            SQLiteDatabase db;
            int index_id;
            Button btn;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_list);
                Button btn;
                btn = (Button)findViewById(R.id.newProjectlist);
                btn.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub


                            Intent intent= new Intent(ListActivity.this,NewProject.class);
                        startActivity(intent);
                    }
                });  
                lv = (ListView)findViewById(R.id.projectListView);
                setUpList();
                dh = new dbhelper(this);
                Cursor c = getAllData();
                showAllData(c);  
                registerForContextMenu(lv);      
            }

          @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                // TODO Auto-generated method stub
                super.onCreateContextMenu(menu, v, menuInfo);

                if(v.getId() == R.id.projectList){
                    menu.setHeaderIcon(R.drawable.ic_launcher);
                    menu.setHeaderTitle("Record List");
                    menu.add(0,1,menu.NONE,"Delete Record");
                    menu.add(0,2,menu.NONE,"Show Record");
                }
            }

            @Override
        public boolean onContextItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
                int itemId = item.getItemId();
                if(itemId == 1)
                {
                    AdapterView.AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
                    int poistion = menuinfo.position;
                    String id_string = items_id.get(poistion);
                    long id = Long.valueOf(id_string);

                    //menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
                    //index_id = menuinfo.position;
                    deleteUserData(index_id);
                    items_id.remove(poistion);
                }
                if(itemId == 2){
                    AdapterView.AdapterContextMenuInfo menuinfo;
                    menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
                    index_id = menuinfo.position;
                    editUserData(index_id);
                }
            return super.onContextItemSelected(item);
        }

            private void editUserData(int indexid) {
                // TODO Auto-generated method stub
                String user_id = items_id.get(indexid);
                Intent intEdit = new Intent(ListActivity.this, MainActivity.class);
                intEdit.putExtra("EditID", user_id);
                startActivityForResult(intEdit, 1);
            }

            private void deleteUserData(int indexid) {
                // TODO Auto-generated method stub
        //      String user_id = items_id.get(indexid);
                //Toast.makeText(getApplicationContext(), "id " + user_id, 1).show();
                db = dh.getWritableDatabase();
                db.delete("timer","_ID" + "=" + indexid, null);
                finish();
        //      Toast.makeText(getApplicationContext(), "Record deleted successfully", 1).show();
            }

            private void showAllData(Cursor c) {
                // TODO Auto-generated method stub
                while(c.moveToNext())
                {
                    int id = c.getInt(0);
                    String name = c.getString(1);
                    String timerpoint1 = c.getString(2);
                    String timerpoint2 = c.getString(3);
                    String timerpoint3 = c.getString(4);
                    String servicetime1 = c.getString(5);
                    String servicetime2 = c.getString(6);
                    items.add("" + name);
                    items_id.add(""+id);
                }
                setUpList();
            }

            private Cursor getAllData() {
                // TODO Auto-generated method stub
                db = dh.getReadableDatabase();
                //String sortorder = dh.Name + "DESC";
            //  String query ="SELECT DISTINCT name FROM timer";
            //  Cursor c = db.rawQuery(query, null);
            //   if (c != null) {
            //            c.moveToFirst();
            //        }
        //      String[] cols = new String[] {dh.Name, "name"};
                Cursor c = db.query(true, "timer",null,null,null,null,null,null, null);   
                //Cursor cur = db.query(Distinct,"timer", null, null, null, null, null, null, null);
                startManagingCursor(c);
                return c;   
                }
            private void setUpList() {
                // TODO Auto-generated method stub
                lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
            }   

            }

当显示记录时,ListView两个条目就完成了。我想让记录与众不同。所以提供帮助。

日志猫

01-01 18:42:49.941: W/dalvikvm(12683): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-01 18:42:49.951: E/AndroidRuntime(12683): FATAL EXCEPTION: main
01-01 18:42:49.951: E/AndroidRuntime(12683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timerdemo2/com.example.timerdemo2.ListActivity}: java.lang.IllegalStateException: get field slot from row 0 col 2 failed
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.os.Looper.loop(Looper.java:130)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at java.lang.reflect.Method.invokeNative(Native Method)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at java.lang.reflect.Method.invoke(Method.java:507)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at dalvik.system.NativeStart.main(Native Method)
01-01 18:42:49.951: E/AndroidRuntime(12683): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 2 failed
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.database.CursorWindow.getString_native(Native Method)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.database.CursorWindow.getString(CursorWindow.java:329)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at com.example.timerdemo2.ListActivity.showAllData(ListActivity.java:123)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at com.example.timerdemo2.ListActivity.onCreate(ListActivity.java:56)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-01 18:42:49.951: E/AndroidRuntime(12683):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-01 18:42:49.951: E/AndroidRuntime(12683):    ... 11 more
01-01 18:42:52.430: D/dalvikvm(12758): GC_EXTERNAL_ALLOC freed 53K, 50% free 2739K/5379K, external 716K/1038K, paused 112ms
4

1 回答 1

0

将此方法放在您的 dbhelper 类中。

public Cursor get_dist(){
 SQLiteDatabase db = this.getReadableDatabase();
 String Distinct_Query="select distinct name from timer;";
 Cursor cursor=db.rawQuery(Distinct_Query,null);
 db.close();
 return cursor;
}

这将返回具有不同记录的游标

然后将此代码添加到您的活动中

 dbhelper dh=new  dbhelper(this);
     Cursor c=dh.get_dist();

现在从游标 c 中检索记录。

于 2013-01-02T09:03:13.930 回答