0

In my program i have a custom listView with two text View . now i want to set choice mode and get those two textView's text . i wrote some code for this but it is not working . i indicate it in comment .

public class ThanaActivity extends Activity {


    //DatabaseOperation dbOperation = new DatabaseOperation(this);
    List <String> thanaName = new ArrayList<String> ();
    List <String> thanaMobileNumber = new ArrayList<String> ();
    private String clickedItemNameDistrict ;


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

    }

       // this method does not work when i click on list item
    public void onListItemClick (ListView parent, View v, int position, long id)    {
        String clickedItemNumberThana = thanaMobileNumber.get(position);

        Toast.makeText(this, "number is:"+clickedItemNumberThana, Toast.LENGTH_SHORT).show();
        TextView textV = (TextView)findViewById(R.id.textViewInstituateNumber);
        String hi= textV.getText().toString();
        System.out.println("clicked item number is:"+hi);
    }


    public void populateArrayListOfDhakaDistrict () {
        List <String> thanaInfoList = getAllThanaInfo();        
        ListView listView = (ListView)findViewById(R.id.listViewInstituate);
        listView.setChoiceMode(listView.CHOICE_MODE_SINGLE); //this line no work
        listView.setAdapter(new IconicAdapter());

        }

    public List <String> getAllThanaInfo() {
        dbOperation.open();

        Cursor c= dbOperation.getThanaInfo();
        c.moveToFirst();
        while (! c.isAfterLast()) {

            String thanaInfoName = c.getString(c.getColumnIndex("thana_name"));
            String thanaInfoMobile = c.getString(c.getColumnIndex("mobile_no"));
            String thanaInfoTelephone = c.getString(c.getColumnIndex("telephone_no"));
            //String teleInfo = new String (c.getColumnIndex(thanaInfo));
            thanaName.add(thanaInfoName);
            thanaMobileNumber.add(thanaInfoMobile);
            c.moveToNext();
        }
        c.close();
        return thanaName;

    }
//________________________________________________________________ adapter class starts 
    class IconicAdapter extends ArrayAdapter {
        public IconicAdapter() {
            super(ThanaActivity.this,R.layout.custom_listview,R.id.textViewInstituateName,thanaName);
        }

        @Override
        public View getView (int position, View v,ViewGroup parent){
            View row =super.getView(position, v, parent);
            TextView thanaNumber = (TextView)row.findViewById(R.id.textViewInstituateNumber);

             thanaNumber.setText(thanaMobileNumber.get(position));
             return (row);

        }
    }



}
3
  • whats s[position] and thanaMobileNumber? Commented Nov 28, 2013 at 10:19
  • thanaMobileNumber is a String type list .. in my emulator i can see this listView but i cannot get those text Commented Nov 28, 2013 at 10:24
  • actually i want , if someone click on listItem then it shows a toast with position and itemText . Commented Nov 28, 2013 at 10:26

2 Answers 2

1

Try this,

public class ThanaActivity extends Activity implements OnItemClickListener{


    //DatabaseOperation dbOperation = new DatabaseOperation(this);
    List <String> thanaName = new ArrayList<String> ();
    List <String> thanaMobileNumber = new ArrayList<String> ();
    private String clickedItemNameDistrict ;


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

    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        String clickedItemNumberThana = thanaMobileNumber.get(position);

        Toast.makeText(this, "number is:"+clickedItemNumberThana, Toast.LENGTH_SHORT).show();
        TextView textV = (TextView)findViewById(R.id.textViewInstituateNumber);
        String hi= textV.getText().toString();
        System.out.println("clicked item number is:"+hi);
    }
    /*   // this method does not work when i click on list item
    public void onListItemClick (ListView parent, View v, int position, long id)    {

    }*/


    public void populateArrayListOfDhakaDistrict () {
        List <String> thanaInfoList = getAllThanaInfo();        
        ListView listView = (ListView)findViewById(R.id.listViewInstituate);
        listView.setOnItemClickListener(this);
        listView.setChoiceMode(listView.CHOICE_MODE_SINGLE); //this line no work
        listView.setAdapter(new IconicAdapter());

        }

    public List <String> getAllThanaInfo() {
        dbOperation.open();

        Cursor c= dbOperation.getThanaInfo();
        c.moveToFirst();
        while (! c.isAfterLast()) {

            String thanaInfoName = c.getString(c.getColumnIndex("thana_name"));
            String thanaInfoMobile = c.getString(c.getColumnIndex("mobile_no"));
            String thanaInfoTelephone = c.getString(c.getColumnIndex("telephone_no"));
            //String teleInfo = new String (c.getColumnIndex(thanaInfo));
            thanaName.add(thanaInfoName);
            thanaMobileNumber.add(thanaInfoMobile);
            c.moveToNext();
        }
        c.close();
        return thanaName;

    }
//________________________________________________________________ adapter class starts 
    class IconicAdapter extends ArrayAdapter {
        public IconicAdapter() {
            super(ThanaActivity.this,R.layout.custom_listview,R.id.textViewInstituateName,thanaName);
        }

        @Override
        public View getView (int position, View v,ViewGroup parent){
            View row =super.getView(position, v, parent);
            TextView thanaNumber = (TextView)row.findViewById(R.id.textViewInstituateNumber);
             //thanaNumber.setText(s[position]);
             thanaNumber.setText(thanaMobileNumber.get(position));
             return (row);

        }
    }
}

1.You do not set listener for listview within populateArrayListOfDhakaDistrict () so added listView.setOnItemClickListener(this); 2.You just put onListItemClick method to handle click but onItemClick get called when you click on list

Sign up to request clarification or add additional context in comments.

3 Comments

just implement OnItemClickListener ?
String clickedItemNumberThana = thanaMobileNumber.get(position); (position)it is int arg2 ?
yeah, arg2 is position, i forget to modify that,
0

use

    TextView textV = (TextView)v.findViewById(R.id.textViewInstituateNumber);

for retrieving text from the TextView of selected row in ListView

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.