Created
January 24, 2015 11:03
-
-
Save dodyw/71c7a536d895d61988b3 to your computer and use it in GitHub Desktop.
Detect onclick event from imageview using interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dodyrw.mpos; | |
/** | |
* Created by dodyrw on 1/14/15. | |
*/ | |
public interface MyClickAction { | |
void onImageClick(int position); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dodyrw.mpos; | |
import android.app.Dialog; | |
import android.content.Intent; | |
import android.graphics.drawable.ColorDrawable; | |
import android.support.v4.app.NavUtils; | |
import android.support.v7.app.ActionBar; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.Window; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class OrderListActivity extends ActionBarActivity implements /*AdapterView.OnItemClickListener,*/ MyClickAction { | |
Toolbar toolbar; | |
TextView screenTitle; | |
ListView list; | |
ImageView btnProcess; | |
ImageView btnAddItem; | |
String[] number = { | |
"1", | |
"2", | |
"3", | |
"4", | |
"5", | |
"6" | |
}; | |
String[] code = { | |
"454334", | |
"454684", | |
"454365", | |
"454211", | |
"454856", | |
"454975" | |
}; | |
String[] itemName = { | |
"Beef Enchilada Stack", | |
"Loaded Potato Soup", | |
"Sparkling Straweberry Lemonade Slush", | |
"Spaghetti Pie", | |
"Fun Sundaes", | |
"Lemon Tea" | |
}; | |
String[] qty = { | |
"3", | |
"1", | |
"3", | |
"1", | |
"2", | |
"1" | |
}; | |
String[] price = { | |
"80,000", | |
"75,000", | |
"60,000", | |
"40,000", | |
"12,000", | |
"4,000" | |
}; | |
//MyClickAction clickAction; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_order_list); | |
initWidget(); | |
initEvent(); | |
} | |
private void initWidget() { | |
toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
if (toolbar != null) { | |
toolbar.setLogo(R.drawable.toolbar_icon); | |
toolbar.setTitle(""); | |
setSupportActionBar(toolbar); | |
} | |
getSupportActionBar().setHomeButtonEnabled(true); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
screenTitle = (TextView) findViewById(R.id.screen_title); | |
screenTitle.setText("Order List"); | |
OrderListListAdapter adapter = new OrderListListAdapter(OrderListActivity.this, this, number, code, itemName, qty, price); | |
list = (ListView) findViewById(R.id.list); | |
list.setAdapter(adapter); | |
btnProcess = (ImageView) findViewById(R.id.process_imageview); | |
btnAddItem = (ImageView) findViewById(R.id.add_imageview); | |
// add waiter info on action bar | |
ActionBar actionBar = getSupportActionBar(); | |
actionBar.setDisplayShowCustomEnabled(true); | |
LayoutInflater inflator = (LayoutInflater) this .getSystemService(this.LAYOUT_INFLATER_SERVICE); | |
View v = inflator.inflate(R.layout.waiter_info_actionbar_custom_view_top_right, null); | |
actionBar.setCustomView(v); | |
} | |
private void initEvent() { | |
btnProcess.setOnClickListener(new ImageView.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
processOrder(); | |
} | |
}); | |
btnAddItem.setOnClickListener(new ImageView.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
addItem(); | |
} | |
}); | |
} | |
private void addItem() { | |
startActivity(new Intent(this, OrderAddActivity.class)); | |
} | |
private void processOrder() { | |
startActivity(new Intent(this, ChoosePaymentActivity.class)); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
if(id==android.R.id.home){ | |
NavUtils.navigateUpFromSameTask(this); | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
public void showDialogDelete(int position) { | |
Log.e("MPOS","showDialogDelete: "+position+": "+itemName[position]); | |
// custom dialog | |
final Dialog dialog = new Dialog(this); | |
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); | |
dialog.setContentView(R.layout.dialog_order_list_delete); | |
// set the custom dialog components - text, image and button | |
TextView itemName = (TextView) dialog.findViewById(R.id.item_textview); | |
itemName.setText("\"Spaghetti Pie\""); | |
ImageView cancelButton = (ImageView) dialog.findViewById(R.id.cancel_imageview); | |
ImageView okButton = (ImageView) dialog.findViewById(R.id.ok_imageview); | |
cancelButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
dialog.dismiss(); | |
} | |
}); | |
okButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
dialog.dismiss(); | |
} | |
}); | |
dialog.show(); | |
} | |
@Override | |
public void onImageClick(int position) { | |
showDialogDelete(position); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dodyrw.mpos; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
/** | |
* Created by dodyrw on 1/11/15. | |
*/ | |
public class OrderListListAdapter extends BaseAdapter { | |
private Context mContext; | |
private final String[] no; | |
private final String[] code; | |
private final String[] itemName; | |
private final String[] qty; | |
private final String[] price; | |
MyClickAction clickAction; | |
private ImageView imgDelete; | |
//private ViewClickListener mViewClickListener; | |
public OrderListListAdapter(Context c, MyClickAction action, String[] no, String[] code, String[] itemName, String[] qty, String[] price) { | |
mContext = c; | |
this.no = no; | |
this.code = code; | |
this.itemName = itemName; | |
this.qty = qty; | |
this.price = price; | |
this.clickAction = action; | |
} | |
@Override | |
public int getCount() { | |
// TODO Auto-generated method stub | |
return code.length; | |
} | |
@Override | |
public Object getItem(int position) { | |
// TODO Auto-generated method stub | |
return itemName[position]; | |
} | |
@Override | |
public long getItemId(int position) { | |
// TODO Auto-generated method stub | |
return position; | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
// TODO Auto-generated method stub | |
View view; | |
LayoutInflater inflater = (LayoutInflater) mContext | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
if (convertView == null) { | |
view = new View(mContext); | |
view = inflater.inflate(R.layout.order_list_list_row, null); | |
TextView txtNumber = (TextView) view.findViewById(R.id.number_textview); | |
TextView txtCode = (TextView) view.findViewById(R.id.code_textview); | |
TextView txtItemName = (TextView) view.findViewById(R.id.item_name_textview); | |
TextView txtQty = (TextView) view.findViewById(R.id.qty_textview); | |
TextView txtPrice = (TextView) view.findViewById(R.id.price_textview); | |
imgDelete = (ImageView) view.findViewById(R.id.delete_imageview); | |
txtNumber.setText(no[position]); | |
txtCode.setText(code[position]); | |
txtItemName.setText(itemName[position]); | |
txtQty.setText(qty[position]); | |
txtPrice.setText(price[position]); | |
imgDelete.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// call implement method | |
clickAction.onImageClick(position); | |
} | |
}); | |
} else { | |
view = (View) convertView; | |
} | |
return view; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment