2013-04-05 123 views
0

它在getBaseContext()的代碼行中顯示錯誤。我不知道什麼heppen在這裏,我試圖代替笑着轉接器陣列適配器,但它顯示錯誤.right現在即時通訊與簡單的適配器..它給我的錯誤做..在android中使用圖標,標題和描述列表視圖

SimpleAdapter(getBaseContext(), aList, R.layout.list,   
     from, to); 

headFragment.java

public class HeadFragment extends ListFragment { 
OnHeadlineSelectedListener mCallback; 

// The container Activity must implement this interface so the fragment can deliver 
    messages 
public interface OnHeadlineSelectedListener { 
    /** Called by HeadlinesFragment when a list item is selected */ 
    public void onArticleSelected(int position); 
} 
static String[] Headlines = { 
    "Article One", 
    "Article Two", 
    "Article 3" 
}; 
int[] flags = new int[]{ 
     R.drawable.ic_11, 
     R.drawable.ic_12, 
     R.drawable.ic_13 

    }; 
String[] currency = new String[]{ 
     "Indian Rupee", 
     "Pakistani Rupee", 
     "Sri Lankan Rupee", 

    }; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 


// Each row in the list stores country name, currency and flag 
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();   

    for(int i=0;i<10;i++){ 
     HashMap<String, String> hm = new HashMap<String,String>(); 
     hm.put("txt", "Headlines: " + Headlines[i]); 
     hm.put("cur","Currency : " + currency[i]); 
     hm.put("flag", Integer.toString(flags[i]));    
     aList.add(hm);   
    } 

    // Keys used in Hashmap 
    String[] from = { "flag","txt","cur" }; 

    // Ids of views in listview_layout 
    int[] to = { R.id.flag,R.id.txt,R.id.cur};   

    // Instantiating an adapter to store each items 

    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.list,   
     from, to); 
    // Getting a reference to listview of main.xml lagetBayout file 
    ListView listView =(ListView) findViewById(R.id.listview); 

    // Setting the adapter to the listView 
    listView.setAdapter(adapter);  } 

list.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="match_parent" 
android:orientation="horizontal" 
> 
<ImageView 
    android:id="@+id/flag" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:paddingTop="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="10dp" 
/> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
> 
    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15dp" 
    /> 

    <TextView 
     android:id="@+id/cur" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="10dp" 
    /> 
    <!-- Rightend Arrow --> 
<ImageView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/next" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true"/> 

</LinearLayout> 
    </LinearLayout> 
+0

你可以試試getApplicationContext。 – Unknown 2013-04-05 10:19:44

+0

謝謝你的回覆,但不能正常工作... – Abhay 2013-04-05 10:51:06

回答

1

insted的的getBaseContext()使用getActivity()getApplicationContext()


getApplicationContext()應用上下文與應用程序了關聯,並且將永遠是整個生命週期相同


getBasecontext()不應該使用jst使用上下文而不是它與活動相關聯,並可能是dest royed當活動中的片段被破壞


getActivity()返回片段與相關聯的活動的情況下。


1

使用getApplicationContext()Frgment context ..

+0

thankx for u reply but not working for ... .. – Abhay 2013-04-05 10:51:51

0

@Abhay沒有錯,你的代碼只需使用getApplicationContext()的代替getBaseContext(),我完全同意@DhavalSodhaParmar

要知道更多閱讀之間的區別getBaseContext()getApplicationContext()

+0

arrey pandeyjee chasma keedhar hai? – Nezam 2013-04-05 11:03:27

+0

@Nezam arey kyo nezamjee kya hua ?? dekho pehena toh hua hai – 2013-04-05 11:06:57

+0

@Abhay如果你仍然面臨一些問題,然後問我們..我們將盡力幫助你 – 2013-04-05 11:08:32

相關問題