android - What should I do to make the listview work in the fragment when I am getting data from the user and saving it into a SQLite database -
this shows 1 of fragments has listview code in worked in it's own layout not work in fragment. should work in fragment? new android app development not know do. if not understand trying do, having user enter data form , want data displayed in fragment list can see data entered beginning end. have been looking on solution cannot find 1 fits need do. information see pre-saved sq lite databases.
import android.content.intent; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.listfragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.database.cursor; import android.util.log; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listadapter; import android.widget.listview; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.widget.toast; import java.util.arraylist; import static android.r.attr.button; public class tab3expense extends fragment { private static final string tag = "tab3expense"; databasehelper mdatabasehelper; private listview mlistview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.tab3expense, container, false); return rootview; mlistview = (listview) findviewbyid(r.id.listview); mdatabasehelper = new databasehelper(this); populatelistview(); } private void populatelistview() { log.d(tag, "populatelistview: displaying data in listview."); //get data , append list cursor data = mdatabasehelper.getdata(); arraylist<string> listdata = new arraylist<>(); while(data.movetonext()){ //get value database in column 1 //then add arraylist listdata.add(data.getstring(1)); listdata.add(data.getstring(2)); listdata.add(data.getstring(3)); } //create list adapter , set adapter listadapter adapter = new arrayadapter<>(this, android.r.layout.simple_list_item_1, listdata); mlistview.setadapter(adapter); //set onitemclicklistener listview mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { string name = adapterview.getitematposition(i).tostring(); log.d(tag, "onitemclick: clicked on " + name); cursor data = mdatabasehelper.getitemid(name); //get id associated name int itemid = -1; while(data.movetonext()){ itemid = data.getint(0); } if(itemid > -1){ log.d(tag, "onitemclick: id is: " + itemid); intent editscreenintent = new intent(tab3expense.this, add_expense.class); editscreenintent.putextra("id",itemid); editscreenintent.putextra("name",name); startactivity(editscreenintent); } else{ toastmessage("no id associated name"); } } }); } /** * customizable toast * @param message */ private void toastmessage(string message){ toast.maketext(this,message, toast.length_short).show(); } }
here layout file
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.dharquissandas.budget.mainactivity$placeholderfragment"> <listview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview"/> </linearlayout>
rearrange code
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.tab3expense, container, false); mlistview = (listview)rootview. findviewbyid(r.id.listview); mdatabasehelper = new databasehelper(getcontext()); populatelistview(); return rootview; }
Comments
Post a Comment