[:en]Android: ListView with OnItemClickListener[:]

[:en]makeup.java



// Step 1 of 3: implements OnItemClickListener and automatically create OnItemClick method
public class makeup extends AppCompatActivity implements AdapterView.OnItemClickListener {

        ListView listView;
        String[] arrayList;     

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


            listView = (ListView)findViewById(R.id.makeuplv);
            listView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,new ArrayList()));

            // Step 2 of 3. Add onItemClickListener
            listView.setOnItemClickListener(this);

           }


    // Step 3 of 3: add onItemclick method
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        // To display item position
        Toast.makeText(this, String.valueOf(position), Toast.LENGTH_LONG).show();

        // To display item value
        String item_of_list = arrayList[position];
        // Toast.makeText(this, item_of_list, Toast.LENGTH_LONG).show();
    }

    class MyTask extends AsyncTask {

    
            @Override
            protected void onPostExecute(String result) {
                if (result.isEmpty()) {
                    // Call AsyncTask
                    new MyTask().execute();
                } else {

                    dialog.dismiss();
                    //Toast.makeText(makeup.this, result, Toast.LENGTH_LONG).show();
                    arrayList = result.trim().split("#");
                    listView = (ListView) findViewById(R.id.makeuplv);
                    listView.setAdapter(new ArrayAdapter(makeup.this, android.R.layout.simple_list_item_1, arrayList));
                    // Keep updating any changes
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }



[:]

Print Friendly, PDF & Email
Scroll to Top