how can i change my search with search position by id? - android

I have two spinner the second one is to link with the first one with position and a list view that shows a CV when you click on an item that is linked with position too, I want to replace it will be linked with id, how can I do please
this is my code spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
if(arrayListTache.size() > position)
{
spinner_tache.setSelection(position);
pos = position;
valueInteger = arrayListInteger.get(position);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
my code onclick list view
SubjectListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int
pos, long id) {
String selectedItem =
parent.getItemAtPosition(pos).toString();
Intent intent = new
Intent(getApplicationContext(),Main2Activity.class);
intent.putExtra("Position" , String.valueOf(id));
startActivity(intent);
// insert();
}
});

Related

how to get id of item selected in spinner and return int value

int txt= (int) parent.getItemIdAtPosition(position)
When i display txt variable in Toast my application makes crush and i need to get id of selected Item and then save that id in other table please help me
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String id = spinner.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Mapping items in list view to number of clicks

I have a ListView populated with a custom Array adapter. I want to get the total number of clicks on each item in the ListView .How can i map the listview item id and number of clicks on that item for multiple items?
Use this code.. exact.. hardcoded the whole code for you :p
HashMap<Integer,Integer> map_positions_and_count = new HashMap<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i<listView.getAdapter().getCount()-1; i++)
{
if (i == position)
{
if (map_positions_and_count .get(position) !=null)
{
map_positions_and_count .put(position,map_positions_and_count .get(position)+1);
}
else {
map_positions_and_count .put(position,1);
}
}
}
// map.put(position,)
}
});
You can use this in your mainActivity ..
You can try this in your activity,
ArrayList<Integer> arrayList=new ArrayList<>();//make this as global
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(arrayList.get(position)!=null) {
int a = arrayList.get(position);
arrayList.set(position, a+1);
}
else
arrayList.set(position,1);
}
});
Try this code ->
final HashMap<Integer, Integer> listItemCount = new HashMap<Integer, Integer>();
//Set an onItemClickListener
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listItemCount.containsKey(position)){
listItemCount.put(position, listItemCount.get(position)+1);
}else {
listItemCount.put(position, 1);
}
}
});

How do I get the generated key for an item add to Firebase

Trying to get the generated key for item added, when using the onItemClickListener,
Please see my code, :
ref = new Firebase("https://.......firebaseio.com/Service");
final FirebaseListAdapter<MainCategory> adapter = new FirebaseListAdapter<MainCategory>(getActivity(), MainCategory.class,
android.R.layout.simple_list_item_1,
ref) {
#Override
protected void populateView(View view, MainCategory s, int i) {
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText(s.getTitle());
}
};
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemId = String.valueOf(parent.getId());
String root = String.valueOf(ref.getRoot().getKey());
MainCategory object = (MainCategory) parent.getItemAtPosition(position);
Toast.makeText(getContext(), itemId+"____"+"__"+root, Toast.LENGTH_SHORT).show();
}
});
You're looking for:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String key = adapter.getRef(position).getKey()
}

How simple clic or long clic whit a custom adapter?

I have a custom adapter and in my maiActivity I have elementosList charged with a custom adapter and I have my code:
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String elementoSeleccionado = parent.getItemAtPosition(position).toString();
Boolean realizado = getRealizadoPorElemento(elementoSeleccionado);
if (realizado == true){
actualizaRealizado(elementoSeleccionado,"N");
elementosList.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
}else if (realizado == false){
actualizaRealizado(elementoSeleccionado,"S");
elementosList.getChildAt(position).setBackgroundColor(Color.parseColor("#3DF400"));
}
}
};
elementosList.setOnItemClickListener(listener);
//Long clic
elementosList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
elementoSeleccioando = parent.getItemAtPosition(position).toString();
return false;
}
});
But it is not working.
I need implement some on my custom adapter to the click? or is here the error?

onItemClick and onItemLongClick not firing in ListView until an item has been pressed once

In my ListView I have these two methods, and they DO work, but not until one of the list items have been pressed once. So, nothing happens the first time I press/hold an item, but the next time I press/hold an item it works perfectly.
Here's my onListItemClick method
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
File file = new File(path.get(position));
longClick(file);
Log.d(TAG, "onItemLongClick");
return true;
}
});
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position ,long id) {
File file = new File(path.get(position));
player(file);
Log.d(TAG, "onItemClick");
};
});
}
You are setting setOnItemLongClickListener and setOnItemClickListener inside of onListItemClick, so They won't fire until you press the ListItem for the first time.
What you need to do is to this code outside of onListItemClick.
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
File file = new File(path.get(position));
longClick(file);
Log.d(TAG, "onItemLongClick");
return true;
}
});
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position ,long id) {
File file = new File(path.get(position));
player(file);
Log.d(TAG, "onItemClick");
};
});

Categories

Resources