2013-11-28 51 views
0

在我的程序中,我有一個自定義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); 

     } 
    } 



} 
+0

whats'[position]'和'thanaMobileNumber'? –

+0

thanaMobileNumber是一個字符串類型列表..在我的模擬器,我可以看到這個listView,但我不能得到這些文字 –

+0

實際上我想要的,如果有人點擊listItem然後它顯示與位置和itemText吐司。 –

回答

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.You不設置監聽listviewpopulateArrayListOfDhakaDistrict()所以添加listView.setOnItemClickListener(this); 2.You只是把onListItemClick方法來處理單擊但onItemClick被調用當你點擊列表

+0

添加您已更改的信息。 –

+0

只是實現OnItemClickListener? –

+0

查看編輯帖子@silvia_aut – SathishKumar

0

使用

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

用於從ListView中選定行的TextView中檢索文本