Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get it work with the fragments. #2

Open
vakasgnabi opened this issue Mar 24, 2017 · 0 comments
Open

Cannot get it work with the fragments. #2

vakasgnabi opened this issue Mar 24, 2017 · 0 comments

Comments

@vakasgnabi
Copy link

package com.peekatuts.inductin;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;

import java.util.ArrayList;
import java.util.List;

public class Courses_ListView extends Fragment implements AsyncResponse {
//final String LOG="Courses_ListView";

final String LOG="Courses_ListView";
public ListView mListView;
List<String> items = new ArrayList<String>();
public String str = "";
DatabaseManagement dm=new DatabaseManagement(getContext());

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v=inflater.inflate(R.layout.fragment_courses_list_view,container,false);

    return v;

}



@Override
public void onStart() {
    super.onStart();
    initControls();
}

public void initControls(){
    mListView = (ListView) getView().findViewById(R.id.courseList);
    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getContext(),this);
    taskRead.execute("http://www.inductin.com/webservices/course_categories.php?request=ios");
    populateListView();
}



@Override
public void processFinish(String s) {
    Gson g=new Gson();

    Courses c=g.fromJson(s,Courses.class);
    Datum d=g.fromJson(s,Datum.class);
    int count=c.getData().toArray().length;
    // Toast.makeText(Courses_ListView.this, ""+count, Toast.LENGTH_SHORT).show();

    Cursor res=dm.getAllData(dm,"SELECT * FROM course_categories");

    int getCount=res.getCount();

    if(getCount==0) {
        for (int i = 0; i <= count; i++) {
            boolean insert = dm.onInsert(dm, c.getData().get(i).getCategoryId(), c.getData().get(i).getCategoryName());
            if (insert == true) {
                Log.i("Database front end:", "Data has been inserted successfully");
            } else {
                Log.i("Database front end:", "Insert issue");
            }
        }
    }
    else if(count!=getCount){
        dm.deleteAllData(dm,"course_categories");
        for (int i = 0; i <= count; i++) {
            boolean insert = dm.onInsert(dm, c.getData().get(i).getCategoryId(), c.getData().get(i).getCategoryName());
            if (insert == true) {
                Log.i("Database front end:", "Data has been inserted successfully");
            } else {
                Log.i("Database front end:", "Insert issue");
            }
        }
    }

}



private void populateListView(){
    Cursor cursor=dm.getAllRows(dm,"SELECT category_name FROM course_categories");
    for( cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext() ) {
        items.add(cursor.getString(0));
    }




    ArrayAdapter<String> adapter= new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,items);
    mListView.setAdapter(adapter);
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
            str= items.get(position);

                Toast.makeText(getContext(),"You clicked:"+items.get(position),Toast.LENGTH_SHORT).show();



        }
    });
}

}

The above is my code. if something is wrong with my code then let me know but everytime i start my application and click on the option of the navigation drawer. The application closes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant