Mapping items in list view to number of clicks - android

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);
}
}
});

Related

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

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();
}
});

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()
}

Clear all List in spinner items

I have try this code but one array list will be clear but second data not clear please give me solution in 1st spinner clear spinner 2 data but spinner 2 cant clear 3rd spinner data please tell me solution
my array list
private ArrayList<String> students;
private ArrayList<String> teachers;
private ArrayList<String> CastName;
private ArrayList<String> SubCastName;
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerreligiontext.setText(getNames(position));
stst = spinnerreligiontext.getText().toString();
System.out.println(getNames(position));
SubCastName.clear();
CastName.clear();
CastName();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
aaa = getNamess(position);
System.out.println("spin2"+getNamess(position));
SubCastName.clear();
SubCastName();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}); spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
aaa = getSubNamess(position);
System.out.println("spin3"+getSubNamess(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
set adapter code
for (int i = 0; i < j.length(); i++) {
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
students.add(json.getString("AllInOne"));
} catch (JSONException e) {
e.printStackTrace();
}
}
spinner1.setAdapter(new ArrayAdapter<String>(RagistarActivity.this, android.R.layout.simple_spinner_dropdown_item, teachers));
Since you are using four array to populate the spinner you can clear it by .clear() method, so
students.clear();
teachers.clear();
CastName.clear();
SubCastName.clear();
else you can set the adapter to null at the point were you need to clear spinner items

How can I pick only once a value from spinner in android

I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable

How to get object value from listview adapter position

How to get value from adapter position, i have code in below:
CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;
try {
categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
categories, R.layout.per_item_category, from, to);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
If I look log it on logcat in the MyLog I get as:
Value is: {position=12,
image_path=http://192.168.103.121/xml/icon.png,
link=http://192.168.103.121/xml/category.php?kat_id=13, name=Category
13}
So my question, I want to get value from name and stored to variable String name, I want to get just "Category 13" in String name. Because I want to passing it to another activity.
Looks like you made ​​the object with hashmap, such blablabla.put("name", "value")?? If yes. Try this:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
Change to:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String name = (String) obj.get("name");
Log.d("Yourtag", name);
}
});
value = value.substring(value.indexOf("name=")+5,value.length()-1);
listID.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
Object obj=listID.getAdapter().getItem(position);
String str=obj.toString();
Toast.makeText(MainActivity.this, "Item is= "+str, Toast.LENGTH_LONG).show();
}
});
You can also get the adapter data via AdapterView<?> parent
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//setHasOptionsMenu(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Filedata mnotes = (Filedata) parent
.getItemAtPosition(position);
Toast.makeText(getActivity(), Filedata.getName(),Toast.LENGTH_SHORT).show();
});
}
This is what i did:-
lvHomePage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Transaction selectedFromList =(Transaction)parent.getAdapter().getItem(position);
Log.d("val:",selectedFromList.getDate());
Log.d("val:",selectedFromList.getMobile_no());
}
});
listID.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
HashMap<String, Object> sdsa = (HashMap<String, Object>) adapter.getItem(position);
Log.e("IndentNo", (String) sdsa.get("IndentNo"));
Log.e("IndentId", (String) sdsa.get("IndentId"));
}
});

Categories

Resources