skip to Main Content
public class MainActivity extends ActionBarActivity {

    ListView listview;
    String[] subjects = new String[] {"Android","PHP","Blogger","WordPress","SEO"};
    List<String> subject_list;
    ArrayAdapter<String> arrayadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView)findViewById(R.id.listView1);

        subject_list = new ArrayList<String>(Arrays.asList(subjects));

        arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subject_list);

        listview.setAdapter(arrayadapter);

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub

                subject_list.remove(position);

                arrayadapter.notifyDataSetChanged();

                Toast.makeText(MainActivity.this, "Item Deleted", Toast.LENGTH_LONG).show();

                return true;
            }

        });

    }
}

Here I had program of deleting item from list view by long click, instead of deleting item have to disable item by long clicking on the item in the ListView.

2

Answers


  1. Try This :

     subject_list.getChildAt(position).setEnabled(false);
    

    🙂 🙂

    Login or Signup to reply.
  2. Try code like below

    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view,
                                               int position, long id) {
                    // TODO Auto-generated method stub
    
                    //subject_list.remove(position);
    
                    if(!view.isEnabled()){
                        return false;
                    }
                    view.setEnabled(false);
                    view.setClickable(false);
    
                    arrayadapter.notifyDataSetChanged();
    
                    Toast.makeText(Main2Activity.this, "Item Deleted", Toast.LENGTH_LONG).show();
    
                    return true;
                }
    
            });
    

    As listview is replaced with RecyclerView, so i recommend you to use RecyclerView in future or with this.

    Happy Coding 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search