0
   public class homescreenjava extends AppCompatActivity {


    public void addNewFields(View view) {

        Intent callnew =new Intent(this,AddNewField.class);
        startActivity(callnew);

    }
    public void showUserInfo(View view){

        Intent callnew =new Intent(this,showuserinfo.class);
        startActivity(callnew);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homescreen);
        ListView m_listview = (ListView) findViewById(R.id.list_view);

        SQLiteOpenHelper AgroDatabase = new AgroDatabase(this);
        SQLiteDatabase db = AgroDatabase.getReadableDatabase();
        Cursor cursor=db.query("fieldinfo",new String[]{"_id", "FieldName","Area"},null,null,null,null,null);
        CursorAdapter listAdapter= new SimpleCursorAdapter(this,
                                            android.R.layout.simple_list_item_1,
                                            cursor,
                                            new String[]{"FieldName"},
                                            new int[]{android.R.id.text1},0);

        m_listview.setAdapter(listAdapter);



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        cursor.close();
        db.close();
    }
}

Iam also not being able to close cursor and database here on the same class. cursor and db cant be resolved here is the error message

 @Override
        protected void onDestroy() {
            super.onDestroy();
            cursor.close();
            db.close();
        }

I read that onListItemClick can be implemented if my class extends listActivity can I implement this method in my case or is there any other solution

public void onListItemClick(ListView listview,
                                   View itemView,
                                    int position,
                                    long id){
      Intent intent=new Intent(homescreenjava.this,SecondHome.class);
      startActivity(intent);
  }

just for reference i am putting my xml code for this activity . listview is at the last of xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_homescreen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.sagar.ahs.homescreenjava"
    android:weightSum="1">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="120dp"
        android:layout_height="77dp"
        app:srcCompat="@drawable/leaf"
        android:id="@+id/imageView2"
        android:layout_gravity="end"
        android:background="@color/colorAccent"
         />

    <TextView
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="77dp"
        android:id="@+id/textView2"
        android:textSize="30sp"
        android:textScaleX="2"
        android:gravity="start|center"
        android:elevation="1dp"
        android:textColor="#aaaf"
        android:textStyle="normal|bold"
        android:background="@color/colorAccent"
         />
</LinearLayout>

    <TextView
        android:text="@string/main_page"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start|center"
        android:id="@+id/textView5"
        android:layout_weight="7.88"
        android:textStyle="normal"
        android:textSize="20sp"
        android:background="@color/colorPrimary"
        android:textColor="#ffff"
        android:paddingStart="10dp"
        android:paddingEnd="2dp"
        android:paddingTop="10dp"
        android:paddingBottom="8dp"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/plus"
            android:id="@+id/imageButton8"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:tint="#ffff"
            android:onClick="addNewFields"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/boss"
            android:id="@+id/imageView"
            android:tint="#ffff"
            android:background="@color/colorPrimary"
            android:layout_weight="1"
            android:onClick="showUserInfo"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="@string/button_info_field"
            android:layout_width="175dp"
            android:layout_height="wrap_content"
            android:id="@+id/textView7"
            android:textColor="#ffff"
            android:textSize="23sp"
            android:background="@color/colorPrimary"
            android:gravity="fill"
            android:layout_weight="0.76"/>

        <TextView
            android:text="@string/button_info_UI"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView8"
            android:layout_weight="1"
            android:textColor="#ffff"
            android:textSize="23sp"
            android:background="@color/colorPrimary"
            android:gravity="fill"/>
    </LinearLayout>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
android:divider="@color/colorPrimary"
        android:dividerHeight="3dp"
        android:id="@+id/list_view"
/>



</LinearLayout>
Sagar Acharya
  • 1,763
  • 2
  • 19
  • 38
  • I believe this post address same issue that you have. http://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android – mumair Jan 13 '17 at 13:01
  • Possible duplicate of [How to handle the click event in Listview in android?](http://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android) – The SE I loved is dead Jan 13 '17 at 13:07
  • thanks for your reply but i still dont understand why am i not being able to close the cursor and database in onDestroy method? – Sagar Acharya Jan 13 '17 at 13:23

1 Answers1

0

To close the database and cursor, first you have to make cursor and db global variable, like this:

private SQLiteDatabase db;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homescreen);
    ListView m_listview = (ListView) findViewById(R.id.list_view);

    SQLiteOpenHelper AgroDatabase = new AgroDatabase(this);
    db = AgroDatabase.getReadableDatabase();
    cursor = db.query("fieldinfo", new String[]{"_id", "FieldName", "Area"}, null, null, null, null, null);

Now you can close in ondestroy function.

To handle onclick of listview items, follow the below code inside oncreate:

 m_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            Log.v("TAG","listview item clicked");
        }
    });
captaindroid
  • 2,868
  • 6
  • 31
  • 45