解析Android Studio ListView
伙计们,我只是想从一个解析查询创建一个对象的列表视图。所以我对android中的列表视图知之甚少,但有人能告诉我如何实现它吗?或者你有没有教程可以解释我应该做什么解析Android Studio ListView,android,android-listview,parse-platform,android-parsequeryadapter,Android,Android Listview,Parse Platform,Android Parsequeryadapter,伙计们,我只是想从一个解析查询创建一个对象的列表视图。所以我对android中的列表视图知之甚少,但有人能告诉我如何实现它吗?或者你有没有教程可以解释我应该做什么 多谢各位 ListView是显示可滚动项目列表的视图组。列表项使用适配器自动插入到列表中,适配器从源(如数组或数据库查询)提取内容,并将每个项结果转换为放置到列表中的视图 因此,使用listview元素在xml文件中创建listview 比如说 <ListView android:id="@android:id/list"
多谢各位 ListView是显示可滚动项目列表的视图组。列表项使用适配器自动插入到列表中,适配器从源(如数组或数据库查询)提取内容,并将每个项结果转换为放置到列表中的视图 因此,使用
listview
元素在xml文件中创建listview
比如说
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
这将把值数组中的所有项填充到列表中
有关更多示例和教程,我将向您展示我是如何做到这一点的。首先,我不会使用
query.findInBackground()
y将使用query.find()
返回所需对象的列表。要使用query.find()
您必须在要在其中创建listview的活动或片段中创建AsyncTask
类
/* the arraylist of RowItemChapter is becouse im making use of a
* custom adapter for my listview
*/
ArrayList<RowItemChapter> grupos;
ParseObject course;
// this list is the one whe are going to use to save what the query gives back
List<ParseObject> ob;
ProgressDialog mProgressDialog;
// this would be my custo adater
ChaptersAdapter adapter;
// this is my expandableListView
ExpandableListView listView;
// RemoteDataTask AsyncTask
class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("Looking for Chapters");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
grupos = new ArrayList<RowItemChapter>();
try {
// Locate the class table named "Chapters"
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Chapters");
// look only for objects witch column is equal to the ParseObject course.
query.whereEqualTo("Curso", course);
// get data in an ascending list
query.orderByAscending("position");
// execute the query and get back a list of the data you where looking for
ob = query.find();
// make a for inside the list and set the data to the adapter
for (ParseObject chapter : ob) {
// Locate images in flag column
RowItemChapter item = new RowItemChapter((String) chapter.get("name"), new SubrowItemChapter(10 , 5, 80), 80);
grupos.add(item);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in the XML
listView = (ExpandableListView) getActivity().findViewById(R.id.listaChapters);
// Pass the results into ListViewAdapter.java
adapter = new ChaptersAdapter(getActivity(), grupos);
// Binds the Adapter to the ListView
listView.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
您可能不需要知道我所做的是从我所做的项目中提取的,如果您不使用任何自定义适配器,那就更容易了。希望对您有所帮助。在谷歌上搜索并从教程中学习您可以从这里开始developer.android.com您可以提供本教程的链接或在github中共享项目链接吗?
/* the arraylist of RowItemChapter is becouse im making use of a
* custom adapter for my listview
*/
ArrayList<RowItemChapter> grupos;
ParseObject course;
// this list is the one whe are going to use to save what the query gives back
List<ParseObject> ob;
ProgressDialog mProgressDialog;
// this would be my custo adater
ChaptersAdapter adapter;
// this is my expandableListView
ExpandableListView listView;
// RemoteDataTask AsyncTask
class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("Looking for Chapters");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
grupos = new ArrayList<RowItemChapter>();
try {
// Locate the class table named "Chapters"
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Chapters");
// look only for objects witch column is equal to the ParseObject course.
query.whereEqualTo("Curso", course);
// get data in an ascending list
query.orderByAscending("position");
// execute the query and get back a list of the data you where looking for
ob = query.find();
// make a for inside the list and set the data to the adapter
for (ParseObject chapter : ob) {
// Locate images in flag column
RowItemChapter item = new RowItemChapter((String) chapter.get("name"), new SubrowItemChapter(10 , 5, 80), 80);
grupos.add(item);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in the XML
listView = (ExpandableListView) getActivity().findViewById(R.id.listaChapters);
// Pass the results into ListViewAdapter.java
adapter = new ChaptersAdapter(getActivity(), grupos);
// Binds the Adapter to the ListView
listView.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new RemoteDataTask().execute();
}