我有兩個Buttons
以及文本ListView
。當我點擊按鈕時Onclick監聽器不工作。Onclick無法在列表視圖中的按鈕來獲取TextView Android
以下是我的代碼:
list_details.xml
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp" />
list_item_details.xml
<TextView
android:id="@+id/try_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="tile try" />
<Button
android:id="@+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
ListDetailsActivity.java
公共無效的onCreate(捆綁savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_details);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
Button download = (Button) findViewById(R.id.download);
download.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TextView try_txt = (TextView) findViewById(R.id.try_it);
String try_text_val = try_txt.getText().toString();
Toast.makeText(getApplicationContext(), try_text_val, Toast.LENGTH_LONG).show();
}
});
}
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
ListDetailsActivity.this, productsList,
R.layout.list_item_details, new String[] { TAG_TRY_IT},
new int[] { R.id.try_it});
// updating listview
setListAdapter(adapter);
}
});
}
}
非常感謝您的幫助!
你可以在這裏找到一個例子http://stackoverflow.com/a/4461244/1007522它非常簡單。 – user1007522