첫 번째 목록보기에서 항목을 클릭하면 첫 번째 목록보기에서 항목을 클릭하면 사용자가 첫 번째 목록에서 클릭 한 항목에 따라 새 항목 목록이로드됩니다.다른 목록 항목을 선택한 후 목록보기가 업데이트되지 않습니다.
기본적으로 두 번째 목록보기는 사용자가 첫 번째 목록에서 다른 항목을 선택하고 있더라도 매번 같은 목록을로드합니다. onItemClick isnt가 호출되거나 비슷한 것으로 보입니다.
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 = 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;
}
@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 : 여기
코드의adapter2 = null;
adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetchNewList());
listview2.setAdapter(adapter2);
rem 여분의 행을 추가하고 코드 들여 쓰기를 개선하면이 방법으로 읽기가 어렵습니다. – MByD