Displaying MySQL Data into ListView

Tutorials with advanced 'difficulty' and more Lines of Code.

Displaying MySQL Data into ListView

Postby dharmatin » Thu Jun 09, 2011 3:49 am

Hi Guys... I am very newbie on Android Development... Can you help me How to display MySQl data into ListView, I have Successed Displaying All MySQL Data into one Listview, not Every Row Data on MySQL Data... So, I Want To Dispaly Each Row on MySQL database into Each ListView, Can you Help me?? :) Thanks a lot.. Sorry My English is Bad... :mrgreen:
dharmatin
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jun 09, 2011 3:40 am

Top

Re: Displaying MySQL Data into ListView

Postby Exoit » Sun Jun 12, 2011 2:27 pm

So if i understand you correctly you want to display each row in the database table as a row in the listview.

If the activity is only going to show a listview then use a listactivity, create a function which returns a cursor with the data you want from the database, let the listactitivty manage this cursor, create a layout for the listview rows, then finally build a custom CursorAdapter which will handle the cursor data and use the listview row layout.

ListActivity layout example:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
     <ListView 
         android:id="@android:id/list"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         />
     <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Empty set"
         />
</LinearLayout>


ListActivity example:
Code: Select all
public class ExampleListAct extends ListActivity {

     private Cursor cursor;
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.listlayout);
         DBHandler dbHandler = new DBHandler(this);
         cursor = dbHandler.fetchDataFromDatabase();
         if(cursor != null){
           startManagingCursor(cursor);
           this.setListAdapter(new ExampleCursorAdapter(this, cursor));
        }

     }



}


CursorAdapter Example:
Code: Select all
public class ExampleCursorAdapter extends CursorAdapter {

    private LayoutInflater inflater;

    public ExampleCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        viewHolder vh = (viewHolder) view.getTag();
        vh.firstRow.setText(cursor.getString(ColumnIndex));
        vh.secondRow.setText(cursor.getString(ColumnIndex));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        final View view =  inflater.inflate(R.layout.listview_row_layout, parent, false);
        viewHolder vh = new viewHolder();
        vh.firstRow = (TextView) view.findViewById(R.id.firstRow);
        vh.secondRow = (ImageView) view.findViewById(R.id.secondRow);
        view.setTag(vh);
        return view;
    }

   public static class viewHolder
    {
        public TextView firstRow;
        public TextView secondRow;
    }

}


listview_row_layout Example:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
     <TextView android:id="@+id/firstRow"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
     <TextView android:id="@+id/secondRow"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
</LinearLayout>
Last edited by Exoit on Sun Jul 24, 2011 4:22 pm, edited 1 time in total.
Exoit
Freshman
Freshman
 
Posts: 4
Joined: Sun Dec 19, 2010 1:00 am

Re: Displaying MySQL Data into ListView

Postby uxc.wilis » Mon Jun 20, 2011 1:51 pm

good references,,,

thx bro.
uxc.wilis :) :)
uxc.wilis
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 30, 2010 3:22 pm

Re: Displaying MySQL Data into ListView

Postby rookey » Fri May 04, 2012 12:00 pm

Hi @all,

my question may sound very stupid, but I'm really just getting started with Android, and maybe an answer could help someone else too, as this thread seems to be often referred on other sites as well.

How am I supposed to make these code snippets work in my project using Eclipse? I am aware of how to create these XML files, to create a new Java source file and add the class - but Eclipse displays numerous errors, warnings and doesn't seem to 'integrate' them into my project. And last but not least, how could I get my 'own' listview widget (added in main.xml) to work with it?

kind regards,
r.
User avatar
rookey
Once Poster
Once Poster
 
Posts: 1
Joined: Fri May 04, 2012 11:03 am
Location: Malaysia

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: Google [Bot] and 4 guests