2016-01-31 116 views
0

我有一個SQLite數據庫,其中有2個表(即將更多)我試圖從這些表中的一個填充微調,並且無法找到目標資源一個特定的表格。如何從SQLite數據庫表填充Android微調錶

所以我目前的情況是我不得不在表'菜單項'和'菜單類別'...我有'菜單項'中的微調,我想填充菜單類別中的項目。

這裏是我的菜單項活動

public class SettingsMenuItem extends AppCompatActivity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings_menu_item); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    countRecords(); 
    readRecords(); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    Button buttonCreateLocation = (Button) findViewById(R.id.btnAdd); 
    buttonCreateLocation.setOnClickListener(new OnClickListenerCreateMenuItem()); 
} 

public void countRecords() { 

    int recordCount = new TableControllerMenuItem(this).count(); 
    TextView textViewRecordCount = (TextView) findViewById(R.id.textViewRecordCount); 
    textViewRecordCount.setText(recordCount + " records found."); 

} 

public void readRecords() { 

    LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords); 
    linearLayoutRecords.removeAllViews(); 

    List<ObjectMenuItem> menuitem = new TableControllerMenuItem(this).read(); 

    if (menuitem.size() > 0) { 

     for (ObjectMenuItem obj : menuitem) { 

      int id = obj.id; 
      String menuitemName = obj.name; 
      String menuitemDescription = obj.description; 
      Float menuitemPrice = obj.price; 
      Float menuitemCost = obj.cost; 

      String textViewContents = menuitemName; 

      TextView textViewMenuItem= new TextView(this); 
      textViewMenuItem.setPadding(0, 10, 0, 10); 
      textViewMenuItem.setText(textViewContents); 
      textViewMenuItem.setTag(Integer.toString(id)); 
      textViewMenuItem.setTextSize(20.0f); 

      textViewMenuItem.setOnLongClickListener(new OnLongClickListenerMenuItem()); 


      linearLayoutRecords.addView(textViewMenuItem); 
     } 

    } 

    else { 

     TextView locationItem = new TextView(this); 
     locationItem.setPadding(8, 8, 8, 8); 
     locationItem.setText("No records yet."); 

     linearLayoutRecords.addView(locationItem); 
    } 

} 

這裏是我的菜單項XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<EditText 
    android:id="@+id/editTextItemName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:hint="Item Name" 
    android:singleLine="true" > 

    <requestFocus /> 
</EditText> 

<EditText 
    android:id="@+id/editTextItemDesc" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/editTextItemName" 
    android:hint="Item Description" 
    android:singleLine="true" /> 

<EditText 
    android:id="@+id/editTextItemPrice" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/editTextItemDesc" 
    android:hint="Item Price" 
    android:singleLine="true" 
    android:inputType="numberDecimal"/> 

<EditText 
    android:id="@+id/editTextItemCost" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/editTextItemPrice" 
    android:hint="Item Cost" 
    android:singleLine="true" 
    android:inputType="numberDecimal"/> 

<Spinner 
    android:id="@+id/spinnerCategory" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editTextItemCost"></Spinner> 

回答

0

從數據庫讀取的數據,並將相關數據保存到一個數組。然後使用該數組來填充和ArrayAdapter,最後將該ArrayAdapter設置爲微調器的適配器。

希望這會有所幫助!

+0

我有一個readRecords列表,我已經設置了顯示錶格的內容,我可以將它轉換爲微調嗎? –