Java 将值从SimpleAdapter提取到数组[]
是否可以将值从Java 将值从SimpleAdapter提取到数组[],java,android,Java,Android,是否可以将值从simpledapter传输到array[]。我正在从ArrayList获取SimpleAdapter中的值,并希望将这些值从SimpleAdapter传递到array[] static ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); SimpleAdapter adapter = new Simpl
simpledapter
传输到array[]
。我正在从ArrayList
获取SimpleAdapter
中的值,并希望将这些值从SimpleAdapter
传递到array[]
static ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter = new SimpleAdapter(this, contactList,R.layout.list_item,new String[] {android_H_NAME}, new int[] {R.id.name});
String[] hospFields = //want to get values here from adapter
static ArrayList contactList=new ArrayList();
simpledapter adapter=newsimpledapter(this,contactList,R.layout.list_项,新字符串[]{android\u H_NAME},新int[]{R.id.NAME});
字符串[]hospFields=//要从适配器获取值吗
无法直接访问数据,但您可以使用(一般情况下):
Object[]o=新对象[adapter.getCount()];
对于(inti=0;i
创建适配器数据的副本。如果只想提取特定值,请使用:
String[] hospFields = new String[adapter.getCount()];
for (int i = 0; i < adapter.getCount(); i++) {
HashMap<String, String> rowData = adapter.getItem(i); //gets HashMap of a specific row
String name = rowData.get("name"); //gets the field name of that row
hospFields[i] = name; // copies field into your array
}
String[]hospFields=新字符串[adapter.getCount()];
对于(inti=0;i
修改并使用以下内容:
ArrayList<String> listItems = new ArrayList<String>();
for (int i = 0; i < result.length(); i++) {
try {
listItems
.add(result.getJSONObject(i)
.getString(BsharpConstant.NAME_CONSTANT)
.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
ArrayList listItems=new ArrayList();
for(int i=0;i
内部oncreate方法:
ListView list = (ListView) findViewById(R.id.idOfUrView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
ActivityName.this,
android.R.layout.simple_list_item_1, listItems);
list.setAdapter(adapter);
list.setOnItemClickListener(this);//if u need this until not required
ListView列表=(ListView)findViewById(R.id.idOfUrView);
ArrayAdapter适配器=新的ArrayAdapter(
ActivityName.this,
android.R.layout.simple_list_item_1,listItems);
list.setAdapter(适配器);
list.setOnItemClickListener(此)//如果你需要这个直到不需要
使用此选项可将值存储在项目中单击:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String clickedItemName = (String) listOfProductsCategory
.getItemAtPosition(position);
int clickedItemId = (int) listOfProductsCategory
.getItemIdAtPosition(position);
// implement your login to store the value
//by using for loop listItems[i] ,i++
Toast.makeText(this.getActivity(),
"Custom text : " + clickedItemName,
Toast.LENGTH_SHORT).show();
//call your method if u want
@覆盖
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
String clickedItemName=(String)产品目录列表
.getItemAtPosition(位置);
int clickedItemId=(int)产品目录列表
.GetItemIDataPosition(位置);
//实现登录以存储值
//通过使用for循环列表项[i],i++
Toast.makeText(this.getActivity(),
“自定义文本:”+单击编辑项名称,
吐司。长度(短)。show();
//如果需要,请调用您的方法
}有什么特别的原因可以避免contactList的toArray方法吗?你应该定义“值”,我试过了,但不知道它会出错。对于(int x=0;x)您也想实现setOnItemClickListener吗?是的,我想实现setOnItemClickListener然后如何从o传输到String[]hospFieldsfor(i=0;icheck编辑的答案..如果这对您不起作用,请尝试(String)adapter.getItem(i)而不是adapter.getItem(i).toString()感谢每一位花时间帮助我解决问题的人如果你的问题得到解决,那么请接受这一回答。这将帮助其他可能面临同样问题的人
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String clickedItemName = (String) listOfProductsCategory
.getItemAtPosition(position);
int clickedItemId = (int) listOfProductsCategory
.getItemIdAtPosition(position);
// implement your login to store the value
//by using for loop listItems[i] ,i++
Toast.makeText(this.getActivity(),
"Custom text : " + clickedItemName,
Toast.LENGTH_SHORT).show();
//call your method if u want