0

So I want to create a listview that consists of more than a column. I already succeed called the database, but the layouting still not work. I think this is because the all my data are put in one array and its difficult to make them in three columns. anyone can find solution for me?

My program result is like this in listview:

John Doe     12   Argentina
Marilyn Rose  32  Russia
Annabella  19  United States

However what I want is more like this:

John Doe        12      Argentina
Marilyn Rose    32      Russia
Annabella       19      United States

From what I read, we will need 2 XMLs. One for listview, and another is for layouting (give space between text). And One .JAVA called adapter to connect my MainActivity.java and layouting XML.

add: I already tried using two XMLs. one XML, lets call it main.XML is for calling ListView. And grid.XML, is where i put android:layout_alignParentLeft="true" (to create spaces). I used MyAdapter.JAVA to convertview in grid.XML. and in MainActivity.JAVA i called MyAdapter. However my code in MainActivity became error when its connected it MyAdapter.

this was my code that gave error java.lang.RuntimeException. So I had to delete it.. more information about it, please check two last code...

MainActivity.java (error)

public static ArrayList<String> arraydealer = new ArrayList<String>(); 
MyAdapter bohwat = new MyAdapter(MainActivity.this, arraydealer);   
lvcustom.setAdapter(bohwat);

And here is the code that is working well. It uses class MainActivity, AstraDB, and MySetGet, and main.XML. Other class thats not working is MyAdapter and grid.xml

This is how I called my database:

public class MainActivity extends Activity
{
    public static ArrayList<String> arraydealer = new ArrayList<String>();
    AstraDB astrahandler = new AstraDB(this);
    Spinner spDealer;
    ListView lvcustom;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        lvcustom = (ListView)findViewById(R.id.custom_lv);
        ShowListView();
    }

private void ShowListView()
{
        astrahandler.getAllDealer();
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, arraydealer);
        lvcustom.setAdapter(adapt);
}
}

This code of ArrayList in Activity and String name in AstraDB are very important to connect my MainActivity with the database but it seems this create trouble in layouting. because they are contained in ONE array

And this is function to get all data in my DB. its on AstraDB.java:

public List<MySetGet> getAllDealer()
        {
            List<MySetGet> info = new ArrayList<MySetGet>();

            db = DBHelper.getReadableDatabase();

            // Select All Query
            String selectQuery = "SELECT * FROM Dealer";

            cursor = db.rawQuery(selectQuery, null);
            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    MySetGet lets = new MySetGet();
                    lets.setDealerID(Integer.parseInt(cursor.getString(0)));
                    lets.setDealerName(cursor.getString(1));
                    lets.setDealerOwner(cursor.getString(2));
                    lets.setDealerWil(cursor.getString(3));

                    String name = cursor.getInt(0) +
                            "\u00A0 "+ cursor.getString(1)+ 
                            "\u00A0 "+ cursor.getString(2)+ 
                            "\u00A0 "+ cursor.getString(3);


                    MainActivity.arraydealer.add(name);
                    //add
                    info.add(lets);
                } while (cursor.moveToNext());
            }
            // closing connection
            cursor.close();
            db.close();
            //return contentdealer;
            return info;
        }

The MySetGet in getAllDealer() connects with MySetGet.java where I put setter and getter so the data can become object. which is more like this:

public int getDealerID(){ return DealerID;}
public void setDealerID(int DealerID) { this.DealerID = DealerID; }

Code to connect other XML with Java but still gave error:

grid_dealer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

                <TextView 
                    android:layout_height="wrap_content"
                    android:layout_width="50dp"
                    android:id="@+id/col1"
                    android:layout_alignParentLeft="true"    
                    android:layout_centerVertical="true"
                    android:text=""/>

                <TextView 
                   android:layout_height="wrap_content"
                    android:layout_width="100dp"
                    android:id="@+id/col2"
                    android:layout_toRightOf="@id/col1"                
                    android:layout_centerVertical="true"
                    android:text=""/>

important code in MyAdapter.java:

public class MyAdapter extends BaseAdapter
{
    Context context;
    ArrayList<MySetGet> dealerlist;

    public MyAdapter(Context context, ArrayList<MySetGet>list)
    {
        this.context = context;
        dealerlist = list;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        MySetGet yay = dealerlist.get(position);

        //this is to customize the layout of the listview
        if(convertView == null)
        {
            LayoutInflater inflater = null;
            inflater = (LayoutInflater)context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_dealer, null);
        }

        TextView tvID = (TextView)convertView.findViewById(R.id.col1);
        tvID.setText(yay.getDealerID());

        TextView tvName = (TextView)convertView.findViewById(R.id.col2);
        tvName.setText(yay.getDealerName());

        TextView tvOwner = (TextView)convertView.findViewById(R.id.col3);
        tvOwner.setText(yay.getDealerOwner());

        return convertView;
    }

}

Please help me. I am very new to this. Is there a way to modify my code without changing too much on how I called my database? The class and XML below works fine in showing database, but didnt create a neat layout space between columns Working class : AstraDB, MainActivity, MySetGet Working XML : main.xml Im sorry, if the post becomes longer. I want to clarify several things so that there is no misunderstanding.

6
  • If you have more number of tables like this, you can think about some ORM tools like ormlite ormlite.com/sqlite_java_android_orm.shtml Commented Aug 22, 2014 at 4:50
  • Since you are new to this, you can refer to this url androidtuts4u.blogspot.com/2012/11/… Commented Aug 22, 2014 at 4:59
  • Try using custom layout. Commented Aug 22, 2014 at 6:13
  • @Vinothkumar Arputharaj , thank you. I already used the tutorial for reference. All I want now is to make neat column space, but my code gave error when i try to connect 2 XMLs.. Commented Aug 22, 2014 at 6:27
  • you can follow @Aniruddha suggestion Commented Aug 22, 2014 at 7:58

3 Answers 3

1

you can use android:layout_weight="" for better arrangement of views

Sign up to request clarification or add additional context in comments.

5 Comments

umm, i think the major problem is not in XML.. thank you, i will give addition in my post
if u want to customize listview row, then you will need to modify ur AdapterActivity and XML file
I already modified adapter, but it gave error so I had to removed it. do u know where i did wrong? I already put the error code above
Thanks it worked for me. I just added android:layout_weight="1" in my xml file. Thanks a lot.
I think it doesnt work for me because I put all database rows into one array... Any suggestion for that? I tried to put the one row in three columns but eclipse didnt recognize when I put: String name = cursor.getString(0), String age = cursor.getInt(1). I want to add them into this, but unsuccessfull : MainActivity.arraydealer.add(name, age);
0

Using SimpleCursorAdapter is the ideal solution in your case. Please checkout a tutorial here that will take you through the steps in achieving what you want.

5 Comments

thank you. i am going to try it... i'll tell you how it turns @Rajesh
well, I already read but still confused.. is the tutorial using URI for layouting? if I use URI intead, will a class of URI able to handle several xmls that show database with many columns? Thank you for reply
You could do without the ContentResolver and directly use the Cursor you got from the DBHelper.
Is ContentResolver the same as URI? Well I am trying.. I think it doesnt work for me because I put all database rows into one array... Any suggestion for that? I tried to put the one row in three columns but eclipse didnt recognize when I put: String name = cursor.getString(0), String age = cursor.getInt(1). I want to add them into this, but unsuccessfull : MainActivity.arraydealer.add(name, age);
Using a Cursor is different from storing everything to an array. You iterate a Cursor to get what you want as against referencing an index. Please get yourself a book - you could find a list of books or reference materials in stackoverflow.com/tags/android/info.
0

In simple_list_item_1 layout if you arrange items relative to each other you will get the result what you are getting now. If you want proper formatting, relate the elements to left, center and right using android:layout_alignParentLeft="true", android:centerHorizontal="true" and android:layout_alignParentRight="true" respectively

1 Comment

I already tried it in two XMLs. one XML, lets call it main.XML is for calling ListView. And grid.XML, is where i put the code you gave. But it still gave error. I already put the code above. Do you know where I got wrong? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.