I want to implement a dialog box in on click of listitem of list view in my adapter class. How can I access my ListView from another class?
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = " + listView.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
Why do you make in your Adapter class. Use listview.setOnItemClickListener(this) in your activity or fragment.
or
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
in your onCreate() method.
Try this -
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = " + getItem(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
or implement onItemClick() listener in your activity/fragment containing the ListView.
Your example code uses onClick, you should have onItemClick.
Your code will look like this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyAdapter adapter = (MyAdapter) parent.getAdapter; // use your actual adapter class name here
AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = " + adapter.getItem(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
And make sure you have a proper implementation of getItem in your adapter.
Related
I have ArrayList of object (citiesInSpinner) each object have two value(Id, Name)
I have already get it in alert dialog
I use this function to alert dialog:
public void test()
{
FillSpinner();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchFligtsActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Select City");
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,citiesInSpinner);
lv.setAdapter(adapter);
/* alertDialog.setItems(citiesInSpinner, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
/////
}
});*/
alertDialog.show();
/**/
}
now I want to make something to get the ((ID)) of item (I mean the Id of City)
I tried to do that but I failed...
any help Please !!
and thank you
Simply use
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
City city = citiesInSpinner.get(position)
//get your id -> city.Id
}
});
i have a Listview which displays list of clients, i have added an onClickListner to Listview so that i can get the detailed information of clicked client.
ListView l = (ListView) findViewById(R.id.jl);
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ClientListView j = JList.get(position);
String mess = "you clicked position " + position + " With Name " + j.GetClientName();
Toast.makeText(Home.this,mess,Toast.LENGTH_LONG).show();
}
}
);
}
i want to display information but not in toast, i will prefer some activity,fragment or some popup king of thing.
can anybody help?
Use ths code:
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ClientListView j = JList.get(position);
String mess = "you clicked position " + position + " With Name " + j.GetClientName();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ActivityName.this);//Your activity name
// set title
alertDialogBuilder.setTitle("Hello!");
// set dialog message
alertDialogBuilder.setMessage(mess)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
Hope this will work..
If you want to pass Client Name which you are displaying in list, you can get id from adapter and use it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String clientName= (String) ((TextView) view
.findViewById(R.id.client_name)).getText();
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("client", clientName);
startActivity(intent);
}
}
}
ListView l = (ListView) findViewById(R.id.jl);
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ClientListView j = JList.get(position);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Keyname", j);
startActivity(intent);
}
}
);
}
You can get the intent value in another activity.
If I have understood, you want to send some information to another activity to display this information.
You can have a look to the "Intent" in Android. It's used to start a component like an activity and can carry information to this new activity to start.
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("CUSTOMER_INFO", customer.getName());
startActivity(i);
Note: If you want to pass a custom object between activities, like for instance a List of custom object :
List<'CustomObject'>
Your custom object class have to implement Parcelable.
Here is my code:
When I click on textview one dialog displaying list is shown. When i select particular list item dialog is not dismissed. how to dismiss dialog when list item is clicked,
educationtxt=(TextView)findViewById(R.id.education_txt);
String[] educationarray = new String[]{"High School","Som College","Associates Degree","Bachelor Degree","Masters Degree","PHD"};
educationtxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final ListView lv ;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegistrationActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Education");
View convertView1 = (View) inflater.inflate(R.layout.custom_dialog_row, null);
TextView tv =(TextView)convertView1.findViewById(R.id.list_row_txt);
lv = (ListView) convertView.findViewById(R.id.custom_listView1);
lv.setBackgroundColor(Color.WHITE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(RegistrationActivity.this,R.layout.custom_dialog_row,R.id.list_row_txt,educationarray);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
education_selected = lv.getAdapter().getItem(position).toString();
educationtxt.setText(education_selected);
Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
//here i want dismiss
}
});
alertDialog.show();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
education_selected = lv.getAdapter().getItem(position).toString();
educationtxt.setText(education_selected);
Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
//here i want dismiss
alertDialog.dissmiss(); <----- add this lone to dismiss
}
});
and make alertdialoge as a final
Just Call this method and Show Dialog. Also Dismiss Dialog On Click ListItem
public void showDialog(){
final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
dialog.setTitle("Choose App");
dialog.setCancelable(true);
View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_dialog_all_app, null);
list = (ListView) view.findViewById(R.id.AllAppList);
AllAppPckName = getPackages();
AllAppListAdapter adapter= new AllAppListAdapter(getContext(), R.layout.app_item, AllAppPckName);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
appIcon.setVisibility(View.VISIBLE);
appIcon.setImageDrawable(getPackageIcon(getContext(), AllAppPckName.get(i)));
appNameBtn.setText(getAppNameFromPkgName(getContext(), AllAppPckName.get(i)));
dialogg.dismiss();
}
});
dialog.setView(view);
dialogg = dialog.show();
}
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();
}
I have an Alert Dialog which has a list on it, and i want to close onlistclick is it possible?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
ListView myList = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
}
});
builder.setTitle("Please Choose");
builder.setInverseBackgroundForced(true);
builder.setView(myList);
final Dialog dialog = builder.create();
dialog.show();
}
The alert dialog is running perfect i just dont want to put any buttons in it.
If you define the onItemClickListener after the Dialog you can just call dialog.dismiss(); in the onItemClick() method.
check below code
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
dialog.dismiss();
}
});