Upload xml file to Gridview?

Asked

Viewed 246 times

3

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

I don’t know how to start, does anyone have any idea how?

I think with the starting 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) {

            }
        });        
    }
}

2 answers

1

By pressing your Activity button, you could call a new Activity, containing the desired gridview.

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 one, but your Activity’s xml 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 only serves 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 xml of this Adapter. The content of the "getView()" function of Adapter is exactly for displaying each cell of 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 excerpt I say that the component "grid_item_label" will have as content the position "position" of the vector "mobileValues". That is, for each space on 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, just present it in your Adapter, that it will appear on the screen.

In the case of xml of the contents of your grid, just insert the elements that each cell will have. In the case of the example, 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. (:

1

To work with Gridviews, you will need four things: A Activity (you already have), an Adapter class, which will contain the contents of each cell on your grid, an xml file containing the layout of each cell on your grid, and finally the xml responsible for your Activity (where you will have your Joint Committee)

To know better how to read xml files, see that link.

Take a look too at that link, where author explains very well how to create a custom Gridview.

Your Adapter file will receive in your constructor, in addition to a context, a list of objects (in your case, the information relating to 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! (:

  • Pedro, to half covered in this project ://type, as I will be reading the xml file by "pressing" the button that will be in the application, I do not know how to put in Adapter the values and much less in Activity since I supposedly do not know each data of the file. I’m already using Imageadapter, the custom table and some tips on the same links. Have any idea how to get started with Activity?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.