2014-09-13 69 views
-1

所以我有一個使用JSON的外部數據庫填充片段內的列表視圖。設置onclicklstener內JSON填充列表視圖中使用SimpleAdapter在一個片段

listvie中的每一行都有一個textview,一個imagebutton和一個複選框。

我想給imagebutton和複選框的功能,但由於soe原因它不工作。

這是我在listvieww XML,而不是在片段XML

<?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); 
     } 
    } 
} 

但由於某些原因被點擊按鈕時什麼都沒發生。我甚至沒有得到一個NPE。

+0

我想你已經爲句柄ImageButton和CheckBox點擊偵聽器製作了自定義的適配器。 – 2014-09-13 04:49:01

+0

@HareshChhelana沒有查看mView = mInflater.inflate(R.layout.taxi_companies_list_view,null);足夠? – user123859 2014-09-13 04:51:42

+0

檢查:http://stackoverflow.com/questions/25375831/customarrayadapter-implementationunable-to-get-the-resource-id/25376257#25376257這一些想法如何使您的自定義適配器。 – 2014-09-13 05:02:15

回答

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 – 2014-09-13 05:05:14

0

要實現偵聽器子視圖,你必須通過擴展ArrayAdapterBaseAdapter來定義自定義適配器類。在自定義適配器類中,您可以在getView()方法內找到位於項目xml佈局內部的視圖,並且可以實現您希望實現的任何監聽器,以便實現您希望的任何視圖。