Load xml file into Gridview?

3

In my project, I need to select an XML file (table) from my tablet and then read its contents and show it in a GridView.

I do not know how to start, does anyone have any idea how?

I think with the start code you can understand what I want to do:

public class XMLActivity extends Activity {

    private Button btnOpen; 
    private GridView tabelaxml; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml);

        btnOpen = (Button) findViewById(R.id.btnOpen);
        tabelaxml= (GridView) findViewById(R.id.tabelaxml);

        btnOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

            }
        });        
    }
}
    
asked by anonymous 11.08.2015 / 19:09

2 answers

1

To work with GridViews, you will need four things: The Activity (you already own), an Adapter class, which will have the contents of each cell in your grid, an xml file containing the layout of each cell in your grid, and , finally the xml responsible for your activity (where it will have its GridView component)

To learn more about reading xml files, see this link .

Take a look at in this link , where author explains very well how to create a GridView custom.

Your Adapter file will receive in your constructor, in addition to a context, a list of objects (in your case, the information regarding your xml), as in the example:

public ImageAdapter(Context context, ArrayList<XmlValores> valoresParametro) {
        this.context = context;
        this.meusValores = valoresParametro;
    }

From there, just work with your object as you wish! (:

    
24.08.2015 / 02:25
1

By pressing the button of your activity, you could call a new activity, containing the gridview you want.

Example:

 btnOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(this, NovaActivity.class);
                startActivity(intent);
            }
        });  

In your "NovaActivity" you will initialize the same way you start the first, but the xml of your activity will contain the GridiView component. Example:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>

This example xml is only meant to tell android that you will have a grid on your screen. To format the contents of this grid, you will need an adapter and the adapter's xml. The content of the "getView ()" function of the adapter is exactly to display each cell in your gridview. Example:

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

        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(mobileValues[position]);

In this section I say that the component "grid_item_label" will have the "position" position of the "mobileValues" vector as its content. That is, for each space of your grid, the getView () function will be executed. Now the content of each cell will be determined by you. The vector "mobileValues" can have any attribute, you simply present it in your adapter, it will appear on the screen.

In the case of the xml of the contents of your grid, simply insert the elements that each cell will have. In the example case, xml would have only one text.

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:layout_marginTop="5px"
        android:textSize="15px" >
    </TextView>

</LinearLayout>

You may not know the data in the file, but you need to know the type of data you will receive. This data will be stored in your vector so that each position is displayed on your adapter. (:

    
06.09.2015 / 20:11