SQLite在TextViews中存储ListView对象



我有一个SQLite数据库,在数组列表中存储联系人。然后使用这个数组列表创建对象并将它们添加到listview中。当我尝试使用侦听器单击一个项目时,设置为不包含任何内容的Textviews。通过日志检查后,由于某种原因,所有的textview都包含空值。我假设onClick没有从数据库中提取任何信息,但我不知道如何解决这个问题。有人知道哪里出了问题吗?谢谢。请原谅我过多的评论。

onCreate用于填充列表视图(包含侦听器)的方法

public void fillContactsList()
   {    
        // Call method to get all contact entries.
        db.getAllContacts();
        // Defines listview.
        listView = (ListView) findViewById(R.id.listView1);
        // Create adapter using entries from database.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, contactList);        
        // Set adapter in listView.
        listView.setAdapter(adapter);
        // Listener for when the user clicks a contact object.
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                /* 
                 Shows the dialog box containing the user details 
                 and any operations that they may wish to perform.
                */
                contactsView();
                //Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }           
        });     
    }

包含textviews的对话框

public  void contactsView()
    {       
         // Define alert dialog.
         AlertDialog.Builder contactView = new AlertDialog.Builder(this);
         // Define inflater.
         LayoutInflater inflater = this.getLayoutInflater();
         // Set the view  to inflate the dialog_view xml file.
         final View contactDetailsView = inflater.inflate(R.layout.dialog_view, null);
         // TextView variables.
         viewNameValue = (TextView) contactDetailsView.findViewById(R.id.viewName);
         viewMobNoValue = (TextView) contactDetailsView.findViewById(R.id.viewMobNo);
         viewEmailValue = (TextView) contactDetailsView.findViewById(R.id.viewEmail);
         viewPostcodeValue = (TextView) contactDetailsView.findViewById(R.id.viewPostcode);
         viewDateOfBirthValue = (TextView) contactDetailsView.findViewById(R.id.viewDateOfBirth);
         viewAdditionalInformationValue = (TextView) contactDetailsView.findViewById(R.id.viewAdditionalInformation);
         // TextView variables (shows the user their details).
         viewNameValue.setText(contact.getName());
         viewMobNoValue.setText(contact.getPhoneNumber());
         viewEmailValue.setText(contact.getEmail());
         viewPostcodeValue.setText(contact.getPostcode());
         viewDateOfBirthValue.setText(contact.getDateOfBirth());
         viewAdditionalInformationValue.setText(contact.getAdditionalInformation());
         // Adds a title to the dialog box.
         contactView.setTitle("Contact Details")
         // Sets dialog view to the layout provided by dialog_view xml file.
         .setView(contactDetailsView)  
         // Button used to allow the user to stop viewing their details.
         .setPositiveButton(R.string.go_back, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
             }
         })
         // Button used to allow the user to delete their details.
         .setNeutralButton(R.string.delete_contact, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                 contactOptionsDelete();
             }
         })
         // Button used to allow the user to edit their details.
         .setNegativeButton(R.string.edit, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                 contactOptionsEdit();
             }
         });
         // Creates the AlertDialog.
         contactView.create();
         // Displays the AlertDialog.
         contactView.show();
    }

填充listview的方法

public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select query to get all data.
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Clears the list (prevents duplication when used in onCreate / onResume).
        MainActivity.contactList.clear();
        // Loops through all rows, adding to the list.
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                contact.setPhoneNumber(cursor.getString(2));
                contact.setEmail(cursor.getString(3));
                contact.setPostcode(cursor.getString(4));
                contact.setDateOfBirth(cursor.getString(5));
                contact.setAdditionalInformation(cursor.getString(6));
                // Adds contact to the array list.
                String name = cursor.getString(1) +"n"+ cursor.getString(2) +"n"+ cursor.getString(3)
                              +"n"+ cursor.getString(4) +"n"+ cursor.getString(5) +"n"+ cursor.getString(6);
                MainActivity.contactList.add(name);
                // contactList.add(contact);
            } while (cursor.moveToNext());
        }
        // Returns the contact list.
        return contactList;
    }

我假设....

你验证了吗?

用一些调试语句完成你的代码,你会在LogCat中看到它。(假设您使用的是eclipse)另一种方法是设置一些断点并跟踪整个程序。这我正在做,如果调试语句不能给出所有的答案。

最新更新