2011-07-08 9 views
1

私はJSON形式のAPIにいくつかのデータストーリーを持っています。JSONObjectからListView()へ

私はJSONからデータを取得し、ループしてTextViewに入れ、textViewにonClick()を追加することができました。

が、私はスクロールする電話での光のボタンを使用することができるようにしたいので、私は私が(ListViewコントロールを必要と言われて)

は、だから私は、配列の中に、私がしたいテキストを入れてに配列を追加しましたリストビュー

例を以下に示します しかし、どのアイテムにonClickを追加してID(intStoryID)?????????を渡すのですか?リストビューに

私のコード例

BufferedReader jsonBr = null; 
ArrayList arrStoryList = null; 

public void onCreate(Bundle savedInstancesState){ 
    super.onCreate(savedInstancesState); 
    setContentView(R.layout.stories_main); 

    if(setTopStoryData()){ 
      setStoriesArray(); 
      buildPage(); 
    }else{ 
     // TODO add Error Handling 
    } 

    } 

public boolean setTopStoryData(){ 
     String strURL = "http://www.example.com/api/index.php?my=params"; 

    JSONConn jsonConn = new JSONConn(); 
    JSONConn.setURL(strURL); 
    if(jsonConn.setData()){ 
     jsonBr = jsonConn.Br(); 
     return true; 
    }else{ 
      // my Error Handling 
      return false; 
    } 
} 


     public void setStoriesArray(){ 
    String line; 
    String strAddDate; 
    String strCurrentStatus; 
    String strStatusRead; 
    int intStoryID; 
    int intNumStories; 

    arrStoryList = new ArrayList(); 

    JSONObject arrStories; 
    JSONObject arrAllStories; 
    JSONObject arrSingleStory; 
    try{ 

     while((line = jsonBr.readLine()) != null){ 
      arrStories  = new JSONObject(line); 
      intNumStories = Integer.parseInt(arrStories.optString("NumStories")); 
      arrAllStories = arrStories.getJSONObject("StoryData"); 

      if(intNumStories > 0){ 
       for(int i = 0; i < intNumStories; i++){ 
        arrSingleStory = arrAllStories.getJSONObject("Story"+i); 

        intStoryID = Integer.parseInt(arrSingleStory.getString("ID")); 
        strAddDate = arrSingleStory.getString("ADDEDDATE"); 
        strCurrentStatus = arrSingleStory.getString("CURRENTSTATUS"); 

        if(strCurrentStatus.equals("y")){ 
         strStatusRead = "Online"; 
        }else if(strCurrentStatus.equals("n")){ 
         strStatusRead = "Offline"; 
        }else{ 
         strStatusRead = "Pending"; 
        } 

        String strStoryText = strAddDate+" - " +strCurrentStatus; 
        arrStoryList.add(strStoryText)); 


       } // end FOR loop 
      } 


     } //End while loop 


    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     strDEBUG += "Error (2)"; 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     strDEBUG += "Error (3) "+e.getLocalizedMessage()+"\n"; 
    } 
     } 

    public void buildPage(){ 
    if(this.arrStoryList != null){ 
     ListView lv1 = (ListView)findViewById(R.id.listView1); 

     lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,this.arrStoryList)); 
    } 
} 

答えて

0

使用onItemClickListener()は、各項目

listview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View view, 
       int position, long id) { 
      // do whatever you want here 
     } 
    }); 
+0

上のイベントをクリックし取得するしかし、私はどのように、彼らがクリックした項目のストーリーIDを渡したいです私はそれを配列に入れますか? – Jamie

+0

あなたはarraylistに格納している場合、storyIDを取得することができますpositionを渡すことによってアイテムの位置を取得することができます –

+0

基本的に私は2つの配列を格納する必要があります 私はListView() StoryID 次に、選択したListVIew配列のキーを一致させてStoryIDに接続します – Jamie

関連する問題