2

I'm developing an application with ListView for Student Marklist creation.

In this application, the List have 10 students. There are four grades provided for the exam which was conducted to those Students. One student can adapt only one grade from the four.

The teacher will assign the grade to the student in ListView.

My xml file Studentlist.xml is as follow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

and my row.xml file is as follow:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:padding="10dp" >

<TableRow>

    <TextView
        android:id="@+id/SNo"
        style="@style/text"
        android:layout_width="40dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/StudNo"
        style="@style/text"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/StudName"
        style="@style/text"
        android:layout_width="180dp"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:checked="true" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="40dp"
            android:layout_height="wrap_content" />
    </RadioGroup>
</TableRow>

Now I'm trying for the output in the form of:

1 0001 AAAA <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
2 0002 BBBB <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>

How can I use the Adapter functionalities?

ArrayAdapter<String,String,String,RadiGroup> studList=new ArrayAdapter<String,String,String,RadiGroup>();

Can I use like this, and how to develop the customized Adapter for the ListView?

Suggest me for the best solution!

Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
  • Why are you using a TableLayout for the row? A regular LinearLayout or RelativeLayout will fit as well. – etienne Apr 08 '13 at 15:33
  • you need to define a custom adapter this purpose.http://stackoverflow.com/questions/15832335/android-custom-row-item-for-listview/15832695#15832695 – Raghunandan Apr 08 '13 at 15:35
  • Both are same, right.. We are replacing the list item in the listview by a customized layout. Here LinearLayout and TableLayout are doing the same, we can use anything. tell me a suggestion with LinearLayout for the solution.. – user2258206 Apr 08 '13 at 15:36
  • check the link in my above comment each row will display two textviews . One the header and the other position of list item. you customize the same for your purpose. I could post an answer here but it would be redundant.http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – Raghunandan Apr 08 '13 at 15:37
  • Yeah, i have to do with the CustomAdapter, i'm new to android. I couldnt get the clear concept of CustomAdapter in this process.. – user2258206 Apr 08 '13 at 15:37

2 Answers2

1

You can use a class inheriting from ArrayAdapter, and overriding its getView() method.

public class StudentAdapter extends ArrayAdapter<Student> {

protected LayoutInflater inflater;

    public StudentAdapter(final Context context) {
        super(context, 0);
        inflater = (LayoutInflater) ((Context) context)
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        // Note: You should optimize here with re-using convertView

        View rowView = inflater.inflate(R.layout.user_address_row_layout,
                parent, false);
        TextView sNo = (TextView) rowView.findViewById(R.id.sNo);
        sNo.setText(getItem(position).number);
            // same for every field of the row
            // ...

        return rowView;
    }

 }
etienne
  • 3,146
  • 1
  • 24
  • 45
  • Agree, but you should make it more efficient checking if the convertView already exists, case where you only need to update the View. Here you got an example using ViewHolder pattern:https://github.com/Jachu5/RssFeeder_sherlock/blob/master/src/com/example/adapter/RSS_CustomAdapter.java – Jachu Apr 08 '13 at 15:45
  • That's why I specified 'You should optimize here [etc.]' in a comment. I didn't use it to keep the example simple enough. – etienne Apr 08 '13 at 17:17
1

This is a sample example of custom adapter for listview. Modify it as your need: XML layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Header -->

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black" >

        <TextView
            android:id="@+id/item2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:height="30dip"
            android:text="Latest News - Your Healing Place"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"
            android:width="100dip" />

        <TextView
            android:id="@+id/item1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:height="30dip"
            android:width="20dip" />

    </LinearLayout>

    <View android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/listDivider" />

    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black" >

        </ListView>

    </LinearLayout>
</LinearLayout>

news.java :

List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"details", "date"};
int[] to = new int[] { R.id.item1, R.id.item2};
ListView lv= (ListView)findViewById(R.id.listview);
HashMap<String, String> map = new HashMap<String, String>();
map.put("details", "This is a sample message");
map.put("date", "24-07-2003");
fillMaps.add(map);

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
Kaidul
  • 15,409
  • 15
  • 81
  • 150