【问题标题】:Using SQLite with ListFragment makes problems将 SQLite 与 ListFragment 一起使用会产生问题
【发布时间】:2013-03-23 14:08:13
【问题描述】:

我正在使用 ListFragment 来显示数据库中的数据。但是数据显示不正确。

在我的 Fragment 中,在用户单击 ActionBar 项并插入名称后,我将联系人添加到 SQLite 数据库:

//Handle OnClick events on ActionBar items
@Override
public boolean onOptionsItemSelected(MenuItem item) {
   // handle item selection
   switch (item.getItemId()) {
      case R.id.menu_add:
         Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();

         LayoutInflater factory = LayoutInflater.from(getActivity());

         //textEntryView is an Layout XML file containing text field to display in alert dialog
         textEntryView = factory.inflate(R.layout.dialog_add_room, null);       

         //get the control from the layout      
         enter_room = (EditText) textEntryView.findViewById(R.id.enter_room);

         //create Dialog
         final AlertDialog.Builder alert1 = new AlertDialog.Builder(getActivity());
         //configure dialog
         alert1.setTitle("Raum hinzufügen:").setView(textEntryView)
         .setPositiveButton("Hinzufügen",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int whichButton) {
                String roomname = enter_room.getText().toString();

                Log.d("Insert: ", "Inserting ..");
                dbHandler.addContact(new Contact(roomname, "0"));

                adapter.notifyDataSetChanged();
            }
         }).setNegativeButton("Abbrechen",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int whichButton) {
                //cancel dialog
            }
         });
         alert1.show();          
         return true;
      default:
         return super.onOptionsItemSelected(item);
   }
}

然后我想获取所有联系人并使用以下代码将它们显示在 ListFragment 中:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //display ActionBar items
        setHasOptionsMenu(true);

        //database
        dbHandler = new DatabaseHandler(getActivity());

        //get all contacts
        contacts = dbHandler.getAllContacts();

        adapter = new ArrayAdapter<Contact>(getActivity(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1, contacts);

        adapter.setNotifyOnChange(true);

        //show all contacts in the ListFragment
        setListAdapter(adapter);
    }

我的 DatabaseHandler 中的 getAllContacts():

// Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to 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));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

但在我的 ListFragment 中显示了以下内容:

com.example.fragmentmasterdetail.sqlite.Contact@21059ed8

我想查看联系人的姓名。有人有想法吗?

【问题讨论】:

  • com.example.fragmentmasterdetail.sqlite.Contact@21059ed8toString()(每个 Java Object 类中的成员)的默认结果,只需覆盖它即可返回有用的内容。

标签: android android-fragments android-sqlite android-listfragment


【解决方案1】:

根据ArrayAdapter documentation

无论如何引用 TextView,它都会被数组中每个对象的 toString() 填充。您可以添加自定义对象的列表或数组。覆盖对象的 toString() 方法以确定列表中的项目将显示哪些文本。

因此,要将自定义文本添加到您的列表中,请将 toString() 方法添加到您的 Contact 类:

public String toString()
{
    return name;
}

【讨论】:

  • 我已经实现了这个功能,但是我必须在哪里使用这个功能?抱歉这个简单的问题...
  • 我已经编辑了我的答案 - ArrayAdapters 使用 toString() 来确定要为每个项目填写的文本。它由 ArrayAdapter 调用,而不是您手动调用
猜你喜欢
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 2015-03-08
  • 2016-11-07
  • 1970-01-01
  • 2017-11-14
相关资源
最近更新 更多