Thursday, 18 September 2014

Items inside a ListView - android



i have a list view which contains 2 text views and 3 buttons, and i want to do something when click on one of thoses buttons but where to write OnclickListener of thoses buttons ?!


my java class



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://ift.tt/1ue2GqT";
private static final String url_delete_product = "http://ift.tt/1ue2Iz6";

// 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);
}
});
}}


my list_item_2.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
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>


can someone please help me with this ?


thank you ..:)


No comments:

Post a Comment