Event click on ListView (extends ListActivity) - java

i have this code but i can't see the action when i click on a item from the list.
This code shows me the info on ArrayList profilesArrayList but i dont know how i check what item i am selecting form the listview. Anyone can help me?
profilesArrayList = new ArrayList<Profile>();
profilesArrayList = copyProfilesToArrayList();
ProfileAdapter adapter = new ProfileAdapter(
getApplicationContext(), R.layout.profiles_item, profilesArrayList);
listViewProfiles = (ListView)findViewById(android.R.id.list);
listViewProfiles.setAdapter(adapter);
listViewProfiles.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
switch(position) {
case 0:
Log.d("cardNumber", profilesArrayList.get(0).getCardNumber());
break;

If your container class for this code is the ListActivity, just override the onListItemClick for that class, rather than set it as an OnItemClickListener for the view. That works for me
public class ProfileList extends ListActivity
{
private ArrayList<Profile> profilesArrayList;
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
//populate your arraylist
setListAdapter ( new ArrayAdapter<Profile>() );
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Log.d("cardNumber", profilesArrayList.get(position).getCardNumber());
}
}

Try to use getSelectedItemPosition() and add INVALID_POSITION as one of your possible cases

Related

Delete a specific item from list in firebase with onClick

I have tried reading the documentation on it and I understand that I need to use removeValue() to accomplish this, I just don't know how to get the position of the click. I am pulling the list down from firebase and trying to use an OnItemClick() to say anytime something in the listview is clicked, it will be deleted. I feel like this is super simple and I'm just missing something.
public class DeleteChoiceListFragment extends Fragment {
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mRestReference = mRootRef.child("restaurants");
List<String> listofrest = new ArrayList<String>();
ListView restaurantListView;
ListAdapter restaurantListAdapter;
public DeleteChoiceListFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.restaurant_selection_list_frag,container,false);
restaurantListView = (ListView) view.findViewById(R.id.restaurantListView);
restaurantListAdapter = new FirebaseListAdapter<Restaurants>(getActivity(),Restaurants.class,R.layout.individual_restaurant_name_nocheckbox,mRestReference) {
#Override
protected void populateView(View v, Restaurants model, final int position) {
TextView restName = (TextView) v.findViewById(R.id.restname);
restName.setText(model.getName());
listofrest.add(position,model.getName());
}
};
restaurantListView.setAdapter(restaurantListAdapter);
restaurantListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
return view;
}
}
Going on 36 hours of no sleep now so my brain is 100% dead. I can provide more detail if necessary
Implement your setOnItemClickListener like this
restaurantListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Firebase itemtoRemove = restaurantListAdapter.getRef(i);
itemtoRemove.removeValue();
}
});
add your gradle
compile 'com.google.firebase:firebase-core:9.0.1'
compile 'com.google.firebase:firebase-database:9.0.1'

ListView for option list in AlertDialog

I'm trying to insert a listview inside an AlertDialog to display some options but I'm getting an error with the constructor: ArrayAdapter(this, R.layout.option_list, R.id.option, option_items);
It says cannot resolve constructor. The code is below.
private void LongClick() {
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View viewClicked, int position, long IDinDB) {
Cursor res = myDb.GetRow(IDinDB);
if (res.moveToFirst()) {
long idDB = res.getLong(DatabaseHelper.ROWID);
}
String[] option_items = {"Delete"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.option_list, R.id.option, option_items);
optionList.setAdapter(adapter);
optionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewGroup vg = (ViewGroup) view;
TextView txt = (TextView) vg.findViewById(R.id.option);
Toast.makeText(ViewActivity.this, txt.getText().toString(), Toast.LENGTH_LONG).show();
}
});
return true;
}
});
}
Any help will be appreciated.
When you write this it means the current object instance class you write in. When you wrote new AdapterView.OnItemLongClickListener() you create a new anonymous class, so the word this inside its methods will refer to this newly created class object. ArrayAdapter needs context instance in constructor, not a AdapterView.OnItemLongClickListener. So you need to reference the context object. You can get it:
a) from method parameter viewClicked object (viewClicked.getContext())
or
b) as
ViewActivity.this as you did in onItemClick

Retrieve Object From Spinner Array And Set It

I'm trying retrieve an object from my spinner array and set it to a certain constant, in this case "EFFECT_AQUA"
My array
String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};
when the user clicks on the "Aqua" in the spinner I want the screen to change to Aqua.
My spinner is set and called
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));
But not sure how should I approach. Seen many different answers but haven't found anything working.
I know my switch will come in this part
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
Any help is appricated!!
If you want to retrieve an object from your spinner array on item click, you can easily do it with the position parameter in the method onItemSelected.
For example :-
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelected = spinnerValues[position];
//Set this to a constant
}
}
Now if you select Aqua, then code will set the variable itemSelected to Aqua.
I hope this is what you need
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
cardStatusString = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
put the code below to get the item first then you can manipulate by taking the values like aqua or any color.
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
switch(tv.getText().toString()){
case "Aqua":{
//change the color to aqua
break;
}
case " ...":{
//
break;
}
//.... for all the option
}
}

Access main class member variable from setOnItemClickListener

I have implemented an activity with a simple ListView. In the java file, I have implemented OnItemClickListener() to respond to ListView item clicked.
public class MainActivity extends Activity
{
private String value;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
value = "Hello World";
final ListView listview = (ListView)findViewById(R.id.listview);
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ListValues);
listview.setAdapter(aa);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
// Get Item Clicked
String ItemSelected = (String)listview.getItemAtPosition(position);
// Access "value" containing "Hello World"
}
}
}
I need to access member variable value of my MainActivity class in onItemClick(). Kindly tell me how to do it.
Simply use:
MainActivity.this.value;
Or, even simpler, just as if it was a member of the OnClickListener:
value
Will also work.
you declared it globally so you can use it directly
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
//Get Item Clicked
String ItemSelected = (String)listview.getItemAtPosition(position);
Toast.makeText(getApplicationContext,value,Toast.LENGTH).show();
^
here
}

How to separate what is called an onListItemClick when a list item is clicked?

I have a list of items that include a checkbox. There is text on the left side of a list item and a check box on the right side. When I click a list item the checkbox is clicked and it fires the rest of the intent in the onListItemClick method. How do I change my code so that when a check box is checked the onListItemClick does one thing and when the text is clicked, the onListItemClick does something else?
I have a method like this to refresh my list items:
public void refreshlist(){
mymap = null;
mymap = providerTester.downLoadinfo(value1,value2);
list_my = new ArrayList<String>(mymap.keySet());
adapter = new ArrayAdapter<String>(ClassName.this, android.R.layout.simple_list_item_multiple_choice, list_my);
//using builtin list_item
getListView().setChoiceMode(2);
setListAdapter(adapter);
}
And then I have an onListClickListener setup like this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
My_DownLoad my_dl = (My_DownLoad) map_thoughts.get(my_list.get(position));
Integer id1 = position;
Log.i("ListOthers", "onListItemClick position: " + id1.toString());
Long id2 = id;
Log.i("ListOthers", "onListItemClick position: " + id2.toString());
//For Some reason id2 and id1 are the same
//I was thinking about setting up a case statement if something different happens if I click the checkbox and not the text
//start activity if text is checked
//change state of checkbox if checkbox is clicked
}
OK, after a lot of trial and error, I got this to work. I am pretty happy to finally see what I was looking for. Here is what I ended up with:
public void refreshlist(){
mymap = null;
mymap = providerTester.downLoadinfo(value1,value2);
list_my = new ArrayList<String>(mymap.keySet());
adapter = new ArrayAdapter<String>(ClassName.this,android.R.layout.simple_list_item_multiple_choice, list_my);
//using builtin list_item
getListView().setChoiceMode(2);
setListAdapter(adapter);
}
and the second method
#Override
protected void onListItemClick(ListView l, View v, int position, final long id){
super.onListItemClick(l, v, position, id);
final My_DownLoad my_dl = (My_DownLoad) map_thoughts.get(my_list.get(position));
final Intent i = new Intent(this, NextActivity.class);
l.setOnItemLongClickListener(new OnItemLongClickListener(){
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3){
}
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//add values to intent
startActivity(i);
return false;
}
});
}

Categories