代码之家  ›  专栏  ›  技术社区  ›  Pentium10

如何在android上获取表列arraylist?

  •  8
  • Pentium10  · 技术社区  · 15 年前

    我正在寻找一个代码来获取android中一个表的可用列名?

    我环顾四周,什么也没发现。

    7 回复  |  直到 9 年前
        1
  •  21
  •   Tim Cooper    11 年前

    这是一种更简单的方法:

    Cursor ti = db.rawQuery("PRAGMA table_info(mytable)", null);
    if ( ti.moveToFirst() ) {
        do {
            System.out.println("col: " + ti.getString(1));
        } while (ti.moveToNext());
    }
    
        2
  •  14
  •   Zul    13 年前

    我实现的最简单的方法。

    Cursor cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 1", null);
    String[] colNames = cursor.getColumnNames();
    

    欢迎提出更好的建议。

        3
  •  2
  •   Pentium10    15 年前
        try {
            Cursor c = db.query(tableName, null, null, null, null, null, null);
            if (c != null) {
                int num = c.getColumnCount();
                for (int i = 0; i < num; ++i) {
                    String colname = c.getColumnName(i);
    
                }
            }
    
        } catch (Exception e) {
            Log.v(tableName, e.getMessage(), e);
            e.printStackTrace();
        }
    
        4
  •  2
  •   karthik    12 年前
    public class Database_demo extends Activity {
    private String jsonResult;
    public static final int DIALOG_DOWNLOAD_JSON_PROGRESS = 0;
    private String url = "http://192.168.43.2/brother/song.php";
    String urll = url;
    private ListView listView;
    private static final String HTTP_REQUEST_FAILED = null;
    int[] flags;
    String strImageName;
    int n = 10000;
    String[] mySecondStringArray = new String[n];
    String[] strArray;
    private ProgressDialog mProgressDialog;
    HashMap<String, String> hm;
    List<HashMap<String, String>> aList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        listView = (ListView) findViewById(R.id.listview);
        try {
            accessWebService();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "Check ur network connection", 10000).show();
        }
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_JSON_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Please wait.....");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
    
        default:
            return null;
        }
    }
    
    // Async Task to access the web
    private class JsonReadTask extends AsyncTask<String, Void, String> {
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
        }
    
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            }
    
            catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    
            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            }
    
            catch (IOException e) {
                // e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }
    
        @Override
        protected void onPostExecute(String result) {
    
            ListDrwaer();
    
            dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    
        }
    }
    
    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[] { url });
    }
    
    public void ListDrwaer() {
    
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("products");
    
            aList = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("field");
    
    
                hm = new HashMap<String, String>();
                hm.put("txt", name);
                //hm.put("cur", number);
    
                aList.add(hm);
    
            }
        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        String[] from = { "flag", "txt" };
        int[] to = { R.id.flag, R.id.txt };
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
                R.layout.news, from, to);
        listView.setAdapter(adapter);
    
    
    }
    

    }

        5
  •  1
  •   Scott    10 年前

    zul方法的另一种选择是使用columncount和他的简单示例,并且限制为0。

    Cursor cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 0", null);
    int colCount = cursor.getColumnNames().length();
    
        6
  •  0
  •   Vahe Gharibyan    10 年前

    数据库映射

     private HashMap<String, String[]> getDataBaseMap(SQLiteDatabase db){
    
        HashMap<String, String[]> DBMap = new HashMap<>();
        ArrayList<String> tableNames = new ArrayList<>();
        String[] columns ;
    
        Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    
        if (c.moveToFirst()) {
            while ( !c.isAfterLast() ) {
                tableNames.add(c.getString(0));
                c.moveToNext();
            }
        }
        c.close();
    
        for ( int i = 0 ; i < tableNames.size() ; i++){
            Cursor  cursor = db.rawQuery("select * from " + tableNames.get(i),null);
            columns = cursor.getColumnNames();
            cursor.moveToNext();
    
            DBMap.put(tableNames.get(i),columns);
            cursor.close();
    
        }
    
        return DBMap;
    }
    
        7
  •  0
  •   user6224849    9 年前

    试试这个:

    最好使用 数据操作 实例和使用查询方法

    SQLiteDatabase mDataBase;
    (some code here...)
    mDataBase = getReadableDatabase();
    Cursor dbCursor = mDataBase.query(TABLE_NAME, null, null, null, null, null, null);
    String[] columnNames = dbCursor.getColumnNames();
    
    推荐文章