我从服务中获取数据,并从我拥有的这种格式的数据中删除
后。
我已经将数据保存在这样的数组中:
String data array(24150): 1,"product group","12345","Service Spares"
String data array(24150): 2,"product group","12345","No Longer In Use"
String data array(24150): 3,"product group","12345","Door Entry"
现在我想从该数组中提取字符串**产品组,如果我已经初始化的字符串等于产品组,然后将文本保存在最后一个,(逗号)之后的数组列表中,不带双引号。这是代码,但它返回给我“”我如何删除双引号,请指导我...**。
for(int i=0;i<spinnerDataArraylocal.length;i++)
{
Log.i("String data array",""+spinnerDataArraylocal[i]);
int index1 = spinnerDataArraylocal[i].indexOf(",");
String labelString = spinnerDataArraylocal[i].substring(index1+1,spinnerDataArraylocal[i].indexOf(",",index1+1));
Log.i("first comma position:",""+labelString);
if(labelString.equals("\"Premises Type\""))
{ int index = spinnerDataArraylocal[i].lastIndexOf(",");
Log.i("string compare","labelString" + labelString);
Log.i("extrACT STRING:" , spinnerDataArraylocal[i].substring(index+1));
String sample = spinnerDataArraylocal[i].substring(index+1);
Log.i("sample",""+sample);
premisesTypeArrayList.add(sample.toString().trim());
Log.i("premisesTypeArrayList",""+premisesTypeArrayList);
ArrayAdapter<String> adapterPremisesType = new ArrayAdapter<String>(
getActivity().getApplicationContext(),R.layout.custom_spinner,
premisesTypeArrayList);
adapterPremisesType.setDropDownViewResource(R.layout.custom_spinner_item_list);
spinnerPremisesType.setAdapter(adapterPremisesType);
spinnerPremisesType.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
adapterPremisesType.notifyDataSetChanged();
}
我需要将此代码重写为: if(labelString.equals("Premises Type"))
而不是这个
if(labelString.equals("\"Premises Type\""))
这是我的下拉数据的填充方式,应该没有双引号和空格
这样做 string =string.replaceAll("\"", "");
下面的分割代码。
String string = "41,\"Premises Type\",\"A\",\"Retail\"";
string =string.replaceAll("\"", "");
String[] array =string.split(",");
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
您的完整代码如下所示
for(int i=0;i<spinnerDataArraylocal.length;i++)
{
String spinData = spinnerDataArraylocal[i];
spinData = spinData.replaceAll("\"", "");
String spinData[] = spinData.split(",");
if(spinData[1].trim().equals("Premises Type"))
{
String sample = spinData[1]+","+spinData[2]+","+spinData[3];
premisesTypeArrayList.add(sample.toString().trim());
ArrayAdapter<String> adapterPremisesType = new ArrayAdapter<String>(
getActivity().getApplicationContext(),R.layout.custom_spinner,
premisesTypeArrayList);
adapterPremisesType.setDropDownViewResource(R.layout.custom_spinner_item_list);
spinnerPremisesType.setAdapter(adapterPremisesType);
spinnerPremisesType.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
adapterPremisesType.notifyDataSetChanged();
}
我是一名优秀的程序员,十分优秀!