gpt4 book ai didi

java - 我的 GridView 未显示

转载 作者:行者123 更新时间:2023-12-01 10:37:17 25 4
gpt4 key购买 nike

我正在 Android 上制作日历;我尝试在 GridView 中获取一些数据,但我的适配器没有发现任何问题。
问题是,当我启动带有 GridView 的 Activity 时,应用程序正在关闭,因此我删除了 GridView,然后 Activity 就开始工作了。
我编辑了 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;
}
}

最佳答案

在将 Activity 附加到 xml 文件之前,您正尝试膨胀 View 。你应该移动

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

调用setContentView方法之后。并在你的声明中留下

private GridView calendar;

关于java - 我的 GridView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34594718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com