2015-02-10 58 views
0

我試圖讓這個應用程序工作。修復了幾次崩潰之後,該應用程序正在運行,但它充滿了黑屏,沒有任何反應。 該應用程序很簡單。它連接到sqlite並創建商品數據庫,商品名稱和價格。它插入一個測試行。最後它顯示了gridview中的所有行。這是現在所有的功能。 你能否指出我在哪裏挖或甚至提供修復?Android 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); 
} 
} 
+0

我解決了它。我犯了一個錯誤。應該是while(cr.moveToNext()); – Qeeet 2015-02-10 15:14:55

回答

1

首先設置適配器之前查詢data.just將其設置爲ListView控件,然後添加你的數據ArrayList和通話

dataAdapter.notifyDataSetChanged() 

添加每個項目後。希望它會幫助。謝謝。

+0

謝謝你的答案,但它沒有幫助:( – Qeeet 2015-02-10 14:16:44