在我的程序中,我有一個自定義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);
}
}
}
whats'[position]'和'thanaMobileNumber'? –
thanaMobileNumber是一個字符串類型列表..在我的模擬器,我可以看到這個listView,但我不能得到這些文字 –
實際上我想要的,如果有人點擊listItem然後它顯示與位置和itemText吐司。 –