Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在listview中设置sqlite数据库值_Android_Sqlite_Listview - Fatal编程技术网

Android在listview中设置sqlite数据库值

Android在listview中设置sqlite数据库值,android,sqlite,listview,Android,Sqlite,Listview,我在android中设置ListView时遇到了一个问题。一切正常,但它没有设置的方式,我希望它设置。这是正在发生的事情的图片 当我添加数据时,它显示良好并准确插入数据,但当我返回并再次启动活动时,它检索数据并将其显示在listview的单行中 1) 我想在listview的不同项目上显示每个条目的数据。我不知道怎样才能做到 2) 当我启动活动时,它应该显示所有的数据库条目,当我添加新项目时,它应该附加上一个列表。但实际发生的情况是,当已经有3条记录显示时,我再添加一条记录,它显示最后添加的

我在android中设置ListView时遇到了一个问题。一切正常,但它没有设置的方式,我希望它设置。这是正在发生的事情的图片

当我添加数据时,它显示良好并准确插入数据,但当我返回并再次启动活动时,它检索数据并将其显示在listview的单行中

1) 我想在listview的不同项目上显示每个条目的数据。我不知道怎样才能做到

2) 当我启动活动时,它应该显示所有的数据库条目,当我添加新项目时,它应该附加上一个列表。但实际发生的情况是,当已经有3条记录显示时,我再添加一条记录,它显示最后添加的一条记录。以前添加的记录消失了。我不知道他们为什么不展示

这是我的代码

public class noticall extends ListActivity {


    CustomDateTimePicker custom;
    TextView tv,tv1;
    EditText ed;
    String store;
    static String Names;
    public static final int PICK_CONTACT = 1;
    String [] adi ;
    String [] adi1 ;
    String [] adi2 ;
    String [] adi3 ;
    static int incre=0;
    ArrayList<String> myrows = new ArrayList<String>();
    ArrayList<String> time1 = new ArrayList<String>();
    ArrayList<String> name1 = new ArrayList<String>(); 
    ArrayList<String> number1 = new ArrayList<String>();
    private PendingIntent pendingIntent;
    ListView listview;
    String temp1 ,temp2 ;
    int x=0; 
    OnItemClickListener listener;
    String Date,Time,Name,Number;
    MyNotiAdapter adp;


    private SQLiteAdapter mySQLiteAdapter = new SQLiteAdapter(noticall.this);



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notify);
            listview = (ListView) findViewById(android.R.id.list);

           listview.setDividerHeight(2);



    custom = new CustomDateTimePicker(noticall.this,new CustomDateTimePicker.ICustomDateTimeListener() {

               @Override
               public void onCancel() { 
                   finish();
               }


               @Override
               public void onSet(Dialog dialog, Calendar calendarSelected,Date dateSelected, int year, String monthFullName,
                            String monthShortName, int monthNumber, int date,
                            String weekDayFullName, String weekDayShortName,
                            int hour24, int hour12, int min, int sec,
                            String AM_PM) {

                      Calendar calendar =  Calendar.getInstance();
                      calendar.set(year, monthNumber, date, hour24,  min, 0);

                      long when = calendar.getTimeInMillis();         // notification time

                      Intent myIntent = new Intent(noticall.this, MyReceiver.class);
                      pendingIntent = PendingIntent.getBroadcast(noticall.this, 0, myIntent,0);

                      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);


        Date = calendarSelected.get(Calendar.DAY_OF_MONTH) + "/" + (monthNumber+1) + "/" +year; 
        Time = hour12 + ":" + min + " " + AM_PM;


        setlisto(Date);


        //Toast.makeText(noticall.this, store, Toast.LENGTH_SHORT).show();

           }
    });      



            custom.set24HourFormat(false);
            custom.setDate(Calendar.getInstance());



                findViewById(R.id.button_date).setOnClickListener(
                    new OnClickListener() {
                        @Override
                        public void onClick(View v) {



                        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(intent,PICK_CONTACT);
                        custom.showDialog();
                        incre++;
                        }
                    });



                read_db();

    }




     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data)
     {
       super.onActivityResult(requestCode, resultCode, data);

        if(resultCode != RESULT_CANCELED){

            if (requestCode == 1)
            {
                // Get the URI that points to the selected contact
                Uri contactUri = data.getData();

                // We only need the NUMBER column, because there will be only one row in the result
                String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID};

                String[] segments = contactUri.toString().split("/");
                String id = segments[segments.length - 1];

                // Perform the query on the contact to get the NUMBER column
                // We don't need a selection or sort order (there's only one result for the given URI)
                // CAUTION: The query() method should be called from a separate thread to avoid blocking
                // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                // Consider using CursorLoader to perform the query.
                Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
                cursor.moveToFirst();
                while (!cursor.isAfterLast())
                {
                    int cid = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
                    String contactid = cursor.getString(cid);

                    if (contactid.equals(id))
                    {
                        // Retrieve the phone number from the NUMBER column
                        int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        String number = cursor.getString(column);

                        // Retrieve the contact name from the DISPLAY_NAME column
                        int column_name = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                        String name = cursor.getString(column_name);

                        // Do something with the phone number...
                       // Toast.makeText(this, "I added the Contact: \n" + name + " " + number, Toast.LENGTH_SHORT).show();

                       // ed.setText(name+" - "+number);

                      Name=  Names=name;
                      Number =number;  



                    }

                 cursor.moveToNext();
                }
                cursor.close();


               }
            }
        }


     public void setlisto(String one ){



    // Log.e("setlistoo",one+"");
     myrows.add(one);
     time1.add(Time);
     name1.add(Name);
     number1.add(Number);

      adi=myrows.toArray(new String[myrows.size()]);
     adi1=time1.toArray(new String[time1.size()]);
     adi2=name1.toArray(new String[name1.size()]);
     adi3=number1.toArray(new String[number1.size()]);
     x++;
     listview.setAdapter(new MyNotiAdapter(noticall.this, adi,adi1,adi2,adi3));



     Log.i("a=",one+"");

     Log.i("b=",Time+"");

     Log.i("c=",Name+"");

     Log.i("d=",Number+"");



     mySQLiteAdapter = new SQLiteAdapter(noticall.this);
     mySQLiteAdapter.openToWrite();

     mySQLiteAdapter.insert(one,Time,Name,Number);


     mySQLiteAdapter.close();


  }
     public void del_db(){

         mySQLiteAdapter = new SQLiteAdapter(noticall.this);
         mySQLiteAdapter.openToWrite();

         mySQLiteAdapter.deleteAll();

     }



     public void read_db(){

         Log.e("jsahbdv", "munda agaya medaan men hay jamaalo");


            mySQLiteAdapter.openToRead();

            Cursor cursor = mySQLiteAdapter.getAllData();

            startManagingCursor(cursor);

            String[] from = new String[]{SQLiteAdapter.KEY_Name,SQLiteAdapter.KEY_Number,SQLiteAdapter.KEY_Date,SQLiteAdapter.KEY_Time};
            int[] to = new int[]{R.id.dato,R.id.tym,R.id.person,R.id.edu};

           SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(noticall.this, R.layout.row2, cursor, from, to);



           while (cursor.moveToNext()){



               cursor.getString(0);
               cursor.getString(1);
               cursor.getString(2);
               cursor.getString(3);
               cursor.getString(4);



            /*   strBfr.append("Id :"+ cursor.getString(0) + "\n");
               strBfr.append("Name:" + cursor.getString(1) + "\n");
               strBfr.append("Number:" +cursor.getString(2) + "\n");
               strBfr.append("Date:" + cursor.getString(3) + "\n");
               strBfr.append("Time:" + cursor.getString(4) + "\n");

             */  

               Log.i("Error", cursor.getString(0)+"");
               Log.i("Error", cursor.getString(1)+"");
               Log.i("Error", cursor.getString(2)+"");
               Log.i("Error", cursor.getString(3)+"");
               Log.i("Error 8", cursor.getString(4)+"");


               listview.setAdapter(cursorAdapter);

     }

     }


}
与此相关=


如何设置此问题,或者如何根据ID读取行,然后设置为listview。。伙计们,请帮帮我。

您正在列表视图中使用多个适配器

listview.setAdapter(new MyNotiAdapter(noticall.this, adi,adi1,adi2,adi3));
后来

listview.setAdapter(cursorAdapter);
当代码运行时,您会看到一些意想不到的结果

例如,第二个图像看起来像是在一行中显示Arraylist,而不是每行显示一个元素

这可能是由这行代码引起的,在这行代码中,您将字符串化一些ArrayList

mySQLiteAdapter.insert(myrows.toString(),time1.toString(),name1.toString(),number1.toString());
因此,我的建议是使用一个适配器(可能只是一个游标适配器,因为您使用的是SQLite)来显示数据。您可以在
onCreate
中只设置适配器一次,而无需再次设置。您只需重新加载光标

还有一件事需要注意。将光标添加到适配器后,不要对其进行迭代。因为适配器读取光标并显示其内容,所以您稍后将耗尽迭代器

Cursor cursor = mySQLiteAdapter.getAllData();
startManagingCursor(cursor);
// ...
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(noticall.this, R.layout.row2, cursor, from, to);

while (cursor.moveToNext()){ // <--- This is bad
Cursor Cursor=mySQLiteAdapter.getAllData();
开始管理游标(游标);
// ...
SimpleCursorAdapter cursorAdapter=新的SimpleCursorAdapter(noticeAll.this,R.layout.row2,游标,从,到);

而(cursor.moveToNext()){/您正在列表视图中使用多个适配器

listview.setAdapter(new MyNotiAdapter(noticall.this, adi,adi1,adi2,adi3));
后来

listview.setAdapter(cursorAdapter);
当代码运行时,您会看到一些意想不到的结果

例如,第二个图像看起来像是在一行中显示Arraylist,而不是每行显示一个元素

这可能是由这行代码引起的,在这行代码中,您将字符串化一些ArrayList

mySQLiteAdapter.insert(myrows.toString(),time1.toString(),name1.toString(),number1.toString());
因此,我的建议是使用一个适配器(可能只是一个游标适配器,因为您使用的是SQLite)来显示您的数据。并且您可以在
onCreate
中只
setAdapter
一次,并且无需再次设置。您只需重新加载游标

另一件需要注意的事情。在将光标添加到适配器后,不要对其进行迭代。因为适配器读取光标并显示其内容,所以您稍后将耗尽迭代器

Cursor cursor = mySQLiteAdapter.getAllData();
startManagingCursor(cursor);
// ...
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(noticall.this, R.layout.row2, cursor, from, to);

while (cursor.moveToNext()){ // <--- This is bad
Cursor Cursor=mySQLiteAdapter.getAllData();
开始管理游标(游标);
// ...
SimpleCursorAdapter cursorAdapter=新的SimpleCursorAdapter(noticeAll.this,R.layout.row2,游标,从,到);

虽然(cursor.moveToNext()){//您的sqlite适配器类不是适配器…它最好扩展SqliteOpenHelper…无论如何,不知道没有XML布局会出现什么问题?另外,为什么有
getContentResolver().query(ContactsContract
?这没有任何作用,是吗?SqliteOpenHelper在sqlite类和getContentResolver()中初始化(ContactsContract用于选择联系人并获取他们的姓名和号码。您的sqlite适配器类不是适配器…它最好扩展SqliteOpenHelper…无论如何,如果没有XML布局,还不清楚问题出在哪里?另外,为什么有
getContentResolver().query(ContactsContract
?这没有任何作用,是吗?SqliteOpenHelper在sqlite类和getContentResolver()中初始化(contacts contract用于选择联系人并获取他们的姓名和号码。我不确定您需要什么示例,但首先要解决删除
MyNotiAdapter
的问题,在将数组列表插入数据库时不要对其进行字符串化,而是将每个元素添加到for looparraylist中要添加到数据库中。MyNotiAdapter很重要,因为我正在使用它来显示添加的条目。稍后,游标适配器用于显示从sqlite检索到的数据。您可以向我提供将sqlite的多个列检索到ListViews的示例。我已经根据您的建议更改了代码,现在它没有显示repeating条目,但它只读取一条记录。我可以将MynotiAdapter用于sqlite结果吗?我可以共享它的代码吗?您已经正确地获取了行中的列。
SimpleCursorAdapter cursorAdapter=new SimpleCursorAdapter(notiall.this,R.layout.row2,cursor,from,to);
int[]to
是要将列绑定到的布局的ID。您是否不应该
toString
一个
数组列表
,因为您看到的是
[data1,data2,data3]
在一行上…我不确定您需要什么示例,但首先要修复删除
MyNotiAdapter
的问题,不要在将ArrayList插入数据库时将其字符串化,而是将每个元素添加到for looparraylist中。ToStarting只是我要添加到数据库中的字符串。MyNotiAdapter同样重要我正在使用它来显示添加的条目。稍后,游标适配器用于显示从sqlite检索的数据。您可以向我提供检索多个sqlite列到ListViews的示例。我已经根据您的建议更改了代码。现在它不显示重复条目,但只读取一条记录。我可以使用MynotiAdapt吗对于sqlite结果,我也可以分享它的代码