4

我在我的項目中設置了listfragment。但似乎我的片段無法通過我的適配器正確完成。其原因是Context contextMyListAdapter。如果我點擊糾正它。它變爲MenuFragment menuFragment。但是在更改之後,MyListAdapter出現錯誤。所以我糾正它。它變爲Context context。如果我糾正它,它仍然繼續下去。它的循環就是這樣。使用自定義適配器的ListFragment

注意:我想實現的是帶圖標的ListFragment。像我之前的其他問題(但不幸的是沒有人回答)。

public class MenuFragment extends ListFragment { 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return super.onCreateView(inflater, container, savedInstanceState); 
    } 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    String [] proMenu ={ "Homies", "Best Nearby", "Coupon" , "Profile" , "History" , "", "Setting" , 
       "About" , "Sign Out"}; 

    setListAdapter(new MyListAdapter(this, proMenu)); 

} 

@Override 
public void onListItemClick(ListView lv, View v, int position, long id) { 
    Fragment newContent = null; 
    switch (position) { 
    case 0: 
     newContent = new ColorFragment(); 
     break; 
    case 1: 
     Intent intent7 = new Intent(); 
     intent7.setClass(getActivity(), Home.class); 
     intent7.putExtra("index", position); 
     startActivity(intent7); 
     break; 

編輯:這是我的佈局。它現在完美無瑕。我只需要調整textview和線性佈局,這樣一個字就不會減半。但我面臨着另一個問題。它就像背景圖像堆疊起來一樣。這是我的佈局上的xml。

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

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="50dp" 
android:orientation="horizontal" 
android:cacheColorHint="#00000000" 
android:background="@drawable/menu_drawer"> 

<ImageView 
    android:id="@+id/row_icon" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:padding="10dp" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/row_title" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical" 
    android:padding="10dp" 
    android:text="Medium Text" 
    android:textSize="20dp" 
    android:textAppearance="@android:style/TextAppearance.Medium" /> 

</LinearLayout> 

the layout

如果我刪除此android:background="@drawable/menu_drawer"linear layout。這將是完美的背景。沒有彼此堆放。但是當我在列表中滑動時,背景變得瘋狂,它消失了,並顯示出一些黑色背景。它像列表視圖android:cacheColorHint="#00000000"的問題一樣。我已經在linear layout中添加了cachecolor。但它仍然顯示出那些黑色背景。就像這樣。

this is what happen when i remove <code>android:background</code>

我知道什麼這個問題的。因爲默認背景是黑色的。但我不知道如何解決它。

編輯2:黑色問題解決。

已解決。

+1

首先,你用'機器人:layout_height =「50dp」'該行高度,這是正常的,該文本將被削減,因爲它不能適合身高(尤其是當你使用一個20dp大小(用sp代替))。使用'wrap_content'作爲行高。其次,'cacheColorHint'屬性需要在ListView中使用,而不是行背景(可以在代碼中設置)。 – Luksprog 2013-04-11 08:29:56

+0

謝謝。你爲我節省了很多。其實我有1個最後的問題。如何在歷史和環境之間創造空間。所以這3個列表將會出現在底部(設置,關於和退出)。 – Nicolas 2013-04-11 14:29:55

+1

如果這是某種菜單,那麼最好用一個'LinearLayout'替換'ListView',它將包含這些行,這樣就可以像想要的那樣分開它們。您仍然可以通過計算屏幕所需的確切空間並添加具有該高度的額外空行來使用「ListView」。 – Luksprog 2013-04-11 14:46:22

回答

7

您不會將有效的Context傳遞到您的適配器,Fragment不是 a Context。您需要使用,例如,Activity作爲Context

setListAdapter(new MyListAdapter(getActivity(), proMenu)); 

我希望你們也實現適配器中getCount()方法,否則你不會看到任何ListView不管有多少個元素你有。

+0

很多非常感謝你這麼多非常感謝。終於在一個星期後解決。 FYI =我沒有實現getCount(),它仍然完美地工作。但是有一個問題。我的文本看起來不太適合列表。可以說當菜單「附近」。單詞「y」的底部並沒有填入名單。它只是一半。它就像「你」。 – Nicolas 2013-04-11 05:13:05

+0

@Nicolas文字在底部被切斷?你能發佈你的佈局文件嗎? – Luksprog 2013-04-11 06:15:35

+0

我編輯了「添加布局」的問題。謝謝 – Nicolas 2013-04-11 07:21:10

0

創建自定義適配器時,不要將「Context」對象作爲參考傳遞給Activity,只需將Activity對象傳遞給Custom Adaper即可。

public class HealthAdvice extends ListFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.health_advice, container, false); 

     return rootView; 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     HealthAdviceArray health_data[] = new HealthAdviceArray[] 
       { 
       new HealthAdviceArray(R.drawable.ic_launcher, "Cloudy"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Showers"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Snow"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Storm"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Sunny") 
       }; 

     HealthAdviceAdapter adapter = new HealthAdviceAdapter(getActivity(), 
       R.layout.health_advice_item_row, health_data); 

     /** Setting the array adapter to the list view */ 
     setListAdapter(adapter); 

    } 

}// end main class HealthAdvice 
1
Following is the method to create listFragement list view: 
HealthAdvice.java 

package com.example.babs; 


import android.app.ListFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class HealthAdvice extends ListFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.health_advice, container, false); 

     return rootView; 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     HealthAdviceArray health_data[] = new HealthAdviceArray[] 
       { 
       new HealthAdviceArray(R.drawable.ic_launcher, "Cloudy"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Showers"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Snow"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Storm"), 
       new HealthAdviceArray(R.drawable.ic_launcher, "Sunny") 
       }; 

     HealthAdviceAdapter adapter = new HealthAdviceAdapter(getActivity(), 
       R.layout.health_advice_item_row, health_data); 

     /** Setting the array adapter to the list view */ 
     setListAdapter(adapter); 

    } 

}// end main class HealthAdvice 

Step 2: 

HealthAdviceAdapter.java 
package com.example.babs; 

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class HealthAdviceAdapter extends ArrayAdapter<HealthAdviceArray>{ 

    Context context; 
    int layoutResourceId;  
    HealthAdviceArray data[] = null; 

    public HealthAdviceAdapter(Context context, int layoutResourceId, HealthAdviceArray[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     HealthAdviceArrayHolder holder = null; 

     if(row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new HealthAdviceArrayHolder(); 
      holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); 
      holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 

      row.setTag(holder); 
     } 
     else 
     { 
      holder = (HealthAdviceArrayHolder)row.getTag(); 
     } 

     HealthAdviceArray weather = data[position]; 
     holder.txtTitle.setText(weather.title); 
     holder.imgIcon.setImageResource(weather.icon); 

     return row; 
    } 

    static class HealthAdviceArrayHolder 
    { 
     ImageView imgIcon; 
     TextView txtTitle; 
    } 
} 

Step 3: 

HealthAdviceArray.java 

package com.example.babs; 


public class HealthAdviceArray { 

    public int icon; 
    public String title; 

    // we are over loading here 
    public HealthAdviceArray(){ 
     super(); 
    } 

    public HealthAdviceArray(int icon, String title) { 
     super(); 
     this.icon = icon; 
     this.title = title; 
    } 

} 

step 4: 

Health Advice health_advice.xml 

<?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:background="#FFFFFF" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

step 5: 

Health Advice items: health_advice_item_row.xml 

<?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:padding="10dp" > 

    <ImageView 
     android:id="@+id/imgIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="5dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="5dp" 
     android:contentDescription="@string/image_view" 
     android:gravity="center_vertical" /> 

    <TextView 
     android:id="@+id/txtTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:gravity="center_vertical" 
     android:textColor="#000000" 
     android:textSize="22sp" 
     android:textStyle="bold" /> 

</LinearLayout> 
相關問題