2014-09-13 1 views
-1

그래서 JSON을 사용하여 외부 데이터베이스에 의해 채워지는 조각 안에 listview가 있습니다.단편에서 SimpleAdapter를 사용하여 JSON 채워진 목록보기에서 버튼에 onclicklstener 설정

listvie의 각 행에는 textview, imagebutton 및 checkbox가 있습니다.

imagebutton 및 체크 박스 기능을 제공하지만 작동하지 않는 이유가 있습니다.

<?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:background="@color/white" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.22" 
      android:background="#309BFF" 
      android:layout_marginLeft="5dp" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginTop="5dp" 
       android:layout_marginLeft="14dp" 
       android:layout_weight="0.81" 
       android:background="#309BFF" 
       android:textColor="@color/white" 
       android:textSize="25sp" /> 

      <ImageButton 
       android:id="@+id/callButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="10dp" 
       android:layout_gravity="center" 
       android:src="@drawable/phone" /> 
     </LinearLayout> 

     <CheckBox 
      android:id="@+id/selectCurrentCompany" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="14dp" /> 
    </LinearLayout> 

</RelativeLayout> 

이 버튼을 클릭 할 때 난 내 조각

ImageButton callButton; 

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

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

    DownloadTask downloadTask = new DownloadTask(); 
    downloadTask.execute(strUrl); 
    declarations(rootView, inflater); 

    return rootView; 
} 

public void declarations(View view, LayoutInflater mInflater) { 
    saveButton = (ImageButton) view.findViewById(R.id.saveButton); 
    infoButton = (ImageButton) view.findViewById(R.id.infoButton); 
    mListView = (ListView) view.findViewById(R.id.mListView); 

    saveButton.setOnClickListener(this); 
    infoButton.setOnClickListener(this); 

    View mView = mInflater.inflate(R.layout.taxi_companies_list_view, null); 
    callButton = (ImageButton) mView.findViewById(R.id.callButton); 
    callButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getActivity().getApplicationContext(), "callButton" , Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

private class ListViewLoaderTask extends 
     AsyncTask<String, Void, SimpleAdapter> { 

    JSONObject jObject; 

    @Override 
    protected SimpleAdapter doInBackground(String... strJson) { 
     try { 
      jObject = new JSONObject(strJson[0]); 
      JSONParser urlJsonParser = new JSONParser(); 
      urlJsonParser.parse(jObject); 
     } catch (Exception e) { 
      Log.d("JSON Exception1", e.toString()); 
     } 

     JSONParser urlJsonParser = new JSONParser(); 
     List<HashMap<String, Object>> names = null; 

     try { 
      names = urlJsonParser.parse(jObject); 
     } catch (Exception e) { 
      Log.d("Exception", e.toString()); 
     } 

     String[] from = {"name"}; 
     int[] to = { R.id.name}; 

     SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), names, 
       R.layout.taxi_companies_list_view, from, to); 

     return adapter; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void onPostExecute(SimpleAdapter adapter) { 

     mListView.setAdapter(adapter); 

     for (int i = 0; i < adapter.getCount(); i++) { 
      HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i); 
      String name = (String) hm.get("name"); 

      hm.put("name", name); 
      hm.put("position", i); 
     } 
    } 
} 

에서 그러나 어떤 이유가 무엇 내가하지 조각의 XML에서 listvieww의 XML에있는 것입니다 아무 반응이 없습니다. 나는 심지어 NPE를 얻지도 않는다.

+0

ImageButton 및 CheckBox 클릭 수신기를 처리하기위한 맞춤형 어댑터를 만들었다 고 생각합니다. –

+0

@HareshChhelana는 뷰를 수행하지 않습니다. mView = mInflater.inflate (R.layout.taxi_companies_list_view, null); 충분히? – user123859

+0

확인 : http://stackoverflow.com/questions/25375831/customarrayadapter-implementationunable-to-get-the-resource-id/25376257#25376257 사용자 정의 어댑터를 만드는 방법에 대해 알아 보겠습니다. –

답변

0

사용과 같은 일부 사용자 지정 어댑터 :

public class MyCustomAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

    public MyCustomAdapter(Context context, String[] values) { 
    super(context, R.layout.rowlayout, values); 
    this.context = context; 
    this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.taxi_companies_list_view, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.name); 

    Button yourButton=(Button)rowView.findViewById(R.id.callButton); 
    textView.setText(values[position]); 
    yourButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // create toast here 


      } 
     }); 


    return rowView; 
    } 
} 
+0

listview의 확인란 사용은 http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php의 좋은 예입니다. –

0

는 하위 뷰의 리스너를 구현하려면, 당신은 ArrayAdapter 또는 BaseAdapter을 연장하거나 사용자 정의 어댑터 클래스를 정의해야합니다. 사용자 정의 어댑터 클래스에서 항목의 xml 레이아웃 내에있는보기가 getView() 메쏘드 내에서 발견 될 수 있으며 원하는 모든보기에 구현하려는 모든 수신기를 구현할 수 있습니다.

관련 문제