Set Action Listener on ImageView - android

Hey guys i am have a listview and it has an imageview inside of every row. what i want is when i tap/touch/click i want the checkbox to clicked like the gmail app.
my code so far is:
public void registerCallClickBack() {
ListView list = (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View viewClicked,
int position, long id) {
TextView tv1 =(TextView)viewClicked.findViewById(R.id.stock_name);
TextView tv2 =(TextView)viewClicked.findViewById(R.id.stock_price);
Intent intent = new Intent(MainActivity.this, StockItem.class);
intent.putExtra("name", tv1.getText().toString());
intent.putExtra("price",tv2.getText().toString());
intent.putExtra("stockInfo", stocksList.get(position));
startActivity(intent);
ImageView imageCheck = (ImageView)viewClicked.findViewById(R.id.imagestartinglist);
imageCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
chk = (CheckBox)viewClicked.findViewById(R.id.checkBoxMainList);
if (!chk.isShown()){
chk.setVisibility(CheckBox.VISIBLE);
if(!chk.isChecked()){
chk.setChecked(true);
}
}
}
});
}
});
}
i dont know what i am missing. and when i click on the image it checks the one i select and some others. how can i fix it??maybe with ontouch listener??
any help will be much appreciated and accepted!!!thanks in advance!!!

Related

Android: ListView Trouble with textview.isChecked() and textview.setChecked()

I have a listview which uses this layout:
android.R.layout.simple_list_item_checked
I have an OnItemClickListener that gets the check status of the clicked item and either checks or unchecks that item but it does not work. There are NO errors in logcat. When I click on an item in the listview simply nothing happens.
The OnItemClickListener looks like this:
//------------------- OnItemClickListener -----------------------------
lvCheckList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedTextView textview = (CheckedTextView)view;
if (!textview.isChecked()){
textview.setChecked(true);
}else {
textview.setChecked(false);
}
}
});
Try with this...
//sample code
TextView TxtName = (TextView) findViewById(R.id.NameControlId);
TxtName.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.NameControlId:
Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_SHORT).show();
break;
}
}

Delete ListView row in Custom Adapter

I want to delete a row from my list view on click of "delete" button. My listview item has following things placed horizontally: TextView-1,TextView-2,TextView-3,ImageButton-delete button. Now when I click delete button, the row should be deleted from the view. Below is the adapter code;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_name);
String item = tv.getText().toString();
String tableno = mListItems.get(0).getTableNumber();
orderDetailsDB = new OrderDetailsDBAdapter(
getApplicationContext());
orderDetailsDB.deleteItem(item,tableno);
I tried by setting Individual textviews to blank but its not working.
holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");
I read couple of posts and they suggest to remove item from my list(mListItems) and then do adapter.notifyDataSetChanged();. Problem is I am not using array adapter for populating list view but using Custom adapter, so unable to get the position for item to be deleted. Please advise. thanks.
First write below line in your adapter's getView method.
button.setTag(position)
in onClick method
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
yourarraylistObject.remove(position);
// your remaining code
notifyDataSetChanged();
}
Just use remove() to remove list item from the adapter
for your reference
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
What you can do in order to get the position that you want to delete is to pass that into your listener:
// inside custom adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// do your delete code here
notifyDataSetChanged();
}

Multiple List Views in Android

In my android app I have two List Views and two buttons.On clicking first button, first List View will be visible and on second button second List View will be visible.In one List View I will have list of items and in second List View there will be ADD button. Now clicking on ADD button my first List View will be visible and user can select items to add in second List View. I have done it successfully. But my problem is, by clicking on second button List View is appearing but there are no data which are added from First List View. Below is my code.
What am I missing?
final ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
final View header = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(header, null, false);
btnCollege.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItems[] = {};
final ArrayAdapter<String> string = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1);
lv.setAdapter(string);
Button btnAdd = (Button) header.findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (j == 0)
{
lv.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
j = 1;
}
else if (j == 1)
{
lv.setVisibility(View.INVISIBLE);
header.setVisibility(View.INVISIBLE);
j = 0;
}
else {}
lv1.setVisibility(View.VISIBLE);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String selected = lv1.getItemAtPosition(arg2).toString();
string.add(selected);
string.notifyDataSetChanged();
setListAdapter(string);
}
});
}
});
}
});
The view is not getting updated. use handlers for updating listview. That would help you solve the issue

how to pass the value of row in listview to a button

I have created a custom listview using SimpleAdapter and in each row of list view i put a button has single id.
I want to get the position of each row to pass the button but i have a single button id for each row and I want when I click on button it find the position of row and start another activity
please help me
public void click(View v){
//RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
TextView tv = (TextView)findViewById(R.id.text1);
ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
ListView lv=(ListView)findViewById(android.R.id.list);
int position = 0;
Long id=Long.parseLong((String) adapter.getItem(position));
Intent i=null;
switch(position){
case 1:
i=new Intent(this, ButtonActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, PickerActivity.class);
startActivity(i);
break;
}
try this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
final ProgressDialog progressBar = ProgressDialog.show(
VechiclesDetails.this, "", loadingString);
progressBar.setIndeterminate(true);
progressBar.setIndeterminateDrawable(getResources()
.getDrawable(R.anim.progressbar_handler));
new Thread(new Runnable() {
public void run() {
try {
Intent carDetail = new Intent(
Details.this,
Screen.class);
carDetail.putExtra("index", arg2);
carDetail.putExtra("sellerId", SellerId);
startActivity(carDetail);
progressBar.dismiss();
} catch (Exception e) {
progressBar.dismiss();
}
}
}).start();
}
});
I am not sure about this, but lets try it, and inform me about your success..
The line , int position = listView.getPositionForView(v.getParent());
public void click(View v){
// This line is important
int position = listView.getPositionForView(v.getParent());
//RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
TextView tv = (TextView)findViewById(R.id.text1);
ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
ListView lv=(ListView)findViewById(android.R.id.list);
int position = 0;
Long id=Long.parseLong((String) adapter.getItem(position));
Intent i=null;
switch(position){
case 1:
i=new Intent(this, ButtonActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, PickerActivity.class);
startActivity(i);
break;
}
}
If this not works then only option you have to is making a Custom Adapter and write your button's onClick() in adapter's getView() in which you have a position variable accessible.
On list view, you can use setOnItemClickListener
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
//Put Your code here
}
public void onNothingSelected(AdapterView parentView) {
}
});

How do I select a row in a listview not using ListActivity

Here is the code:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find your views
final ListView listPro = (ListView) findViewById(R.id.listProperty);
DBAdapter db = new DBAdapter(this);
db.open();
final Cursor c = db.getAllProperties();
startManagingCursor(c);
// Create the adapter
MainAdapter adapter = new MainAdapter(this, c);
listPro.setAdapter(adapter);
// here is where I think I should put the code to select the row but I
// haven't been able to figure out how to do it.
// all I need to get out is the row id number.. please help
Button addPropBut = (Button) findViewById(R.id.mainButton);
addPropBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i7 = new Intent(Main.this, Home.class);
startActivity(i7);
}
});
}
}
try this one, hope it will help you out.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
you can get the id number by using Log or System.out.println();
Instead of using Button's setOnClickListener use ListView's setOnItemClickListener.
listPro.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
The id parameter will give you the needed id.
Salil.

Categories

Resources