Java Logcat中无错误的空白ListView-Android&;自定义适配器
我一直在尝试创建一个显示事件项的listView,但它没有显示任何内容。我基本上只是遵循了这个例子。方法返回事件项的Arraylist 以下是我的活动:Java Logcat中无错误的空白ListView-Android&;自定义适配器,java,android,listview,android-listview,baseadapter,Java,Android,Listview,Android Listview,Baseadapter,我一直在尝试创建一个显示事件项的listView,但它没有显示任何内容。我基本上只是遵循了这个例子。方法返回事件项的Arraylist 以下是我的活动: public class CategoryActivity extends ActionBarActivity { DatabaseQueries dq = new DatabaseQueries(this); private ArrayList<Event> eventDetails; public String typeSear
public class CategoryActivity extends ActionBarActivity {
DatabaseQueries dq = new DatabaseQueries(this);
private ArrayList<Event> eventDetails;
public String typeSearchTerm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Intent i = getIntent();
typeSearchTerm = i.getStringExtra(String.valueOf(R.string.event_type_extra));
//TRY TO FILL THE ARRAYLIST WITH ALL EVENTS
DatabaseQueries dq = new DatabaseQueries(this);
try {
//eventDetails = dq.getAllEventsCategory(typeSearchTerm);
eventDetails = dq.getAllEvents();
Log.d("ARRAY SIZE", eventDetails.size() + "");
Date d = new Date();
// Event e = new Event("BBQ", "bottle cap",d, "This is an event", "www.google.com", d, d );
// eventDetails.add(e);
// Log.d("Array Size after", eventDetails.size() + "");
} catch (ParseException e) {
e.printStackTrace();
}
final ListView lv1 = (ListView)findViewById(R.id.categoryListView);
lv1.setAdapter(new eventItemAdapter(this, eventDetails));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
Event objEvent = (Event)o;
Toast.makeText(CategoryActivity.this, "You have chosen : " + " " + objEvent.getEventName(), Toast.LENGTH_SHORT).show();
}
});
}}
公共类CategoryActivity扩展了ActionBarActivity{
DatabaseQueries dq=新数据库查询(此);
私有ArrayList事件详细信息;
公共字符串类型搜索项;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_类别);
Intent i=getIntent();
typeSearchTerm=i.getStringExtra(String.valueOf(R.String.event_type_extra));
//尝试用所有事件填充ARRAYLIST
DatabaseQueries dq=新数据库查询(此);
试一试{
//eventDetails=dq.getAllEventsCategory(类型搜索术语);
eventDetails=dq.getAllEvents();
Log.d(“数组大小”,eventDetails.SIZE()+”);
日期d=新日期();
//事件e=新事件(“烧烤”,“瓶盖”,d,“这是一个事件”,“www.google.com”,d,d);
//事件详情。添加(e);
//Log.d(“之后的数组大小”,eventDetails.Size()+”);
}捕获(解析异常){
e、 printStackTrace();
}
最终ListView lv1=(ListView)findViewById(R.id.categoryListView);
lv1.setAdapter(新的eventItemAdapter(这个,eventDetails));
lv1.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
对象o=lv1.getItemAtPosition(位置);
事件对象=(事件)o;
Toast.makeText(CategoryActivity.this,“您已选择:”+“”+objEvent.getEventName(),Toast.LENGTH\u SHORT.show();
}
});
}}
我的适配器:
public class eventItemAdapter extends BaseAdapter{
private static ArrayList<Event> eventDetails;
private LayoutInflater mInflater;
private static final SimpleDateFormat parser = new SimpleDateFormat("dd/MM/yyyy"); //dd-MM-yyyy
private static final SimpleDateFormat parseTime = new SimpleDateFormat("hh:mm a");//hh:mm a
public eventItemAdapter(Context context, ArrayList<Event> results) {
eventDetails = results;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return eventDetails.size();
}
@Override
public Object getItem(int i) {
return eventDetails.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.eventDetailName = (TextView)convertView.findViewById(R.id.eventDetailsName);
holder.eventDetailLocation = (TextView)convertView.findViewById(R.id.eventDetailsLocation);
holder.eventDetailDate = (TextView)convertView.findViewById(R.id.eventDetailsDate);
holder.eventDetailStartTime = (TextView)convertView.findViewById(R.id.eventDetailsStartTime);
convertView.setTag(holder);
} else{
holder = (ViewHolder)convertView.getTag();
}
holder.eventDetailName.setText(eventDetails.get(position).getEventName());
holder.eventDetailLocation.setText(eventDetails.get(position).getEventLocation());
Date temp = new Date();
temp = eventDetails.get(position).getEventDate();
holder.eventDetailDate.setText(parser.format(temp));
Date tempTime = new Date();
tempTime = eventDetails.get(position).getEventStartTime();
holder.eventDetailStartTime.setText(parseTime.format(tempTime));
//Return the current view
return convertView;
}
static class ViewHolder{
TextView eventDetailName;
TextView eventDetailLocation;
TextView eventDetailDate;
TextView eventDetailStartTime;
} }
公共类eventItemAdapter扩展了BaseAdapter{
私有静态ArrayList事件详细信息;
私人停车场;
私有静态最终SimpleDataFormat解析器=新SimpleDataFormat(“dd/MM/yyyy”);//dd-MM-yyyy
私有静态最终SimpleDataFormat parseTime=新SimpleDataFormat(“hh:mm a”);//hh:mm a
公共eventItemAdapter(上下文、ArrayList结果){
eventDetails=结果;
mInflater=LayoutInflater.from(上下文);
}
@凌驾
public int getCount(){
返回eventDetails.size();
}
@凌驾
公共对象getItem(int i){
返回eventDetails.get(i);
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.listview_行,空);
holder=新的ViewHolder();
holder.eventDetailName=(TextView)convertView.findViewById(R.id.eventDetailsName);
holder.eventDetailLocation=(TextView)convertView.findViewById(R.id.eventDetailsLocation);
holder.eventDetailDate=(TextView)convertView.findViewById(R.id.eventDetailsDate);
holder.eventDetailsStartTime=(TextView)convertView.findViewById(R.id.eventDetailsStartTime);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.eventDetailName.setText(eventDetails.get(position.getEventName());
holder.eventDetailLocation.setText(eventDetails.get(position.getEventLocation());
日期临时=新日期();
temp=eventDetails.get(position.getEventDate();
holder.eventDetailDate.setText(parser.format(temp));
日期=新日期();
testime=eventDetails.get(position.getEventStartTime();
holder.eventDetailStartTime.setText(parseTime.format(testime));
//返回当前视图
返回视图;
}
静态类视窗夹{
TextView事件详细名称;
TextView事件详细信息位置;
TextView事件详细日期;
TextView事件详细信息开始时间;
} }
这是项目视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="@+id/event_photo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/eventDetailsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#33CC33"
android:layout_marginTop="25dp"
android:layout_marginBottom="6dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time: "
android:layout_marginRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/eventDetailsStartTime"
android:layout_column="3" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: "
android:layout_marginRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/eventDetailsDate"
android:layout_column="3" />
</TableRow>
</TableLayout>
</LinearLayout>
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location: "
android:layout_marginRight="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/eventDetailsLocation"
android:layout_column="3" />
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView7" />
</LinearLayout>
以及实际的ListView:
<?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">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/categoryListView"/>
</LinearLayout>
编辑:
getAllEventsCategory()的代码:
public ArrayList getAllEventsCategory(字符串类别)引发异常{
SQLiteDatabase dq=this.getReadableDatabase();
ArrayList全局=新的ArrayList();
String query=“选择e.”+事件ID+”,e.“+事件名称+”,e.“+事件协会ID+”,e.。”
+事件位置+”,e.“+事件链接+”,e.“+事件开始时间+”,e
+事件结束时间+”,等“+事件类型+”来自“+表事件+”e左连接”
+表_具有_类别+“e上的h”+事件_ID+“=h.“+具有_事件_ID”
+“连接h上的“+表事件类型+”et.“+具有事件类型ID+”=et。”
+事件类型ID+“其中et.”+事件类型+“=\”+类别+“\”;”;
Log.d(“查询”,“查询+查询”);
Cursor Cursor=dq.rawQuery(查询,空);
int numRows=cursor.getCount();
//for循环将每个事件添加到arraylist
对于(int i=0;ipublic ArrayList<Event> getAllEventsCategory(String category) throws ParseException {
SQLiteDatabase dq = this.getReadableDatabase();
ArrayList<Event> global = new ArrayList<Event>();
String query = "SELECT e." + EVENT_ID + ", e." + EVENT_NAME + ", e." + EVENT_SOCIETY_ID + ", e."
+ EVENT_LOCATION + ", e." + EVENT_LINK + ", e." + EVENT_START_TIME + ", e."
+ EVENT_END_TIME + ", et." + EVENT_TYPE + " FROM " + TABLE_EVENT + " e LEFT JOIN "
+ TABLE_HAS_CATEGORY + " h ON e." + EVENT_ID + " = h." + HAS_EVENT_ID
+ " JOIN " + TABLE_EVENT_TYPE + " et ON h." + HAS_EVENT_TYPE_ID + " = et."
+ EVENT_TYPE_ID + " where et." + EVENT_TYPE + " = \"" + category + "\";";
Log.d("QUERY ", "" + query);
Cursor cursor = dq.rawQuery(query, null);
int numRows = cursor.getCount();
//for loop to add each event to the arraylist
for (int i = 0; i < numRows; i++) {
Event e = new Event();
e.setEventID(Integer.parseInt(cursor.getString(0)));
e.setEventName(cursor.getString(1));
e.setSocietyID(Integer.parseInt(cursor.getString(2)));
e.setEventLocation(cursor.getString(4));
String myDate = cursor.getString(5);
e.setEventDate(parser.parse(myDate));
e.setEventDescription(cursor.getString(6));
e.setEventLink(cursor.getString(7));
String myStartTime = cursor.getString(8);
e.setEventStartTime(parseTime.parse(myStartTime));
String myEndTime = cursor.getString(9);
e.setEventEndTime(parseTime.parse(myEndTime));
global.add(e);
if (i < numRows) {
cursor.moveToPosition(i + 1);
}
if (i > numRows) {
cursor.moveToFirst();
}
}
return global;
}
public ArrayList<Event> getAllEvents() throws ParseException {
SQLiteDatabase dq = this.getReadableDatabase();
ArrayList<Event> global = new ArrayList<Event>();
Log.d("QUERY ", "" + query);
Cursor cursor = dq.rawQuery(query, null);
int numRows = cursor.getCount();
cursor.moveToFirst();
if(cursor.moveToFirst()){
cursor.moveToFirst();
//for loop to add each event to the arraylist
for (int i = 0; i < numRows; i++) {
Event e = new Event();
e.setEventID(Integer.parseInt(cursor.getString(0)));
e.setEventName(cursor.getString(1));
e.setSocietyID(Integer.parseInt(cursor.getString(2)));
e.setEventLocation(cursor.getString(3));
String myDate = cursor.getString(4);
e.setEventDate(parser.parse(myDate));
e.setEventDescription(cursor.getString(5));
if(cursor.getString(6) != null) {
e.setEventLink(cursor.getString(6));
}else{
e.setEventLink("");
}
String myStartTime = cursor.getString(7);
e.setEventStartTime(parseTime.parse(myStartTime));
Date date = new Date();
date.setTime(0);
if(cursor.getString(8) != null){
String myEndTime = cursor.getString(8);
e.setEventEndTime(parseTime.parse(myEndTime));
}else{
e.setEventEndTime(date);
}
global.add(e);
if (i < numRows) {
cursor.moveToPosition(i + 1);
}
if (i > numRows) {
cursor.moveToFirst();
}
}
}else{
Log.d("Results", "no results");
}
Log.d("Array size", "" + global.size());
return global;
}