在我的应用程序中,我有一个带有一系列地点的旋转器(例如机场,商场......)。当我从旋转器点击机场地点时,它应该在列表视图中显示旋转器下面的机场列表(撒哈拉,juhu)我试过这个,但它混合了机场和商场的名单。 在这个错误.Plz帮助我.Below是我使用的json: -
{
"PlacesList": {
"PlaceType": [
{
"-Name": "Airport",
"Places": {
"Place": [
{
"name": "Juhu Aerodrome",
"latitude": "19.09778",
"longitude": "72.83083",
"description": "Juhu Aerodrome is an airport that serves the metropolitan"
},
{
"name": "Chhatrapati Shivaji International Airport",
"latitude": "19.09353",
"longitude": "72.85489",
"description": "Chhatrapati Shivaji International Airport "
}
]
}
},
{
"-Name": "Mall",
"Places": {
"Place": [
{
"name": "Infinity",
"latitude": "19.14030",
"longitude": "72.83180",
"description": "This Mall is one of the best places for all types of brand"
},
{
"name": "Heera Panna",
"latitude": "18.98283",
"longitude": "72.80897",
"description": "The Heera Panna Shopping Center is one of the most popular"
}
]
}
}
]
}
}
///////////////////////////////////
public class PlaceDemo extends Activity
{
private String jString;
private Spinner spinner;
private ListView lister2;
private ByteArrayInputStream json;
private ArrayList<HashMap<String, String>> mylist;
private PlaceType pttype;
private List<PlaceType> results;
private Place place;
private List<Place> places;
private ArrayList<HashMap<String, String>> placelist;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
InputStream is = getAssets().open("place_file.json");
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try
{
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1)
{
writer.write(buffer, 0, n);
}
}
finally
{
is.close();
}
jString = writer.toString();
System.out.println(jString);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
spinner = (Spinner) findViewById(R.id.spinner);
lister2 = (ListView) findViewById(R.id.ListView2);
try
{
json = new ByteArrayInputStream(jString.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e1)
{
e1.printStackTrace();
}
try
{
Gson gson = new Gson();
Reader reader = new InputStreamReader(json);
Places response = gson.fromJson(reader,Places.class);
results = response.plist;
}
catch (Exception e)
{
e.printStackTrace();
}
mylist = new ArrayList<HashMap<String, String>>();
placelist = new ArrayList<HashMap<String, String>>();
try
{
for (int i = 0; i < results.size(); i++)
{
pttype = results.get(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name",pttype.name);
mylist.add(map);
}
for (int i = 0; i < results.size(); i++)
{
pttype = results.get(i);
HashMap<String, String> map1 = new HashMap<String, String>();
places = pttype.place;
for (int j = 0; j < places.size(); j++)
{
place =places.get(j);
map1.put("desc",place.desc);
map1.put("lat",place.lat);
map1.put("lng",place.lng);
map1.put("name1",place.name);
placelist.add(map1);
}
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
SpinnerAdapter adapter = new SpinnerAdapter(this, mylist , R.layout.row,
new String[] { "name" },
new int[] { android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
TextView textView = (TextView) view;
textView.setText(textRepresentation);
return true;
}
};
adapter.setViewBinder(viewBinder);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3)
{
String[] lister = null;
switch (position)
{
case 0:
String[] airport = new String[] { "name1" };
lister = airport ;
break;
case 1:
String[] mall = new String[] { "name1" };
lister = mall ;
break;
}
lister2.setAdapter(new PlaceAdapter(PlaceDemo.this, placelist , R.layout.row,
lister,
new int[] { R.id.tt1}));
lister2.setVisibility (View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public class PlaceAdapter extends SimpleAdapter
{
private ArrayList<HashMap<String, String>> results;
public PlaceAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent)
{
View v = super.getView(position, view, parent);
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView id = (TextView) v.findViewById(R.id.tt1);
id.setText(results.get(position).get("name1"));
return v;
}
}
public class SpinnerAdapter extends SimpleAdapter
{
private ArrayList<HashMap<String, String>> results;
public SpinnerAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent)
{
View v = super.getView(position, view, parent);
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView id = (TextView) v.findViewById(R.id.tt1);
id.setText(results.get(position).get("name"));
return v;
}
}
}
答案 0 :(得分:0)
似乎myList包含值 - 机场,购物中心等.Placelist包含所有地方,包括机场和商场。您在以下代码中提供相同的地点列表 -
lister2.setAdapter(new PlaceAdapter(PlaceDemo.this, placelist , R.layout.row,
lister, new int[] { R.id.tt1}));
lister2.setVisibility (View.VISIBLE);
打印地方列表并确认它是否包含两组地点。如果是这样,您需要为机场和购物中心创建单独的列表。