如何在领域数据库中查看listview中的查询数据?

时间:2017-03-08 13:30:47

标签: android listview realm

我是realm数据库的新手,我在realm数据库中遇到了从数据库查询数据的问题。来自数据库的查询数据如下图所示enter image description here

我需要在列表视图中逐个查看查询数据。

将数据添加到领域数据库的代码。

  public class MainActivity extends AppCompatActivity {

    private Realm mRealm;
    EditText editText;
    Button btn,btnNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.editText);
        btn = (Button)findViewById(R.id.button);
        btnNext = (Button)findViewById(R.id.button2);
        Realm.init(getApplicationContext());
        mRealm = Realm.getDefaultInstance();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String newNames = editText.getText().toString();
                mRealm.beginTransaction();
                MyNames names = mRealm.createObject(MyNames.class);
                names.setName(newNames);
                mRealm.commitTransaction();
                editText.setText("");
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent  = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });



    }

}

从数据库中查询数据的代码。

 public class Main2Activity extends AppCompatActivity {

    ListView listview;
    private Realm mRealm;
    Button next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        listview = (ListView)findViewById(R.id.list);
        next = (Button)findViewById(R.id.button2);

        listdata();
        listAdapter();
    }

    public List<RealmResults<MyNames>> listdata()
    {
        List<RealmResults<MyNames>> names = new ArrayList<>();
        mRealm = Realm.getDefaultInstance();
        RealmQuery<MyNames> data = mRealm.where(MyNames.class);
        RealmResults<MyNames> newData = data.findAll();
        names.add(newData);
        return  names;
    }

    public void listAdapter()
    {
        List<RealmResults<MyNames>> newData = this.listdata();
        ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_simple,R.id.textname,newData);
        listview.setAdapter(adapter);
    }

公共类MyNames扩展了RealmObject {

        String name;
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

请逐一帮助如何在listview中查看数据库中查询的数据。

2 个答案:

答案 0 :(得分:2)

使用RealmBaseAdapter

从数据库中查询数据的代码。

 public class Main2Activity extends AppCompatActivity {
    @BindView(R.id.list)
    ListView listview;

    Realm realm;

    @OnClick(R.id.button2) 
    public void clickButton2() {
        // do next
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        realm = Realm.getDefaultInstance();
        RealmResults<MyNames> results = realm.where(MyNames.class).findAllAsync();
        MyNamesAdapter myNamesAdapter = new MyNamesAdapter(results);
        listView.setAdapter(myNamesAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        realm.close();
    }
}

public class MyNamesAdapter extends RealmBaseAdapter<MyNames> {
    private static class ViewHolder {
        TextView text;
    }

    public MyListAdapter(OrderedRealmCollection<Counter> realmResults) {
        super(realmResults);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(android.R.layout.simple_list_item_1, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (adapterData != null) {
            MyNames item = adapterData.get(position);
            viewHolder.text.setText(item.getName());
        }
        return convertView;
    }
}

因此,您应该使用RealmResults<T>来自https://github.com/realm/realm-android-adapters

RealmBaseAdapter

答案 1 :(得分:1)

newDataList<RealmResults<MyNames>>。列表视图显示来自List<>的项目,这些项目是RealmResult<MyNames>,这是&#34;集合&#34;它本身,导致列表只显示包含所有对象的1个项目。

但是,您希望在列表视图中显示单个MyNames项目,而不是RealmResults。

您可能正在寻找类似的东西(简化一点)

public List<MyNames> listdata()
{
    mRealm = Realm.getDefaultInstance();
    return mRealm.where(MyNames.class).findAll();
}

public void listAdapter()
{
    List<MyNames> names = listdata();
    ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_simple,R.id.textname,newData);
    listview.setAdapter(adapter);
}

另外,请勿忘记在Activity#onDestroy()

中关闭这个领域