我的GridView没有显示出来

时间:2016-01-04 15:41:36

标签: java android gridview calendar android-gridview

我正在开发一个关于android的日历;我尝试在GridView中获取一些数据,但我发现我的适配器没有任何问题 问题是,当我使用GridView启动活动时应用程序正在关闭,因此我删除了GridView,然后活动正在运行。
我编辑了我的GridView,但它没有工作

这是我的GridView的xml文件:

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

<TextView
    android:id="@+id/Month"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<GridView
    android:id="@+id/Calendar"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>

</LinearLayout>`

这是我要插入GridView单元格的xml文件:

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

<TextView
    android:id="@+id/day"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40sp" />

</LinearLayout>`

这是我的java文件,但我认为它没有任何问题:

package com.example.damian.schulkalender;


public class Kalender extends ActionBarActivity {

private static boolean loginSuccsessful = false;

private GridView calendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kalender);
    calendar = (GridView) findViewById(R.id.Calendar);
    initDatabase();
    calendar.setAdapter(new KalenderAdapter(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_kalender, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    if (id == R.id.action_login) {
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);

        loginSuccsessful = Login.getLoginSuccess();

    }

    if (id == R.id.action_vertretungsplan) {

        Intent intent = new Intent(this, Vertretungsplan.class);
        startActivity(intent);

    }

    if (id == R.id.action_stundenplan) {

        Intent intent = new Intent(this, Stundenplan.class);
        startActivity(intent);

    }

    return super.onOptionsItemSelected(item);
}

private void initDatabase() {

    try {

        Class.forName("org.postgresql.Driver");

    } catch (ClassNotFoundException e) {

        return;

    }

    Connection connection = null;
    String url = "jdbc:postgresql://10.0.2.2/Seminarkurs" +
            "?sslfactory=org.postgresql.ssl.NonValidationgFactory" +
            "&ssl=true";

    try {

        connection = DriverManager.getConnection(url , "postgres", "postgres");
        Toast.makeText(getApplicationContext(), "Connection successful!", Toast.LENGTH_LONG).show();

    } catch (SQLException e) {

        Toast.makeText(getApplicationContext(), "Connection Failed! Check output console", Toast.LENGTH_LONG).show();
        e.printStackTrace();
        return;

    }

}

class KalenderData {

    int day;
    int month;
    String events;
    int eventDate;

    KalenderData(int day,int month, String events, int eventDate) {
        this.day = day;
        this.month = month;
        this.events = events;
        this.eventDate = eventDate;
    }

}

class KalenderAdapter extends BaseAdapter {

    ArrayList<KalenderData> list;
    Context context;

    KalenderAdapter(Context context) {

        this.context = context;
        list = new ArrayList<KalenderData>();

        int[] amountOfDays = {31,28,31,30,31,30,31,31,30,31,30,31};
        int[] month = {1,2,3,4,5,6,7,8,9,10,11,12};
        int[] days;

        for(int k = 0; k < month.length; k++) {

            for (int i = 0; i < amountOfDays[i]; i++) {

                days = new int[amountOfDays[month[k]]];

                for (int l = 0; l < amountOfDays[month[k]]; l++) {

                    days[l] = (amountOfDays[month[k]]) - amountOfDays[month[k] - (l + 1)];

                    //Konstruktor ist falsch initialisiert
                    KalenderData tempData = new KalenderData(days[l], month[k], null, 0);
                    list.add(tempData);

                }
            }
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder{

        TextView data;

        ViewHolder(View view){

            data = (TextView) view.findViewById(R.id.day);

        }

    }

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

        View row = convertView;
        ViewHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.single_day, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        }else{

            holder = (ViewHolder) row.getTag();

        }

        KalenderData temp = list.get(position);
        holder.data.setText(temp.day);

        return row;
    }
}

1 个答案:

答案 0 :(得分:1)

在将活动附加到xml文件之前,您尝试对视图进行充气。你应该移动

calendar = (GridView) findViewById(R.id.Calendar);

setContentView方法调用之后。 并在你宣布假期

private GridView calendar;