1
所以我一直在我的應用程序中使用一個網格視圖來處理幾件事情,而現在這個問題正在困擾着我,因爲我在大約一週的嘗試後無法解決問題。填充GridView問題
我試圖填充網格視圖和我的時間表,這是當前存儲在一個多維數組,但被填充的唯一指標是第一位的。我的BaseAdapter類的副本如下:
package com.ian.calendar.event;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.ian.calendar.database.DatabaseHandler;
import com.ian.campusmap.R;
public class ModuleEvents extends BaseAdapter {
private String weekday;
private Context context;
private View view;
private TextView[] textView;
private DatabaseHandler dbHandler;
private int eventNumber;
private String[][] moduleA;
public ModuleEvents(Context _context, String _weekday) {
context = _context;
weekday = _weekday;
dbHandler = new DatabaseHandler(context);
dbHandler.populateDatabase();
dbHandler.getTimeTable();
}
public int getCount() {
return 6;
}
public View getView(int position, View convertView, ViewGroup parent) {
int i = 1;
eventNumber = getEventsOnWeekday();
moduleA = new String[eventNumber][6];
textView = new TextView[eventNumber];
do {
if(weekday.equals(dbHandler.getTimeTableWeekday(i))) {
for(int j = 0; j < eventNumber; j++) {
moduleA[j][0] = dbHandler.listArray(i, 0);
moduleA[j][1] = dbHandler.listArray(i, 1);
moduleA[j][2] = dbHandler.listArray(i, 3);
moduleA[j][3] = dbHandler.listArray(i, 5);
moduleA[j][4] = dbHandler.listArray(i, 6);
moduleA[j][5] = dbHandler.listArray(i, 2);
i++;
}
} else {
i++;
}
} while(i < dbHandler.count);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.grid_item_2, null);
}
for(int k = 0; k < moduleA.length; k++) {
textView[k] = new TextView(context);
textView[k] = (TextView) view.findViewById(R.id.txtModuleID);
textView[k].setText(moduleA[k][position]);
}
return view;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public int getEventsOnWeekday() {
int events = 0;
for(int i = 1; i <= 9; i++) {
if(weekday.equals(dbHandler.getTimeTableWeekday(i))) {
events++;
}
}
return events;
}
}
如果您需要任何進一步的類源代碼隨意問。
編輯:忘了補充說,可能有一些冗餘的代碼,我仍然沒有刪除。抱歉!
EDIT2:/擺脫了做while循環由phtrivier
EDIT3提到:我想沒有人知道..
phtrivier:對不起,我的冗餘代碼的一部分。如果我們要刪除它,只要'返回視圖';它只會返回數組的最後一個索引。 – ieen 2012-02-23 17:03:58