how to add custom listview number of click to action bar in android

后端 未结 2 1962
无人共我
无人共我 2021-01-27 12:55

Please help me how to add custom List view\'s number of clicks to custom action bar (textview) in android?

Here is my Activity Class

public         


        
相关标签:
2条回答
  • 2021-01-27 13:38

    Ok so first of all declare a instance variable to keep the count of the clicks.

    public int listViewClickCounter;
    

    The on your listview onclick increment the count and call the change action bar method

        yourListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
    
                listViewClickCounter++ 
                changeActionBarTitle();
            }
        });
    

    Then implement the change action bar method. You said you have a custom view so you may need to change this a little

    public void changeActionBarTitle() {
    
    myActionBarCustomTitle.setText("number of clicks =" + String.valueOf(listViewClickCounter));
    
    0 讨论(0)
  • 2021-01-27 13:55

    Add a global int variable to your class, and in your ListView onItemClickListener increment this variable ++1 and get a reference to your TextView and use TextView.setText(String.ValueOf(that_int_variable));

    EDIT

    Looking at your code everything looks great, just some arrangements first of all let mTitleTextView be global TextView object on just in oncreate so you can reference it. You see any variable you declare in a method lives while the method is being called upon. or you can get your actionbar and use the findViewById() so your code should look like this

    String valueid,valueid1,valuename;
    public int count=0;
    JSONObject jsonobject;
    JSONArray jsonarray;
    public int listViewClickCounter=0;
    TextView mTitleTextView; // this is the edit. you add the textview
    ListAdapterAddItems adapter;
    String restaurantmenuname,rastaurantname;
    //then the rest will continue, i skipped all that
    

    and instead of setting the text right away in the TextView set it when an item has been clicked

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
    
                // Get Person "behind" the clicked item
                ListModel p =(ListModel)listview.getItemAtPosition(position);
                // Log the fields to check if we got the info we want
                Log.i("SomeTag", "Persons: " + p.getCount());
                Log.i("SomeTag", "Persons name: " + p.getProductName());
                Log.i("SomeTag", "Ruppees: " + p.getPrice());
    
                count++;
                String countString=String.valueOf(count);
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Item " + (position + 1),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
    
               Toast.makeText(getBaseContext(),
                        countString, Toast.LENGTH_LONG).show();
               mTitleTextView.setText(countString);
    
            }
        });
    

    That is all you need to do also you do not use this int variable listViewClickCounter

    0 讨论(0)
提交回复
热议问题