Android 如何从json数组类中获取值
这是我的代码:Android 如何从json数组类中获取值,android,json,Android,Json,这是我的代码: for (int i = 0; i < response.length(); i++) { try { JSONObject obj = response.getJSONObject(i); Movie movie = new Movie(); movie.setTitle(obj.getString("name")); //get json array name }
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("name")); //get json array name
}
// adding movie to movies array
movieList.add(movie);
}
如何从jsonarray获取值并将其用于onclicklistener
有人建议使用bean类,但我不确定我是否理解如何在您的类中使用JsonArray
//create an ivar
private static Movie myMovie // <- this is the value you want.
// then make a method
public static Moive getMovie(){return MyJSONArrayClass.myMovie}
// modify your json Class to do this
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
myMovie = new Movie();
movie.setTitle(obj.getString("name")); //get json array name
}
// adding movie to movies array
movieList.add(movie);
}
// then in the class with the onclick listener do
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
//update json array name to sqlite
db.updateUser(XXXXXXXXXXXXXX); // this value i want get from json array movie.setTitle()
Intent i = new Intent(getApplicationContext(), MainActivity.class);
Movie movie = MyJSONArrayClass.getMovie();
if(null != movie){
// add your code here
}
startActivity(i);
}
});
//创建ivar
private static Movie myMovie//在您的第一个代码片段中,response
变量引用JSON数组,该数组显然是作为来自网络请求的响应接收的。如果希望适配器能够在代码的方法返回后继续使用该数据,则需要保留对原始数据的引用,或者需要制作数据的副本(或者在适配器的生命周期内将其存储在内存中,或者以某种方式将其持久化)
在第二个代码段中,您似乎只需要电影标题。碰巧在第一个片段中,您正在movieList
中存储Movie
对象的列表(每个对象都包含一个电影标题)。因此,如果第二个代码段可以使用movieList
,并且Movie
类提供了一种获取电影标题的方法(例如,一个getTitle()
方法),那么您可以在第二个代码段中执行类似操作来获取所单击项目的标题:
String movieTitle = movieList.get(position).getTitle();
注意:onItemClick()
的第三个参数是单击项目的位置。另外,我假设movieList
的类型类似于java.util.List
tq@whereisSQL。。。我的英语很差,jsonArray在哪里?它在同一个类中吗?不…它是不同的类..在字符串float中,你想得到什么值?这个:movie.setTitle(obj.getString(“name”);
String movieTitle = movieList.get(position).getTitle();