Gridview befüllen , Spaltenweise möglich?

H

Heribert500

Ambitioniertes Mitglied
1
Guten morgen,

mit diesem Code befülle ich ein Gridview dynamisch mit gefilterten Daten eine DB ( jeweils 3 Zeilen / Werte)

Code:
 db = new DBHelper(this);
        db.getMonth("2016", "Februar");
        gridView = (GridView) findViewById(R.id.gridView1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, MyArray);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(),
                        ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
Das ganze soll eine Monatsdarstellung werden
Die Daten werden von rechts nach links und dann in die nächste Zeile geschrieben,
etwa so :
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Es wird alles korrekt im Grid dargestellt, aber
ich brauche aber diese Darstellung
1 8 15
2 9 16
3 10 17
4 11 18
5 12
6 13
7 14

ist das möglich mit einem Gridview,

Danke für eure Tipps

heribert500
 
Hi,
mit ein Gridview habe ich es noch nie versucht, das umzubauen macht zu viel Arbeit.

An deiner Stelle würde ich es noch mit Gridlayout versuchen:
GridLayout | Android Developers
 
  • Danke
Reaktionen: Heribert500
markus.tullius schrieb:
Hi,
mit ein Gridview habe ich es noch nie versucht, das umzubauen macht zu viel Arbeit.

An deiner Stelle würde ich es noch mit Gridlayout versuchen:
GridLayout | Android Developers
würde ich auch machen und dann kannst du mit:
Code:
setOrientation (int orientation)
die Ausrichtung festlegen
 
  • Danke
Reaktionen: Heribert500
Vielen Dank für den Tipp euch beiden.
Gridlayout , war mir nicht bekannt.
Schau ich mir an

heribert500
 
Öhm, vermutlich bin ich zu spät dran, aber falls Du das GridView noch hast, setz doch mal bitte
Code:
android:numColumns="3"
im GridView XML.
 
  • Danke
Reaktionen: Heribert500
Hallo reallord,

danke, hab ich so mal gemacht
dennoch wird zeilenweise geschrieben
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15 usw.
ich bräcuhte das so
1 8 15
2 9 16
3 10
4 11
5 12
6 12
7 14
usw.

Mit dem Gridlayout bin ich allerdings auch noch nicht weiter gekommen

heribert500
 
So, ich hab mir das noch mal angesehen. GridView geht wohl anscheinend so ohne weiteres nicht horizontal. :( Daher dann also GridLayout. Hab jetzt mal ein Minimalbeispiel gebastelt (basierend hierauf), keine Ahnung, ob Du das noch brauchst oder Dir das so hilft.

Code:
package com.example.horizontalgridlayout;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private GridLayout gridLayout;
    private ArrayList<TextView> textViews;

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

        gridLayout = (GridLayout) findViewById(R.id.GridLayout1);
        gridLayout.setOrientation(1);
        gridLayout.setColumnCount(5);
        gridLayout.setRowCount(7);

        int numberOfDays = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);

        textViews = new ArrayList<TextView>();
        TextView textView = null;
        for (int i = 0; i < numberOfDays; i++) {
            textView = new TextView(MainActivity.this);
            textView.setLayoutParams(new GridLayout.LayoutParams());
            textView.setText(String.valueOf(i + 1));
            textView.setTextSize(25);
            textView.setPadding(50, 25, 10, 25);
            textViews.add(new TextView(this));
            final int j = i;
            textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getBaseContext(), j + " Clicked", Toast.LENGTH_SHORT).show();

                }
            });
            gridLayout.addView(textView);
        }

        setContentView(gridLayout);
    }
}

Code:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    tools:context="com.example.horizontalgridlayout.MainActivity" >

</GridLayout>

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.horizontalgridlayout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

So sieht das Ganze dann aus:
horizontalGridLayout.png


Vielleicht bringt Dir das ja was. Viel Erfolg. :smile:
 
  • Danke
Reaktionen: markus.tullius und Heribert500
Hi reallord, danke für deine Bemühung.
Habe eine ähnlich aufgebaute Lösung bei GitHub gefunden.
Komme auch jetzt erst wieder zum Coden, 1 Woche lang war Pause......

vielen Danke
heribert500
 

Ähnliche Themen

G
Antworten
5
Aufrufe
871
jogimuc
J
A
Antworten
10
Aufrufe
1.493
andymcnab
A
W
Antworten
16
Aufrufe
844
jogimuc
J
Zurück
Oben Unten