Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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
Java 在Android中将SQLite数据库中的数据显示到ListView中_Java_Android_Sqlite - Fatal编程技术网

Java 在Android中将SQLite数据库中的数据显示到ListView中

Java 在Android中将SQLite数据库中的数据显示到ListView中,java,android,sqlite,Java,Android,Sqlite,虽然这个问题在这里被问了很多次,但我还是找不到适合我的代码的正确答案。我意识到这可能是个小问题,但我似乎找不到问题所在,因为我对这个问题还真的很陌生 以下是我在DatabaseHelper类中的代码getClientNames: public Cursor getSitesByClientname(String id) { String[] args={id}; Cursor myCursor = db.rawQuery("SELECT client_sitename FROM " +

虽然这个问题在这里被问了很多次,但我还是找不到适合我的代码的正确答案。我意识到这可能是个小问题,但我似乎找不到问题所在,因为我对这个问题还真的很陌生

以下是我在DatabaseHelper类中的代码getClientNames

public Cursor getSitesByClientname(String id) {
    String[] args={id};

Cursor myCursor = db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE + " WHERE client_id=?", args);
        String results = "";
        /*int count = myCursor.getCount();
        String[] results = new String[count + 1];
        int i = 0;*/

         if (myCursor != null) {           
                if(myCursor.getCount() > 0)
                {   
                    for (myCursor.moveToFirst(); !myCursor.isAfterLast(); myCursor.moveToNext())
                    {
                        results = results + myCursor.getString(myCursor.getColumnIndex("client_sitename")); 
                    }
                }
            }
            return results;
}
一个问题是,我将返回一个“字符串”到“游标”,但我不确定最好的解决方法是什么,因为我认为应该返回游标

下面是我要显示数据的ClientSites类:

public class ClientSites extends Activity {

//public final static String ID_EXTRA="com.example.loginfromlocal._ID";

        private DBUserAdapter dbHelper = null;
    private Cursor ourCursor = null;
    private Adapter adapter=null;


    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.client_sites);

            Intent i = getIntent();
            String uID = String.valueOf(i.getIntExtra("userID", 0));

            ListView myListView = (ListView)findViewById(R.id.myListView);

            dbHelper = new DBUserAdapter(this);

            //dbHelper.createDatabase();

            dbHelper.openDataBase();

            ourCursor = dbHelper.getSitesByClientname(uID);
            Log.e("ALERT", uID.toString());

            startManagingCursor(ourCursor);
            Log.e("ERROR", "After start manage cursor: ");

            //@SuppressWarnings("deprecation") 
            //SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.id.myListView, null, null, null);
            CursorAdapter adapter = new SimpleCursorAdapter(this, R.id.myListView, null, null, null, 0);
            adapter = new Adapter(ourCursor);

            //Toast.makeText(ClientSites.this, "Booo!!!", Toast.LENGTH_LONG).show();

            myListView.setAdapter(adapter);

            myListView.setOnItemClickListener(onListClick);


        }
        catch (Exception e)
        {
            Log.e("ERROR", "XXERROR IN CODE: " + e.toString());
            e.printStackTrace();
        } 

    }

    private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,
                                View view, int position,
                                long id) 
        {
            Intent i=new Intent(ClientSites.this, InspectionPoints.class);

            i.putExtra(ID_EXTRA, String.valueOf(id));
            startActivity(i);

        }
    };


    class Adapter extends CursorAdapter {
        @SuppressWarnings("deprecation")
        Adapter(Cursor c) {
          super(ClientSites.this, c);
        }

        //@Override
        public void bindView(View row, Context ctxt,
                           Cursor c) {
            Holder holder=(Holder)row.getTag();
            holder.populateFrom(c, dbHelper);
        }
        @Override
        public View newView(Context ctxt, Cursor c,
                           ViewGroup parent) {
          LayoutInflater inflater=getLayoutInflater();
          View row = inflater.inflate(R.layout.row, parent, false);
          Holder holder=new Holder(row);
          row.setTag(holder);
          return(row);
        }
      }

    static class Holder {
        private TextView name=null;

        Holder(View row) {
            name=(TextView)row.findViewById(R.id.ingredientText);
        }

        void populateFrom(Cursor c, DBUserAdapter r) {
            name.setText(r.getName(c));
        }
    }
}
公共类ClientSites扩展活动{
//public final静态字符串ID_EXTRA=“com.example.loginfromlocal._ID”;
私有DBUserAdapter dbHelper=null;
私有游标ourCursor=null;
专用适配器=空;
@抑制警告(“弃用”)
@SuppressLint(“新API”)
创建时的公共void(Bundle savedInstanceState){
尝试
{
super.onCreate(savedInstanceState);
setContentView(R.layout.client_站点);
Intent i=getIntent();
stringuid=String.valueOf(i.getIntExtra(“userID”,0));
ListView myListView=(ListView)findViewById(R.id.myListView);
dbHelper=新的DBUserAdapter(this);
//dbHelper.createDatabase();
dbHelper.openDataBase();
ourCursor=dbHelper.getSitesByClientname(uID);
Log.e(“警报”,uID.toString());
startManagingCursor(我们的光标);
Log.e(“错误”,“启动后管理光标:”);
//@抑制警告(“弃用”)
//SimpleCursorAdapter=新的SimpleCursorAdapter(getBaseContext(),R.id.myListView,null,null);
CursorAdapter=new SimpleCursorAdapter(this,R.id.myListView,null,null,0);
适配器=新适配器(光标);
//Toast.makeText(ClientSites.this,“Booo!!!”,Toast.LENGTH\u LONG.show();
myListView.setAdapter(适配器);
myListView.setOnItemClickListener(onListClick);
}
捕获(例外e)
{
Log.e(“ERROR”,“XXERROR IN CODE:+e.toString());
e、 printStackTrace();
} 
}
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener(){
公共无效MClick(AdapterView父级、,
视图,int位置,
长id)
{
意图i=新意图(ClientSites.this、InspectionPoints.class);
i、 putExtra(ID_EXTRA,String.valueOf(ID));
星触觉(i);
}
};
类适配器扩展了游标适配器{
@抑制警告(“弃用”)
适配器(光标c){
超级(ClientSites.this,c);
}
//@凌驾
public void bindView(视图行、上下文ctxt、,
光标(c){
Holder Holder=(Holder)行.getTag();
holder.populateFrom(c,dbHelper);
}
@凌驾
公共视图新建视图(上下文ctxt,光标c,
视图组(父级){
LayoutInflater充气机=getLayoutInflater();
视图行=充气机。充气(R.layout.row,父级,false);
持有人=新持有人(世界其他地区);
row.setTag(支架);
返回(行);
}
}
静态类持有者{
私有文本视图名称=null;
支架(视图行){
name=(TextView)row.findViewById(R.id.IngreditText);
}
无效populateFrom(光标c、DBUserAdapter){
name.setText(r.getName(c));
}
}
}
下面是我现在用来尝试在Listview中显示数据的代码。我已经改变了我最初的尝试,但仍然不确定我做错了什么

public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.client_sites);

            Intent i = getIntent();
            String uID = String.valueOf(i.getIntExtra("userID", 0));
            //int uID = i.getIntExtra("userID", 0);

            //ListView myListView = (ListView)findViewById(R.id.myListView);

            dbHelper = new DBUserAdapter(this);

            dbHelper.createDatabase();

            dbHelper.openDataBase();

            String[] results = dbHelper.getSitesByClientname(uID);

            //setListAdapter(new ArrayAdapter<String>(ClientSites.this, R.id.myListView, results));
            //adapter = new ArrayAdapter<String>(ClientSites.this, R.id.myListView, results);
            setListAdapter(new ArrayAdapter<String>(ClientSites.this, R.layout.client_sites, results));

            //ListView myListView = (ListView)findViewById(R.id.myListView);
            ListView listView = getListView();
            listView.setTextFilterEnabled(true);

            //ourCursor = dbHelper.getSitesByClientname(uID);
            //Log.e("ALERT", uID.toString());

            //startManagingCursor(ourCursor);
            //Log.e("ERROR", "After start manage cursor: ");

            //@SuppressWarnings("deprecation") 
            //SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.id.myListView, null, null, null); // LOOK AT THIS IN THE MORNING!!!!!!!!!!! 
            //CursorAdapter adapter = new SimpleCursorAdapter(this, R.id.myListView, null, null, null, 0);
            //adapter = new Adapter(ourCursor);

            //Toast.makeText(ClientSites.this, "Booo!!!", Toast.LENGTH_LONG).show();

            //myListView.setAdapter(adapter);

            //myListView.setOnItemClickListener(onListClick);
public void onCreate(Bundle savedInstanceState){
尝试
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.client_站点);
Intent i=getIntent();
stringuid=String.valueOf(i.getIntExtra(“userID”,0));
//int uID=i.getIntExtra(“userID”,0);
//ListView myListView=(ListView)findViewById(R.id.myListView);
dbHelper=新的DBUserAdapter(this);
dbHelper.createDatabase();
dbHelper.openDataBase();
String[]results=dbHelper.getSitesByClientname(uID);
//setListAdapter(新的ArrayAdapter(ClientSites.this,R.id.myListView,results));
//adapter=newarrayadapter(ClientSites.this、R.id.myListView、results);
setListAdapter(新的ArrayAdapter(ClientSites.this、R.layout.client_sites、results));
//ListView myListView=(ListView)findViewById(R.id.myListView);
ListView ListView=getListView();
setTextFilterEnabled(true);
//ourCursor=dbHelper.getSitesByClientname(uID);
//Log.e(“警报”,uID.toString());
//startManagingCursor(我们的光标);
//Log.e(“错误”,“启动后管理光标:”);
//@抑制警告(“弃用”)
//SimpleCursorAdapter=新的SimpleCursorAdapter(getBaseContext(),R.id.myListView,null,null);//早上看这个!!!!!!!!!!!
//CursorAdapter=new SimpleCursorAdapter(this,R.id.myListView,null,null,0);
//适配器=新适配器(光标);
//Toast.makeText(ClientSites.this,“Booo!!!”,Toast.LENGTH\u LONG.show();
//myListView.setAdapter(适配器);
//myListView.setOnItemClickListener(onListClick);

如果要从dbHelper返回光标,可以执行以下操作

public Cursor getSitesByClientname(String id) {
    String[] args={id};

return db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE + " WHERE client_id=?", args);
}

我还需要花一些时间阅读我从数据库创建的Listview

这是数据库ha的一部分
public List<Category> getAllCategorys() {
        ArrayList<Category> categoryList = new ArrayList<Category>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CATEGORY;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        try{
            if (cursor.moveToFirst()) {
                do {
                    Category category = new Category();
                    category.setID(Integer.parseInt(cursor.getString(0)));
                    category.setCategory(cursor.getString(1));
                    // Adding category to list
                    categoryList.add(category);
                } while (cursor.moveToNext());
            }   
        }finally{
            cursor.close();
        }   
        db.close();
        // return category list
        return categoryList;
int size = db.getCategoryCount();
List<Category> categoryList = db.getAllCategorys();

category_data = new String[size-1];
int i=0;
for(Category cn : categoryList)
{   
    category_data[i] = cn.getCategory(); // get the name of the category and add it to array
    i++;   
} 

listAdapter = new ArrayAdapter<String>(this, R.layout.categoryrow, category_data);
listViw.setAdapter(listAdapter);
public List<String> getSitesByClientname(String id) {
    String[] args={id};
    ArrayList<String> result = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor myCursor = db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE +      " WHERE client_id=?", args);

    try{
       if (myCursor.moveToFirst()){
           do{
              result.add(myCursor.getString(myCusor.getString(myCursor.getColumnIndex("client_sitename"));
           }while(myCursor.moveToNext());
        }
    }finally{
         myCursor.close();
    }
    db.close();
    return result;
}
List<String> sites_data = dbHelper.getSitesByClientname(uID);

result_data = new String[sites_data.size()];
int i=0;
for(String s : sites_data)
{   
    result_data[i] = s; // get the name of the category and add it to array
    i++;   
} 

listAdapter = new ArrayAdapter<String>(this, R.layout.client_sites, result_data);
listViw.setAdapter(listAdapter);