2014-09-01 2 views
1

내 조각 안에있는 listview에 수신기를 추가하려고합니다. 나는 그것을 할 수있는 몇 가지 방법을 시도했지만 여전히 작동하지 않습니다. 나는 다른보기를 focusable="false"으로 만들 수 없습니다. 목록보기 위에 EditText가 있기 때문입니다. 항목을 클릭 할 때마다 내가 만든 JSONAdapter에서 getView이 호출됩니다.ListView OnClickListener가 조각 내에서 작동하지 않습니다.

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

    mJSONAdapter = new JSONAdapter(getActivity(), inflater); 

    View v = inflater.inflate(R.layout.header, container, false); 
    return v; 
} 
@Override 
public void onViewCreated(View view,Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    ListView lv = (ListView) getView().findViewById(android.R.id.list); 
    // Set the ListView to use the ArrayAdapter 
    lv.setAdapter(mJSONAdapter);//isi datany pk json adapter 
    lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show(); 
      JSONObject jsonObject = (JSONObject) mJSONAdapter.getItem(position);//ambil item yg diclick 
      String raceid = jsonObject.optString("id","");//ambil cover id yg diklik, argumen kedua itu klo null defaultny apa 

      // create an Intent to take you over to a new DetailActivity 
      Intent detailIntent = new Intent(getActivity(), DetailActivity.class); 

      // pack away the data about the cover 
      // into your Intent before you head out 
      detailIntent.putExtra("raceid", raceid); 
      // start the next Activity using your prepared Intent 
      startActivity(detailIntent); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

    new RetrieveFeedTask().execute(); 

} 

답변

1

이 코드를 사용해보십시오.

lv.setOnItemClickListener(new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> adapter, View v, int position, 
      long arg3) 
     { 

      // do your work 
     } 
    }); 
+0

코드 만 대답하는 것은 바람직하지 않습니다. – Emmanuel

0

내가 잘못된 방법을 선택하는 것이 나타납니다

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

      <ImageView 
       android:focusable="false" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:src="@drawable/ic_launcher"/> 

      <EditText 
       android:id="@+id/searchquery" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:hint="Search" 
       /> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@android:id/list"> 

    </ListView> 

</LinearLayout> 

여기 내 조각입니다 : 여기 내 XML이다. OnItemClickListener()가 있어야하며 대신 OnItemSelectedListener()를 선택하십시오.

관련 문제