0

请看下面的代码

数据库处理程序.java

用于检索数据的代码

public List<String> getAllBranches() {
        // TODO Auto-generated method stub

        String selectQuery = "select city from BranchNetwork";

        Cursor cursor = database.rawQuery(selectQuery, null);
        List <String>branches = new ArrayList<String>();

        if(cursor.isFirst())
        {
            do
            {

            }
            while(cursor.moveToNext());

            Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
        }

        return branches;
    }

用于插入数据的代码

public String insertData(String city, String streetAddress,String phoneNumber1, String phoneNumber2, String email) 
    {
        String insertQuery = "insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values('"+city+"','"+streetAddress+"','"+phoneNumber1+"','"+phoneNumber2+"','"+email+"');";




        try
        {  
            database.execSQL(insertQuery);


            return "Data Successfully Inserted";

        }
        catch(Exception e)
        {
            Toast.makeText(context, "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
            return "Data Insertion Failed";
        }

    }

表单.java

第一个活动。在这里,我检索数据并插入数据。以下是我检索数据的方式

public class Form extends Activity {
    private List<String>branches = new ArrayList<String>();
        private DatabaseConnector databaseConnector;
    String[]arr;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_form);

            databaseConnector = DatabaseHandler.getInstance(this);
            databaseConnector.createConnection();

            branches = databaseConnector.getAllBranches();

             arr = branches.toArray(new String[branches.size()]);
    }
}

但是,我什么都得不到!在 DatabaseHandler.java 中,我创建了一个Toast在检索代码时应该生成消息的位置。但它没有产生任何东西!我正在将数据加载到 aListView但它也是空的!为什么是这样?请帮忙!

PS:下面是DatabaseConnector.java,DatabaseHandler的插入面

package com.example.esoftcallmanager;

import java.util.List;

import android.content.Context;

public interface DatabaseConnector 
{
    public void createConnection();
    public void closeConnection();
    public String getPhoneNumber();
    public String insertData(String city, String streetAddress, String phoneNumber1, String phoneNumber2, String email);
    public List<String>getAllBranches();

}
4

5 回答 5

2

如果您确定您的数据库已经有数据,请尝试更改if(cursor.isFirst())if(cursor.moveToFirst())

于 2013-02-14T07:56:53.853 回答
1

试试 cursor.moveToFirst() 条件

if(cursor.moveToFirst())
    {
        do
        {

        }
        while(cursor.moveToNext());

        Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
    }
于 2013-02-14T07:58:43.223 回答
1

那是因为你并没有真正对数据做任何事情。

首先,您必须将光标的位置移动到自己的第一个位置: cursor.moveToFirst() 之后,您可以开始一个 while 循环,因为您正在尝试这样做。

其次,你从来没有真正将数据放在你的列表分支中,你必须先把光标中的数据放在那里。

于 2013-02-14T08:00:34.210 回答
1

希望这有效。

public List<String> getAllBranches() {
            // TODO Auto-generated method stub

            String selectQuery = "select city from BranchNetwork";
            Cursor cursor = database.rawQuery(selectQuery, null);
            List <String>branches = new ArrayList<String>();

             if(cursor != null){
              cursor.moveToFirst();

               Toast.makeText(context, "Data Retrieved: ", Toast.LENGTH_LONG).show();

              do
                {

                }
                while(cursor.moveToNext());



             }



                return branches;
        }
于 2013-02-14T08:02:29.293 回答
0

您忘记在列表数组中添加数据。尝试如下:

  String selectQuery = "select city from BranchNetwork";
    Cursor cursor = database.rawQuery(selectQuery, null);
    List <String> branches = new ArrayList<String>();
    String str=null;
    cursor.moveToFirst();
        do
       {
         str= m_cursor.getString(m_cursor.getColumnIndex("city"));
          branches.add(str);
       }
        while(cursor.moveToNext());
        Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
    }
        return branches;
于 2013-02-14T08:02:30.417 回答