1
我是android編程的新手。在我的程序onItemClick事件沒有響應。我通過JSON加載數據。當我從xml佈局中刪除單選按鈕代碼正常工作。Android onItemClick事件不響應
public class Taketest extends ListActivity``
{
private ProgressDialog pDialog;
String question,ans1,ans2,ans3,ans4,ans5;
ArrayList<HashMap<String, String>> productsList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
list.setOnItemClickListener(new OnItemClickListener()
{ @Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String selected = ((TextView) view.findViewById(R.id.textView2)).getText().toString();
}
});
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Taketest.this);
pDialog.setMessage("Loading Questions... Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
int success = 1;// or 0
if (success == 1) {
products = json.getJSONArray(TAG);// load product through json
for (int i = 0; i < products.length(); i++)
{
JSONObject c = products.getJSONObject(i);
String id = c.getString("id");
question = c.getString("question");
ans1 = c.getString("op1");
ans2 = c.getString("op2");
ans3 = c.getString("op3");
ans4 = c.getString("op4");
ans5 = c.getString("op5");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("id", id);
map.put("ques", question);
map.put("a",ans1);
map.put("b",ans2);
map.put("c",ans3);
map.put("d",ans4);
map.put("e",ans5);
// adding HashList to ArrayList
productsList.add(map);
}
}
else {
Intent i = new Intent(getApplicationContext(),
Signup.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run()
{
ListAdapter adapter =
new SimpleAdapter(Taketest.this, productsList, R.layout.list_item, new String[]{"id", "ques","a","b","c","d","e"},
new int[]{R.id.pid, R.id.name, R.id.rb1, R.id.rb2, R.id.rb3, R.id.rb4, R.id.rb5});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
和我的XML是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:id="@+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb3"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb4"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb5"
android:checked="false" />
</RadioGroup>
</LinearLayout>
單選按鈕塊項目點擊。檢查http://stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items和http://stackoverflow.com/questions/9885803/listview-onclicklistener-does-not-work -after-加入-單選按鈕 – nbaroz