Android sqlite gridview。应用程序运行,但黑屏

时间:2015-02-10 13:22:39

标签: android sqlite gridview

我试图让这款应用运行起来。在修复了一些崩溃之后,该应用程序正在运行,但它已经充满黑屏并且没有任何反应。 该应用程序很简单。它连接到sqlite并创建商品数据库,其标题为商品及其价格。它插入一个测试行。最后,它显示了gridview中的所有行。这是现在的所有功能。 你能否指出我在哪里挖掘甚至提供修复?

activity_main.xml中

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity"
>


<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnCount="4"
        android:fastScrollEnabled="true"
        android:fastScrollAlwaysVisible="true"
        android:id="@+id/gridView1"
        android:background="#d3d3d3"></GridView>
</FrameLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add to cart"
        android:id="@+id/btnAdd"
        android:layout_gravity="bottom" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to cart"
        android:id="@+id/btnGoCart"
        android:layout_gravity="bottom" />
</LinearLayout>
</LinearLayout>

MainActivity.java:

package com.eshop.app.eshop;

    import android.app.Activity;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ArrayAdapter;
    import android.widget.GridView;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.List;


public class MainActivity extends Activity {

SQLiteDatabase mydb;
GridView data;

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

    data=(GridView)findViewById(R.id.gridView1);
    List<String> la = new ArrayList<String>();
    ArrayAdapter<String> dataAdaptor = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, la);
    dataAdaptor.setDropDownViewResource(R.layout.activity_main);

    //TextView text = (TextView)findViewById(R.id.textView1);
    mydb = openOrCreateDatabase("eshop.db", MODE_PRIVATE, null);
    mydb.execSQL("DROP TABLE IF EXISTS Goods;");
    mydb.execSQL("CREATE TABLE IF NOT EXISTS Goods (Id INTEGER PRIMARY KEY, Title VARCHAR NOT NULL, Price INTEGER NOT NULL);");
    mydb.execSQL("INSERT INTO Goods (Title,Price) VALUES ('Bread', 123);");
    try{
        Cursor cr = mydb.rawQuery("SELECT * FROM Goods", null);
        if(cr!=null){
            if(cr.moveToFirst()) {
                do {
                    String id = cr.getString(cr.getColumnIndex("Id"));

                    String title = cr.getString(cr.getColumnIndex("Title"));
                    String price = cr.getString(cr.getColumnIndex("Price"));
                    la.add(id);
                    la.add(title);
                    la.add(price);
                    data.setAdapter(dataAdaptor);
                }while(cr.moveToFirst());
            }
            else{
                Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_LONG).show();
            }
        }
        cr.close();
        mydb.close();
    }
    catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error: "+e.getMessage(), Toast.LENGTH_LONG).show();

    }
    //text.setText(cr.getString(cr.getColumnIndex("Nazwa")));


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:1)

首先在查询data之前设置适配器。将其设置为listview,然后将数据添加到arraylist并调用

dataAdapter.notifyDataSetChanged()

添加每个项目后。 希望它会有所帮助。谢谢。