Prabha Karan Prabha Karan - 1 year ago 47
Android Question

How to view the realm database data in listview by using RealmBaseAdapter?

I am new to the realm ,I am facing the issue in viewing the realm database data in listview. I done the code by using the RealmBaseAdapter but when i click on fetch button the null pointer exception is thrown.

when i fetch the data from database and viewed in a textview data is viewing but when i view in listview the null pointer exception is thrown.I don't know how to solve this ,please help me.

My code is attached below

public class MainActivity extends AppCompatActivity {

Realm realm;

EditText name,place,address;
Button submit,next;
TextView text;
// MyNamesAdapter adapter;
ListView realmData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
realm.init(this);
realm = Realm.getDefaultInstance();
name = (EditText)findViewById(R.id.name);
place= (EditText)findViewById(R.id.place);
address = (EditText)findViewById(R.id.address);
submit = (Button)findViewById(R.id.submit);
next = (Button)findViewById(R.id.nextActivity);
text = (TextView)findViewById(R.id.textView2);

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addDataToRealm();
}
});


next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextActivity();
}
});
realmData = (ListView)findViewById(R.id.listItems);
}

public void addDataToRealm()
{
realm.beginTransaction();
DetailsModel detailsModel = realm.createObject(DetailsModel.class);
detailsModel.setName(name.getText().toString());
detailsModel.setPlace(place.getText().toString());
detailsModel.setAddress(address.getText().toString());
realm.commitTransaction();

RealmResults<DetailsModel> realmDataList = realm.where(DetailsModel.class).findAll();
MyNamesAdapter adapter = new MyNamesAdapter(realmDataList);
realmData.setAdapter(adapter);
for (DetailsModel name:realmDataList) {

text.setText(name.getName());
}
}

public void nextActivity(){

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

private class MyNamesAdapter extends RealmBaseAdapter<DetailsModel> {

public MyNamesAdapter(OrderedRealmCollection<DetailsModel> data) {
super(data);
}

public class ViewHolder{
TextView name;
TextView place;
TextView address;
}

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

if (adapterData != null){
viewHolder.name.setText(detailsModel.getName());
viewHolder.place.setText(detailsModel.getPlace());
viewHolder.address.setText(detailsModel.getAddress());
}

return convertView;
}
}

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


This is the complete code of my project.

This is my logcat data

FATAL EXCEPTION: main Process: com.example.seyali_01.realmdatabase, PID: 8146
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.seyali_01.realmdatabase.MainActivity$MyNamesAdapter.getView(MainActivity.java:113)
at android.widget.AbsListView.obtainView(AbsListView.java:2363)

Answer Source

Use RealmBaseAdapter

The code for querying the data from the database.

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();
}

}

and

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;
}}

So you should use RealmResults with RealmBaseAdapter from https://github.com/realm/realm-android-adapters

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download