我已经在我的show category dataloaded方法中声明了我的setonitemclick监听器。如果在列表中单击了一个项目,则会有一个toast未被触发。这个数据加载方法在我的receivertask类中调用,其中列表由服务器数据填充
#Activity class#
private ArrayList <CategoryModel>categoryList;
//private static final String[]paths = {"All", "Favourites"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.bioscope.R.layout.categorylist);
save=(Button)findViewById(R.id.save);
RecieveCategoriesTask task = new RecieveCategoriesTask(this,"all");
task.execute();
save.setVisibility(View.VISIBLE);
}
public void showcateogryDataLoaded(ArrayList<CategoryModel> categoryList) {
this.categoryList = categoryList;
for(CategoryModel c:categoryList)
{
Log.v("title", c.getTitle());
}
CategorysListAdapter adapter=new CategorysListAdapter(this, categoryList);
list=(ListView) findViewById(com.bioscope.R.id.listView1);
list.setAdapter(adapter);
>>list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(CategorySelectonActivity.this, "You Clicked an item ", Toast.LENGTH_SHORT).show();
System.out.print("done");
//showEventInformaion(position);
CheckBox cb = (CheckBox) findViewById(R.id.select_checkbox);
if(cb.isChecked())
cb.setChecked(false);
else
cb.setChecked(true);
}
});
}
在listview中,我设置了可点击的真实
#categorylist 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:layout_margin="0dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:orientation="vertical"
android:padding="0dp" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
</TableRow>
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#C0C0C0"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
</ListView>
</LinearLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="bottom"
>
<Button android:layout_width="fill_parent"
android:id="@+id/save"
android:visibility="invisible"
android:layout_height="wrap_content"
android:text="SAVE"
android:background="#0099cc"
/>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="100dp"
>
<ImageView
android:id="@+id/category_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/category_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/category_icon"
android:textSize="28sp"
android:textColor="#ffcccccc"
android:text="Category Title" />
<TextView
android:id="@+id/category_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/category_icon"
android:layout_below="@+id/category_title"
android:textSize="14sp"
android:textColor="#ffcccccc"
android:text="Cateogry Description" />
<CheckBox
android:id="@+id/select_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/category_title"
android:layout_alignBottom="@+id/category_title"
android:layout_alignParentRight="true"
android:background="#ffcccccc"
android:text=""
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
ProgressDialog dialog;
String responseString;
CategorySelectonActivity appContext;
CreateEventActivity createeventActivity_appContext;
String url;
String pageUrl = "get-event-category";
private String userIdParameter="user_id=";
// String name,email,pass,phone;
public RecieveCategoriesTask(CategorySelectonActivity c, String critiria) {
appContext = c;
// url = Utility.baseUrl + pageUrl + critiria;
// url = Utility.baseUrl + pageUrl;
url = "http://bioscopebd.com/mobileappand/getusercategory?"+userIdParameter+"259214143443955553";
Log.i("url", url);
}
public RecieveCategoriesTask(CreateEventActivity createEventActivity,
String critiria) {
createeventActivity_appContext = createEventActivity;
// url = Utility.baseUrl + pageUrl + critiria;
// url = Utility.baseUrl + pageUrl;
url = "http://bioscopebd.com/mobileappand/getusercategory?"+userIdParameter+"259214143443955553";
}
protected void onPreExecute() {
dialog = new ProgressDialog(appContext==null?createeventActivity_appContext:appContext);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Loading Categories...");
dialog.show();
super.onPreExecute();
}
String filterResponseString(String r) {
return r.replace("\r\n", "");
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
responseString = filterResponseString(responseString);
} else {
// Closes the connection.
response.getEntity().getContent().close();
Utility.showMessage(appContext, "Cannot Connect To Internet");
}
} catch (Exception e) {
// TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
if (responseString != null) {
ArrayList<CategoryModel> categoryList = new ArrayList<CategoryModel>();
;
JSONArray jsonArr;
try {
Log.v("json", responseString);
jsonArr = new JSONArray(responseString);
// jsonArr = events.getJSONArray("events");
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
CategoryModel category = new CategoryModel();
category.setId(jsonObj.getString("event_cat_id"));
category.setTitle(jsonObj.getString("event_cat_title"));
category.setDescription(jsonObj.getString("event_cat_desc"));
category.setStatus(jsonObj.getBoolean("status"));
// event.setLogo(jsonObj.getString("logo").getBytes(Charset.forName("UTF-8")));
/*
* event.setStartDate(jsonObj.getString("startDate"));
* event.setEndDate(jsonObj.getString("endDate"));
*
* event.setStartTime(jsonObj.getString("startTime"));
* event.setEndTime(jsonObj.getString("endTime"));
* event.setCountry(jsonObj.getString("country"));
* event.setCity(jsonObj.getString("city"));
* event.setAddress(jsonObj.getString("address"));
* event.setLogo(jsonObj.getString("logo"));
* event.setBanner(jsonObj.getString("banner"));
* event.setPhone(jsonObj.getString("phone"));
* event.setEmail(jsonObj.getString("email"));
*
* event.setWeblink(jsonObj.getString("weblink"));
* event.setPublishDate(jsonObj.getString("publishDate"));
* event.setCategoryId(jsonObj.getString("categoryId"));
*/
categoryList.add(category);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (appContext != null)
{
appContext.showcateogryDataLoaded(categoryList);
// CategorySelectonActivity c = new CategorySelectonActivity(); // c.visibleSaveButton();
}
// appContext.showCategoryListDataLoaded(responseString);
if(createeventActivity_appContext!=null){
createeventActivity_appContext.showcateogryDataLoaded(categoryList);
}
}
super.onPostExecute(result);
// Do anything with response..
}
我错过了什么?
答案 0 :(得分:2)
您是否收到任何错误(强制关闭)?
请参阅此代码:
CheckBox cb = (CheckBox) findViewById(R.id.select_checkbox);
将其更改为:
CheckBox cb = (CheckBox) view.findViewById(R.id.select_checkbox);
因为我不看到您的CheckBox
布局中有Activitiy's
,所以CheckBox
应该在您的列表项中。因此,在使用view
onItemClick
的参数中的findViewById
<强>更新强>
尝试将android:focusable="false"
和android:focusableInTouchMode="false"
添加到您的CheckBox
项目中。