在视图中显示数据库结果

时间:2014-10-10 21:56:00

标签: java android xml sqlite view

我有一个简单的设置视图(通过片段),我希望从数据库(SQLite)预先填充数据。我的数据库结果在Settingsfragment.java中,现在我不知道如何在XML中获取它们(Android:提示)。

这是我现在的SettingsFragment.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import eu.twenty1media.smokestop.model.Asettings;
import eu.twenty1media.smokestop.model.DatabaseHandler;


public class SettingsFragment extends Fragment {

    public SettingsFragment(){}



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_settings, container, false);

        //Initalize or create Database
         final DatabaseHandler db = new DatabaseHandler(getActivity());
         db.getAsettings(1);



        Button mButton = (Button) rootView.findViewById(R.id.applysettings);
        mButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                db.updateAsettings();
                //db.addData(); wenn Datensatz erst geschrieben werden muss
                Toast.makeText(getActivity(), "Settings saved", Toast.LENGTH_SHORT).show();
            }
        });

        return rootView;
    }
}

这就是函数getAsettings

public Asettings getAsettings(int id)
{
    // 1. get reference to readable DB
    SQLiteDatabase db = this.getReadableDatabase();
    // 2. build query
    Cursor cursor = db.query(DBNAME,
            new String[]{"currency"
                    , "pricepp"
                    , "cigpp"
                    , "name"
                    , "cigperday"
                    , "welcome"
                    , "lastcig"},"id=?",
            new String[] { String.valueOf(id) },
            null, // e. group by
            null, // f. having
            null, // g. order by
            null);  // h. limit
    // 3. if we got results get the first one
    if (cursor != null)
        cursor.moveToFirst();
    // 4. build object

    // 4. build book object
    Asettings asettings = new Asettings();
    //asettings.setId(Integer.parseInt(cursor.getString(1)));
    asettings.setCurrency(cursor.getString(0));
    asettings.setPricepp(Float.parseFloat(cursor.getString(1)));
    asettings.setCigpp(Integer.parseInt(cursor.getString(2)));
    asettings.setFname(cursor.getString(3));
    asettings.setCigperday(Integer.parseInt(cursor.getString(4)));
    asettings.setWelcome(Integer.parseInt(cursor.getString(5)));
    asettings.setLastcig(Integer.parseInt(cursor.getString(6)));


    Log.d("getAsettings(" + id + ")", asettings.toString());

    // 5. return Asettings
    return asettings;

也许有人对我有好的教程或提示?我在网上搜索大约一个小时,我发现唯一的东西是ListViewAdapter。但是我没有Listview,只有6个变量可以放入正确的Android:提示文本框。

1 个答案:

答案 0 :(得分:0)

Flow就像这样...... 1.将文本视图实例化到您希望结果的位置。 2.读取数据库并将结果保存为局部变量。您可以通过登录到控制台来检查它们是否存在并且已正确读取。 3.将结果从2写入您在1中创建的视图。