-3

I want to get data from database and show this data into a custom listview. I have made a custom list view with two textviews and I have an id autoincremented and name and a value in a database.

I want to display that name and the value in the custom listview textview. I am new in Android.

public class List_View_Data extends AppCompatActivity {
    database db;
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list__view__data);
        db=new database(this);
        show_data_sql();
    }

    private void show_data_sql(){
        listView= (ListView) findViewById(R.id.activity_list__view__data);
        ArrayList<String> thelist=new ArrayList<>();
        Cursor data=db.show_data();
        if(data.getCount()==0){
            Toast.makeText(this,"Data Not Found",Toast.LENGTH_LONG).show();}
        else{
                while (data.moveToNext()){
                thelist.add(data.getString(1));
                thelist.add(data.getString(2));
                }
            //BaseAdapter is a very generic adapter that allows you to do pretty much whatever you want.
            //using baseadapter which have listadapter and spinneradapter
            Adapter listAdapter=new Adapter(this,data);
            {
                listView.setAdapter(listAdapter);
            }
        }
        }

}

https://i.stack.imgur.com/b7bDS.png

sajidamin
  • 49
  • 1
  • 1
  • 10
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sajidamin.digital_tasbeh/com.example.sajidamin.digital_tasbeh.List_View_Data}: java.lang.IllegalArgumentException: column '_id' does not exist – sajidamin Nov 03 '17 at 12:42
  • at com.example.sajidamin.digital_tasbeh.Adapter.(Adapter.java:0) at com.example.sajidamin.digital_tasbeh.List_View_Data.show_data_sql(List_View_Data.java:37) at com.example.sajidamin.digital_tasbeh.List_View_Data.onCreate(List_View_Data.java:21) – sajidamin Nov 03 '17 at 12:42
  • ^ Please add that extra information by [editing your question](https://stackoverflow.com/posts/47095999/edit), thanks. – halfer Nov 03 '17 at 20:04
  • the error is shown now – sajidamin Nov 04 '17 at 06:36
  • Please don't post screenshots of code, XML, or logcat output. Please post all text as text. – Mike M. Nov 04 '17 at 11:41

1 Answers1

0

Please can you link the trace for the error ?

I think you have issue with your index :

thelist.add(data.getString(1));

The index start at 0

Second, are you sure it's a String you get ?

Lionel
  • 11
  • 4