Tanim reja
Tanim reja

Reputation: 2188

get text from custom listView

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

        }
    }



}

Upvotes: 0

Views: 584

Answers (2)

SathishKumar
SathishKumar

Reputation: 1644

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

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

use

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

for retrieving text from the TextView of selected row in ListView

Upvotes: 0

Related Questions