从自定义listView获取文本

时间:2013-11-28 10:15:37

标签: android listview

在我的程序中,我有一个带有两个文本View的自定义listView。现在我想设置选择模式并获取这两个textView的文本。我为此写了一些代码,但它没有用。我在评论中表明了这一点。

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

        }
    }



}

2 个答案:

答案 0 :(得分:1)

试试这个,

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.您没有在listview内设置populateArrayListOfDhakaDistrict ()的监听器,因此添加了listView.setOnItemClickListener(this); 2.您只需使用onListItemClick方法处理点击,但点击列表时会调用onItemClick

答案 1 :(得分:0)

使用

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

用于从ListView中所选行的TextView中检索文本