我想用PHP rest API开发一个MCQ应用程序。 如何通过单击“下一步”和“上一个”按钮在ListView中一个接一个地显示问题。
我的JSON数据如下,由PHP API从实时服务器中检索到
[{
"id": 1,
"title": "Apple MacBook ",
"shortdesc": "13.3 inch, Silver",
"rating": 4.7,
"price": 56990,
"image": "https:\/\/www.laptopmag.com\/images\/uploads\/4427\/g\/apple-macbook-air-13inch-w-g03.jpg",
"mid": 1
}, {
"id": 2,
"title": "Dell Inspiron",
"shortdesc": "14 inch, Gray",
"rating": 4.3,
"price": 60990,
"image": "https:\/\/www.laptopmag.com\/images\/uploads\/4442\/g\/dell-inspiron-15-7000-w-g02.jpg",
"mid": 1
}, {
"id": 3,
"title": "Microsoft Surface ",
"shortdesc": "12.3 inch, Silver",
"rating": 4.2,
"price": 54999,
"image": "https:\/\/images-na.ssl-images-amazon.com\/images\/I\/41JOpEMJsDL.jpg",
"mid": 1
}, {
"id": 5,
"title": "Computer Two",
"shortdesc": "This is second computer",
"rating": 5,
"price": 34000,
"image": "image 2",
"mid": 1
}]
我的Java方法,仅在第一次单击时调用。
private void getQuestions (final String subject_id) {
String tag_string_req = "req_login";
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_GET_ALL_PRODUCTS_API, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
JSONObject Obj = array.getJSONObject(i);
Questions questions = new Questions(Obj.getString("title"), Obj.getString("shortdesc"));
QuestionsList.add(questions);
}
ListViewAdapter adapter = new ListViewAdapter(QuestionsList,ShowSubjects.this);
//adapter.refreshQuestionsList(QuestionsList);
listView.setAdapter(adapter);
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
// hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("proID", subject_id);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
我的设计
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShowSubjects">
<EditText
android:id="@+id/editText"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.152"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="155dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Get Questions"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/productList"
android:layout_width="0dp"
android:layout_height="329dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="17dp"
android:layout_marginBottom="38dp"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="38dp"
android:text="Prev"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
这是我的ListViewAdaptor
public class ListViewAdapter extends ArrayAdapter<Questions> {
//the Questions list that will be displayed
private List<Questions> QuestionsList;
//the context object
private Context mCtx;
//here we are getting the Questionslist and context
//so while creating the object of this adapter class we need to give Questionslist and context
public ListViewAdapter(List<Questions> QuestionsList, Context mCtx) {
super(mCtx, R.layout.list_items, QuestionsList);
this.QuestionsList = QuestionsList;
this.mCtx = mCtx;
}
//this method will return the list item
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//getting the layoutinflater
LayoutInflater inflater = LayoutInflater.from(mCtx);
//creating a view with our xml layout
View listViewItem = inflater.inflate(R.layout.list_items, null, true);
//getting text views
TextView textViewName = listViewItem.findViewById(R.id.textViewName);
TextView textViewImageUrl = listViewItem.findViewById(R.id.textViewImageUrl);
//Getting the Questions for the specified position
Questions Quest = QuestionsList.get(position);
//setting Questions values to textviews
textViewName.setText(Quest.getQuestion());
textViewImageUrl.setText(Quest.getOption1());
//returning the listitem
return listViewItem;
}
public void refreshQuestionsList(List<Questions> QuestionsList) {
this.QuestionsList.clear();
this.QuestionsList.addAll(QuestionsList);
notifyDataSetChanged();
}
}
如何在下一个和上一个按钮上显示数据。
答案 0 :(得分:0)
我无法编写代码。我可以提出实施建议,因为我在处理相同类型的项目方面有一些经验。
ListActivity
和一个DetailActivity
DetailActivity
并显示当前
项目。您可以通过使用intent传递列表项的对象来开始DetailActivity
。
一种简单得多的方法是将列表设为静态,以便可以在所有活动中对其进行访问。在这种方法中,您只需要在启动明细活动时传递item(int)的索引值。
这种方法的优点
创建一种方法来显示当前问题。
displayQuestion(int index);
单击下一个按钮时,执行
displayQuestion(index++);
和以前的
dispalyQuestion(index--);
您可以查看此project。您可能会找到一些相关信息