2014-07-05 170 views
0

我是新來的,我正在使用FragmentList,並且想要點擊de點擊事件。我能做些什麼? becouse我onitemclick不捕獲去點擊:( 我試圖把可聚焦TextView的假,因爲我在一個崗位並沒有讀它。執行點擊監聽器到ListFragment

public class FgShopsInfoList extends ListFragment implements OnItemClickListener { 
String[] countries = new String[] { 
    "India", 
    "Pakistan", 
    "Sri Lanka", 
    "China", 
    "Bangladesh", 
    "Nepal", 
    "Afghanistan", 
    "North Korea", 
    "South Korea", 
    "Japan" 
}; 

// Array of integers points to images stored in /res/drawable/ 
int[] flags = new int[]{ 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher, 
    R.drawable.ic_launcher 
}; 

// Array of strings to store currencies 
String[] currency = new String[]{ 
    "Indian Rupee", 
    "Pakistani Rupee", 
    "Sri Lankan Rupee", 
    "Renminbi", 
    "Bangladeshi Taka", 
    "Nepalese Rupee", 
    "Afghani", 
    "North Korean Won", 
    "South Korean Won", 
    "Japanese Yen" 
}; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_shops_info_list, container,false); 

    // 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", "Country : " + countries[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 
    // R.layout.listview_layout defines the layout of each item 
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.fragment_shops_info_list, from, to); 


    setListAdapter(adapter); 



    return super.onCreateView(inflater, container, savedInstanceState); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 
    Log.e("Xx","A"); 

} 
} 

而且佈局

<?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:contentDescription="XXXXX" 
    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" 

     /> 

</LinearLayout> 

</LinearLayout> 
+1

根據您的代碼我不認爲您的列表甚至顯示...您必須返回您的視圖,而不是super.onCreateView ...... 更改行返回super.onCreateView(inflater,容器,savedInstanceState); 返回視圖 – Hasandroid

回答

0

使用此方法

@Override 
public void onListItemClick(ListView l, View v, int pos, long id) { 
super.onListItemClick(l, v, pos, id); 
//do something here 
} 

This是文檔。

選擇列表中的項目時,將調用此方法。子類應該重寫。如果子類需要訪問與選定項相關的數據,它們可以調用getListView()。getItemAtPosition(position)。

參數

在點擊發生

v被點擊的ListView控件內的視圖中的ListView

位置視圖列表中的位置

ID被點擊的項目的行ID