如何在android中的sqlite数据库中显示listview中的数据

时间:2017-05-10 10:23:03

标签: android listview simplecursoradapter

add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_stock', 10 );
function woocommerce_template_loop_stock() {
    global $product;
    if ( ! $product->managing_stock() && ! $product->is_in_stock() )
        echo '<p class="stock out-of-stock">Out of Stock</p>';
}
  

我试图在列表视图中显示数据,但它显示异常:“java.lang.IllegalArgumentException:column'_id'不存在”

5 个答案:

答案 0 :(得分:1)

试试这个

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        createDatabase();
        c = db.rawQuery(fetch,null);


            String[] from = new String[]{"_id","name","number"};
            int[] to = new int[]{R.id.label, R.id.numberq, R.id.num};
            adapter = new SimpleCursorAdapter(this,R.layout.layout,c,from,to,0);

        ListView list = (ListView) findViewById(R.id.listview);
        list.setAdapter(adapter);
    }
    protected void createDatabase(){
        db = openOrCreateDatabase("PersonDB",Context.MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Person(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, number VARCHAR);");

    }

答案 1 :(得分:1)

正如其他人所指出的,将id名称更改为id_id

此外,使用SimpleCursorAdapter您不需要自己遍历Cursor,适配器可以帮助您。

Cursor cursor = db.rawQuery(...);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
        R.layout.layout, 
        cursor, 
        new String[]{"id", "name", "number"}, 
        new int[]{R.id.label, R.id.numberq, R.id.num});

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

答案 2 :(得分:0)

在您的代码中替换它:

public class YourActivity extends Activity implements TextWatcher {
/* initializations
 ********** 
 */

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

eAmountReceived = (EditText) findViewById(R.id.amountReceived);
eTotalCost = (EditText) findViewById(R.id.orderTotal);
eTip = (EditText) findViewById(R.id.tip);
eMileage = (EditText) findViewById(R.id.mileage);
eGrandTotal = (EditText) findViewById(R.id.grandTotal);

eAmountReceived.addTextChangedListener(this);
eTotalCost.addTextChangedListener(this);
eTip.addTextChangedListener(this);
eMileage.addTextChangedListener(this);
eGrandTotal.addTextChangedListener(this); 

// put listener only to the fields you need

}

// implement interface methods

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

// THIS IS CALLED EVERY TIME CHARACTER IS WRITTEN
// HERE YOU CAN CALL YOUR METHODS FOR CALCULATIONS  i.e. afterTextChanged

afterTextChangedCalculations();

}

@Override
public void afterTextChanged(Editable editable) {

// This method is called to notify you that, somewhere within s, the text has been changed.

}

答案 3 :(得分:0)

替换

    public class Iso4217Lookup
    {
        private static Iso4217Definition _notFoundDefinition = new Iso4217Definition("NotFound", -1, -1);
        private static List<Iso4217Definition> _definitionCollection = new List<Iso4217Definition> {
            new Iso4217Definition("AED", 784, 2),
            new Iso4217Definition("AFN", 971, 2),
            new Iso4217Definition("ALL", 8, 2),
            new Iso4217Definition("AMD", 51, 2),
            new Iso4217Definition("ANG", 532, 2),
            new Iso4217Definition("AOA", 973, 2),
            new Iso4217Definition("ARS", 32, 2),
            new Iso4217Definition("AUD", 36, 2),
            new Iso4217Definition("AWG", 533, 2),
            new Iso4217Definition("AZN", 944, 2),
            new Iso4217Definition("BAM", 977, 2),
            new Iso4217Definition("BBD", 52, 2),
            new Iso4217Definition("BDT", 50, 2),
            new Iso4217Definition("BGN", 975, 2),
            new Iso4217Definition("BHD", 48, 3),
            new Iso4217Definition("BIF", 108, 0),
            new Iso4217Definition("BMD", 60, 2),
            new Iso4217Definition("BND", 96, 2),
            new Iso4217Definition("BOB", 68, 2),
            new Iso4217Definition("BOV", 984, 2),
            new Iso4217Definition("BRL", 986, 2),
            new Iso4217Definition("BSD", 44, 2),
            new Iso4217Definition("BTN", 64, 2),
            new Iso4217Definition("BWP", 72, 2),
            new Iso4217Definition("BYR", 974, 0),
            new Iso4217Definition("BZD", 84, 2),
            new Iso4217Definition("CAD", 124, 2),
            new Iso4217Definition("CDF", 976, 2),
            new Iso4217Definition("CHE", 947, 2),
            new Iso4217Definition("CHF", 756, 2),
            new Iso4217Definition("CHW", 948, 2),
            new Iso4217Definition("CLF", 990, 0),
            new Iso4217Definition("CLP", 152, 0),
            new Iso4217Definition("CNY", 156, 2),
            new Iso4217Definition("COP", 170, 2),
            new Iso4217Definition("COU", 970, 2),
            new Iso4217Definition("CRC", 188, 2),
            new Iso4217Definition("CUC", 931, 2),
            new Iso4217Definition("CUP", 192, 2),
            new Iso4217Definition("CVE", 132, 0),
            new Iso4217Definition("CZK", 203, 2),
            new Iso4217Definition("DJF", 262, 0),
            new Iso4217Definition("DKK", 208, 2),
            new Iso4217Definition("DOP", 214, 2),
            new Iso4217Definition("DZD", 12, 2),
            new Iso4217Definition("EGP", 818, 2),
            new Iso4217Definition("ERN", 232, 2),
            new Iso4217Definition("ETB", 230, 2),
            new Iso4217Definition("EUR", 978, 2),
            new Iso4217Definition("FJD", 242, 2),
            new Iso4217Definition("FKP", 238, 2),
            new Iso4217Definition("GBP", 826, 2),
            new Iso4217Definition("GEL", 981, 2),
            new Iso4217Definition("GHS", 936, 2),
            new Iso4217Definition("GIP", 292, 2),
            new Iso4217Definition("GMD", 270, 2),
            new Iso4217Definition("GNF", 324, 0),
            new Iso4217Definition("GTQ", 320, 2),
            new Iso4217Definition("GYD", 328, 2),
            new Iso4217Definition("HKD", 344, 2),
            new Iso4217Definition("HNL", 340, 2),
            new Iso4217Definition("HRK", 191, 2),
            new Iso4217Definition("HTG", 332, 2),
            new Iso4217Definition("HUF", 348, 2),
            new Iso4217Definition("IDR", 360, 2),
            new Iso4217Definition("ILS", 376, 2),
            new Iso4217Definition("INR", 356, 2),
            new Iso4217Definition("IQD", 368, 3),
            new Iso4217Definition("IRR", 364, 0),
            new Iso4217Definition("ISK", 352, 0),
            new Iso4217Definition("JMD", 388, 2),
            new Iso4217Definition("JOD", 400, 3),
            new Iso4217Definition("JPY", 392, 0),
            new Iso4217Definition("KES", 404, 2),
            new Iso4217Definition("KGS", 417, 2),
            new Iso4217Definition("KHR", 116, 2),
            new Iso4217Definition("KMF", 174, 0),
            new Iso4217Definition("KPW", 408, 0),
            new Iso4217Definition("KRW", 410, 0),
            new Iso4217Definition("KWD", 414, 3),
            new Iso4217Definition("KYD", 136, 2),
            new Iso4217Definition("KZT", 398, 2),
            new Iso4217Definition("LAK", 418, 0),
            new Iso4217Definition("LBP", 422, 0),
            new Iso4217Definition("LKR", 144, 2),
            new Iso4217Definition("LRD", 430, 2),
            new Iso4217Definition("LSL", 426, 2),
            new Iso4217Definition("LTL", 440, 2),
            new Iso4217Definition("LVL", 428, 2),
            new Iso4217Definition("LYD", 434, 3),
            new Iso4217Definition("MAD", 504, 2),
            new Iso4217Definition("MDL", 498, 2),
            new Iso4217Definition("MGA", 969, 0.7),
            new Iso4217Definition("MKD", 807, 0),
            new Iso4217Definition("MMK", 104, 0),
            new Iso4217Definition("MNT", 496, 2),
            new Iso4217Definition("MOP", 446, 2),
            new Iso4217Definition("MRO", 478, 0.7),
            new Iso4217Definition("MUR", 480, 2),
            new Iso4217Definition("MVR", 462, 2),
            new Iso4217Definition("MWK", 454, 2),
            new Iso4217Definition("MXN", 484, 2),
            new Iso4217Definition("MXV", 979, 2),
            new Iso4217Definition("MYR", 458, 2),
            new Iso4217Definition("MZN", 943, 2),
            new Iso4217Definition("NAD", 516, 2),
            new Iso4217Definition("NGN", 566, 2),
            new Iso4217Definition("NIO", 558, 2),
            new Iso4217Definition("NOK", 578, 2),
            new Iso4217Definition("NPR", 524, 2),
            new Iso4217Definition("NZD", 554, 2),
            new Iso4217Definition("OMR", 512, 3),
            new Iso4217Definition("PAB", 590, 2),
            new Iso4217Definition("PEN", 604, 2),
            new Iso4217Definition("PGK", 598, 2),
            new Iso4217Definition("PHP", 608, 2),
            new Iso4217Definition("PKR", 586, 2),
            new Iso4217Definition("PLN", 985, 2),
            new Iso4217Definition("PYG", 600, 0),
            new Iso4217Definition("QAR", 634, 2),
            new Iso4217Definition("RON", 946, 2),
            new Iso4217Definition("RSD", 941, 2),
            new Iso4217Definition("RUB", 643, 2),
            new Iso4217Definition("RWF", 646, 0),
            new Iso4217Definition("SAR", 682, 2),
            new Iso4217Definition("SBD", 90, 2),
            new Iso4217Definition("SCR", 690, 2),
            new Iso4217Definition("SDG", 938, 2),
            new Iso4217Definition("SEK", 752, 2),
            new Iso4217Definition("SGD", 702, 2),
            new Iso4217Definition("SHP", 654, 2),
            new Iso4217Definition("SLL", 694, 0),
            new Iso4217Definition("SOS", 706, 2),
            new Iso4217Definition("SRD", 968, 2),
            new Iso4217Definition("SSP", 728, 2),
            new Iso4217Definition("STD", 678, 0),
            new Iso4217Definition("SYP", 760, 2),
            new Iso4217Definition("SZL", 748, 2),
            new Iso4217Definition("THB", 764, 2),
            new Iso4217Definition("TJS", 972, 2),
            new Iso4217Definition("TMT", 934, 2),
            new Iso4217Definition("TND", 788, 3),
            new Iso4217Definition("TOP", 776, 2),
            new Iso4217Definition("TRY", 949, 2),
            new Iso4217Definition("TTD", 780, 2),
            new Iso4217Definition("TWD", 901, 2),
            new Iso4217Definition("TZS", 834, 2),
            new Iso4217Definition("UAH", 980, 2),
            new Iso4217Definition("UGX", 800, 2),
            new Iso4217Definition("USD", 840, 2),
            new Iso4217Definition("USN", 997, 2),
            new Iso4217Definition("USS", 998, 2),
            new Iso4217Definition("UYI", 940, 0),
            new Iso4217Definition("UYU", 858, 2),
            new Iso4217Definition("UZS", 860, 2),
            new Iso4217Definition("VEF", 937, 2),
            new Iso4217Definition("VND", 704, 0),
            new Iso4217Definition("VUV", 548, 0),
            new Iso4217Definition("WST", 882, 2),
            new Iso4217Definition("XAF", 950, 0),
            new Iso4217Definition("XCD", 951, 2),
            new Iso4217Definition("XOF", 952, 0),
            new Iso4217Definition("XPF", 953, 0),
            new Iso4217Definition("YER", 886, 2),
            new Iso4217Definition("ZAR", 710, 2),
            new Iso4217Definition("ZMW", 967, 2)

        };
        public static Iso4217Definition LookupByCode(string code)
        {
            dynamic def = _definitionCollection.Where(d => d.Code == code.ToUpper()).SingleOrDefault();
            if (def == null)
                def = _notFoundDefinition;
            return def;
        }

        public static Iso4217Definition LookupByNumber(int number)
        {
            dynamic def = _definitionCollection.Where(d => d.Number == number).SingleOrDefault();
            if (def == null)
                def = _notFoundDefinition;
            return def;
        }

        public class Iso4217Definition
        {
            private string _code;
            private int _number;
            private int _exponent;
            public string Code {
                get { return _code; }
            }
            public int Number {
                get { return _number; }
            }
            public int Exponent {
                get { return _exponent; }
            }
            public Iso4217Definition(string code, int number, int exponent)
            {
                _code = code;
                _number = number;
                _exponent = exponent;
            }
        }

    }

通过

id = c.getString(c.getColumnIndex("_id"));

答案 4 :(得分:0)

表中的ID列名为 id _id 。 如下所示:

db.execSQL("CREATE TABLE IF NOT EXISTS Person(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, number VARCHAR);");

所以,替换

id = c.getString(c.getColumnIndex("_id"));

id = c.getString(c.getColumnIndex("id"));