所以我有一個使用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。
我想你已經爲句柄ImageButton和CheckBox點擊偵聽器製作了自定義的適配器。 – 2014-09-13 04:49:01
@HareshChhelana沒有查看mView = mInflater.inflate(R.layout.taxi_companies_list_view,null);足夠? – user123859 2014-09-13 04:51:42
檢查:http://stackoverflow.com/questions/25375831/customarrayadapter-implementationunable-to-get-the-resource-id/25376257#25376257這一些想法如何使您的自定義適配器。 – 2014-09-13 05:02:15