简体   繁体   English

空白ListView在Logcat中没有错误-Android和自定义适配器

[英]Blank ListView with no errors in Logcat - Android & custom adapter

I've been trying to create a listView that display event items, only it isn't showing anything. 我一直在尝试创建一个显示事件项目的listView,只是它没有显示任何内容。 I've pretty much just followed this example. 我几乎只是遵循示例。 My dq.getAllEventsCategory() method returns an Arraylist of Event Items. 我的dq.getAllEventsCategory()方法返回事件项的数组列表。

Here's my Activity: 这是我的活动:

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();
        }
    });


}}

My Adapter: 我的适配器:

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;
    } }

This is the item View: 这是项目视图:

    <?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>

And the the actual ListView: 和实际的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>

EDIT: 编辑:

Code for getAllEventsCategory(): getAllEventsCategory()的代码:

public 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;
}

I've also tried without a specific category search (as in just using the arraylist returned by my getAllEvents method) but i'm still getting a null pointer exception. 我也尝试了没有特定类别的搜索(就像仅使用我的getAllEvents方法返回的arraylist一样),但是我仍然遇到空指针异常。 I get a null pointer exception with getAllEvents and an empty/blank listview with getAllEventsCategory 我在getAllEvents中得到一个空指针异常,在getAllEventsCategory中得到一个空/空白列表视图

getAllEvents() method: getAllEvents()方法:

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;
}

This is my logcat. 是我的日志。 the d log "Array size" is the size of the array that should be the parameter for the lv1.setAdapter() method d日志“数组大小”是应作为lv1.setAdapter()方法的参数的数组大小

First of all you must consider these points while using listview, 首先,在使用列表视图时,您必须考虑以下几点,

  • The height of list item view should be "wrap_content" .If you make it "match_parent", the first row take all space on device and there is no room for the rest of row. 列表项视图的高度应为“ wrap_content” 。如果将其设置为“ match_parent”,则第一行将占用设备上的所有空间,其余行将没有空间。

  • The height of Listview should be "match_parent" .If you make it "wrap_content", the recycling concept of list view is not used because it creates all rows of listview as listview display first time.It also affects the scrolling of listview and consume a lot of memory. Listview的高度应为“ match_parent” 。如果将其设置为“ wrap_content”,则不会使用列表视图的回收概念,因为它将第一次创建所有viewview的行作为listview显示。它还会影响listview的滚动并消耗很多内存。

You use the wrong Id for the listview in java class.The listview id in XML is " listViewCategory " and listview id used in java class is " categoryListView ". 您为Java类中的listview使用了错误的ID。XML中的listview id为“ listViewCategory ”,而在Java类中使用的listview id为“ categoryListView ”。

One thing more is that,please make sure that there must be items in array (eventDetails) at the time of setAdapter(). 还有一点是,请确保setAdapter()时数组(eventDetails)中必须有项目。

Have you fixed your problem? 您解决了问题吗? I found that you have placed a table row outside your table layout in the item view. 我发现您已在项目视图中的表格布局之外放置了表格行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM