0

私のフラグメントにはリストがあります。リストにデータを表示したい。これはアダプタークラスです:

@Override
    public int getCount() {
        10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Log.e(TAG, "---");
        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.list_topicslist, null);
            holder      = new ViewHolder();

            holder.tvTitle          = (TextView)        convertView.findViewById(R.id.tvTitle);
            holder.tvExcerpt        = (TextView)        convertView.findViewById(R.id.tvExcerpt);
            holder.tvDate           = (TextView)        convertView.findViewById(R.id.tvDate);
            holder.tvAuthor         = (TextView)        convertView.findViewById(R.id.tvAuthor);
            holder.tvComment_Count  = (TextView)        convertView.findViewById(R.id.tvComment_Count);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText("Title");
        holder.tvExcerpt.setText("Content");
        holder.tvAuthor.setText("Hesam");
        holder.tvComment_Count.setText("Votes:");
        holder.tvDate.setText("1/1/2013");

        return convertView;
    }

これは list_topicslist.xml です。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rlContainer"
        android:background="@color/White" >


    <RelativeLayout
            android:id="@+id/rlHeader"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="25dp" android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" android:background="@color/bg_list_title">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_author"
                android:id="@+id/tvAuthor" android:layout_alignParentRight="true" android:layout_centerVertical="true"
                android:layout_margin="@dimen/sideMargin" android:lines="1" android:maxLines="1"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_date"
                android:id="@+id/tvDate" android:layout_alignParentLeft="true"
                android:layout_margin="@dimen/sideMargin" android:maxLines="1"
                android:lines="1" android:layout_centerVertical="true"/>
    </RelativeLayout>

    <RelativeLayout
            android:id="@+id/rlBody"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_below="@id/rlHeader" android:clickable="false">

        <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/ivPostImage" android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" android:layout_margin="@dimen/sideMargin"
                android:src="@drawable/ic_loading_green"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_title"
                android:id="@+id/tvTitle" android:layout_toLeftOf="@+id/ivPostImage"
                android:layout_alignTop="@+id/ivPostImage" android:layout_margin="@dimen/sideMargin" android:lines="1"
                android:maxLines="3"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_excerpt"
                android:id="@+id/tvExcerpt" android:layout_toLeftOf="@+id/ivPostImage"
                android:layout_below="@+id/tvTitle" android:layout_margin="@dimen/sideMargin" android:lines="1"
                android:maxLines="6"/>
    </RelativeLayout>

    <RelativeLayout
            android:id="@+id/rlFooter"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@id/rlBody"
            android:background="@color/bg_list_footer">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_comment_count"
                android:id="@+id/tvComment_Count" android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" android:layout_margin="@dimen/sideMargin"
                android:layout_centerVertical="true" android:lines="1" android:maxLines="1"/>
    </RelativeLayout>

</RelativeLayout>

これはフラグメントのxmlです:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" >

    <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lvTopicsList"
            android:scrollbars="none"
            android:scrollingCache="false"
            tools:listitem="@layout/list_topicslist" />
</RelativeLayout>

アプリケーションを実行するとリストが表示されませんが、ログには getView() が呼び出されたことが示されます。

02-28 21:09:10.195: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.220: D/dalvikvm(16184): GC_CONCURRENT freed 2720K, 33% free 11321K/16775K, paused 2ms+1ms
02-28 21:09:10.235: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.235: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.235: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.240: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.240: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.250: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.275: E/*** TopicAdapter ***(16184): ---
02-28 21:09:10.295: D/dalvikvm(16184): GC_CONCURRENT freed 262K, 31% free 11693K/16775K, paused 2ms+2ms

私の間違いは何ですか?任意の提案をいただければ幸いです。ありがとう

4

2 に答える 2

0
@Override
 public Object getItem(int position) {
 return position;// change null to position
 }

ここでビデオを見てください。http://www.youtube.com/watch?v=wDBM6wVEO70。メモリ管理の明確なアイデアを得るには、このリンクhttp://www.youtube.com/watch?v=_CruQY55HOkをご覧ください。

同様の質問をご覧ください GC_concurrent freed を解決する方法は? . ヒープがいっぱいのようです。

上記のリンクは、トラブルから抜け出す方法を見つけるのに役立ちます。MAT アナライザーを使用して、メモリ リークをチェックします。

于 2013-02-28T13:07:58.313 に答える
0

問題は、layout_width を match-parent の代わりに 0dp に設定するように要求する lint による愚かな提案が原因でした。アクティビティの xml コード (上記には記載されていません) を activity_main.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">

    <fragment
            android:name="com.kamalan.fragments.Fragment_TopicsList"
            android:id="@+id/fragment"
            android:layout_width="match_parent"   <==================
            android:layout_height="match_parent"
            android:layout_gravity="left|center_vertical" />
</LinearLayout>

現在、アプリケーションは正常に動作しています。


アップデート

この投稿は役に立ちました:なぜ 0dp はパフォーマンスの向上と見なされるのですか?

于 2013-03-01T07:39:53.347 に答える