我有一个列表视图,其中包含2个文本视图和3个按钮,我想点击其中一个按钮但在哪里写按钮的OnclickListener时会做什么?!
我的java类
public class AdminGlossary extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://www.lamia.byethost18.com/get_all_glossaries.php";
private static final String url_delete_product = "http://www.lamia.byethost18.com/delete_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "ID_glossary";
private static final String TAG_NAME = "name";
private static final String TAG_DES = "description";
private static final String TAG_CHA = "ID_chapter";
String pid="0";
// products JSONArray
JSONArray products = null;
JSONParser jsonParser = new JSONParser();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_glossary);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
/*lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});*/
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AdminGlossary.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String des = c.getString(TAG_DES);
String cha = c.getString(TAG_CHA);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_DES, des);
map.put(TAG_CHA, cha);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
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() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AdminGlossary.this, productsList,
R.layout.list_item_2, new String[] { TAG_PID,
TAG_NAME, TAG_DES, TAG_CHA},
new int[] { R.id.pid, R.id.name, R.id.des, R.id.cha });
// updating listview
setListAdapter(adapter);
}
});
}}
我的list_item_2.xml
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:text="@string/ddd"
android:textSize="17dip"
android:textStyle="bold" />
<TextView
android:id="@+id/des"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/name"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:text="@string/dd"
android:textSize="17dip"
android:textStyle="bold" />
<Button
android:id="@+id/cha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/des"
android:background="@null"
android:text="@string/d"
android:textColor="#0000ff"
android:textSize="15sp" />
<Button
android:id="@+id/deletee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/name"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button"
android:textSize="15dp" />
<Button
android:id="@+id/editt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cha"
android:layout_alignParentRight="true"
android:layout_below="@+id/deletee"
android:text="Button"
android:textSize="15dp" />
</RelativeLayout>
有人可以帮帮我吗?
谢谢.. :))
答案 0 :(得分:0)
有几种方法可以做到这一点:
1。)设置&#34; android:onClick&#34;布局文件中每个按钮的属性指向要调用的方法。它看起来像这样:
<Button
android:id="@+id/cha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/des"
android:background="@null"
android:text="@string/d"
android:textColor="#0000ff"
android:textSize="15sp"
android:onClick="<methodname>"/>
2。)在ListView的适配器中生成每个按钮时,为每个按钮设置onClickListener。
希望这有帮助!
答案 1 :(得分:0)
在BaseAdapter类中添加按钮的侦听器。下面是一个示例代码。如果在列表视图中单击按钮,我将执行一些操作。
public class People_Adapter extends BaseAdapter {
private LayoutInflater inflater = null;
private ImageButton delete_contact;
private Context context;
@Override
public int getCount() {
return list.size() / 2;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my list_item_2, parent, false);
delete_contact = (ImageButton) convertView.findViewById(R.id.excl_ppl_delete);
delete_contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Add code here what needs to be done onclick
});
}
return convertView;
}
}