Android 从JSONObject到ListView()
基本上,我有一些JSON格式的API中的数据故事 我已经设法从JSON获取数据,通过循环,将它们放入TextView,在TextView上添加onClick() 但我希望能够使用手机上的光学按钮滚动,所以有人告诉我需要一个ListView() 因此,我将想要的文本放入一个数组,并将该数组添加到listView中 如下例所示 但是如何为每个项目添加onClick并传递ID(intStoryID) 我的代码示例Android 从JSONObject到ListView(),android,Android,基本上,我有一些JSON格式的API中的数据故事 我已经设法从JSON获取数据,通过循环,将它们放入TextView,在TextView上添加onClick() 但我希望能够使用手机上的光学按钮滚动,所以有人告诉我需要一个ListView() 因此,我将想要的文本放入一个数组,并将该数组添加到listView中 如下例所示 但是如何为每个项目添加onClick并传递ID(intStoryID) 我的代码示例 BufferedReader jsonBr = null; ArrayList ar
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));
}
}
BufferedReader jsonBr=null;
ArrayList arrStoryList=null;
创建时的公共void(Bundle savedinstancestate){
super.onCreate(savedinstancestate);
setContentView(R.layout.stories_main);
如果(setTopStoryData()){
setStoriesArray();
buildPage();
}否则{
//TODO添加错误处理
}
}
公共布尔值setTopStoryData(){
字符串strURL=”http://www.example.com/api/index.php?my=params";
JSONConn JSONConn=新的JSONConn();
JSONConn.setURL(strURL);
if(jsonConn.setData()){
jsonBr=jsonConn.Br();
返回true;
}否则{
//我的错误处理
返回false;
}
}
公共无效设置存储阵列(){
弦线;
串跨;
字符串strCurrentStatus;
字符串strStatusRead;
int intStoryID;
int intNumStories;
arrStoryList=新的ArrayList();
JSONObject-arrStories;
JSONObject arrAllStories;
JSONObject-arrSingleStory;
试一试{
而((line=jsonBr.readLine())!=null){
arrStories=新的JSONObject(行);
intNumStories=Integer.parseInt(arrstores.optString(“NumStories”);
arrAllStories=arrStories.getJSONObject(“故事数据”);
如果(内部存储器>0){
for(inti=0;i
使用列表视图中的onItemClickListener()
获取每个项目的单击事件
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// do whatever you want here
}
});
listview.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView arg0,视图,
内部位置,长id){
//在这里你想干什么就干什么
}
});
但我想传递他们单击的项目的故事ID,如何将其放入数组?您可以通过传递位置从arraylist获取项目的位置,如果您已存储在arraylist中,则可以获取故事ID。因此,基本上我需要存储两个数组,其中一个数组包含我希望在ListView()中显示的文本,另一个具有StoryID,然后匹配所选ListVIew数组中的键以连接到StoryID