Listview not updating after selecting different list item
I have two listviews, clicking an item on the first list view loads the second listview, with a new list of items (depending on what the user clicked on in the first list).
Basically, the second list view keeps loading the same list everytime, even though the user is selecting different items on the first list. It seems like the onItemClick isnt getting called or something similar.
Here's the code:
listview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object item = listview.getItemAtPosition(position);
//Log.v(LOG_TAG,"selected item: " + item);
SavePreferences("item",item.toString());
fetchNewList();
flipper.showNext();
}});
listview2.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
flipper.showPrevious();
}});
@SuppressWarnings("null")
public ArrayList<String> fetch()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"JSON.php");
URLConnection tc = 开发者_如何学Ctwitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
//make sure youe String line is completely filled after that..
if (!line.equals(null) && !line.equals("") && line.startsWith("["))
{
JSONArray jArray = new JSONArray(line);
Log.v(LOG_TAG,"jarray value: " + jArray);
for (int i = 0;i < jArray.length(); i++)
{
//SONObject jobj = jArray.getJSONObject(i);
String country = jArray.getString(i);
listItems.add(country);
// also make sure you get the value from the jsonObject using some key
// like, jobj.getString("country");
//istItems.add(jobj.getString(""));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItems;
}
@SuppressWarnings("null")
public ArrayList<String> fetchNewList()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String selectedcounty = sharedPreferences.getString("item", "");
Log.v(LOG_TAG,"ITEM**:" + selecteditem);
URL twitter = new URL(
"JSON2.php?item=" + selecteditem);
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
//make sure youe String line is completely filled after that..
if (!line.equals(null) && !line.equals("") && line.startsWith("["))
{
JSONArray jArray = new JSONArray(line);
// Log.v(LOG_TAG,"jarray value: " + jArray);
for (int i = 0;i < jArray.length(); i++)
{
//SONObject jobj = jArray.getJSONObject(i);
String country = jArray.getString(i);
listItems.add(country);
// also make sure you get the value from the jsonObject using some key
// like, jobj.getString("country");
//istItems.add(jobj.getString(""));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItems;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
UPDATE: Got it working, I created a new method, called public void refreshNewList(), and put the following code in it:
adapter2 = null;
adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetchNewList());
listview2.setAdapter(adapter2);
listview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object item = listview.getItemAtPosition(position);
//Log.v(LOG_TAG,"selected item: " + item);
SavePreferences("item",item.toString());
listview2.setAdapter(new YourAdapter(context, resource, fetchNewList()));
flipper.showNext();
}});
EDIT: Try this
adapter2 = null;
adapter2 = new YourAdapter(paramters);
listview2.setdapter(adapter2);
精彩评论