Java Xml自定义ListView仅显示一个元素
我有一个xml布局,它将显示我从数据库中获取的元素。当我将所有元素放入ListView时,只会显示最后一个元素。我用Java Xml自定义ListView仅显示一个元素,java,android,xml,listview,Java,Android,Xml,Listview,我有一个xml布局,它将显示我从数据库中获取的元素。当我将所有元素放入ListView时,只会显示最后一个元素。我用System.out.println(bookList)测试了我的java类显示了我的所有数据。因此,我想问题应该出在我的xml文件上,但我无法找出导致问题的部分 activity\u e\u database.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln
System.out.println(bookList)测试了我的java类代码>显示了我的所有数据。因此,我想问题应该出在我的xml文件上,但我无法找出导致问题的部分
activity\u e\u database.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.apulibrary.E_Database" >
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#BDBDBD"
android:gravity="center"
android:text="APU Library"
android:textColor="#000000" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView01"
android:layout_below="@+id/TextView01"
android:layout_marginTop="2dp"
android:background="#BDBDBD"
android:gravity="center"
android:text="APU E-Database"
android:textColor="#000000" />
<EditText
android:id="@+id/searchinput"
android:layout_width="175dip"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:background="@drawable/searchbox"
android:ems="10"
android:textSize="15sp"
android:hint="Enter book title, author....." />
<Spinner
android:id="@+id/searchtype"
android:layout_width="265dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/searchinput"
android:layout_alignBottom="@+id/searchinput"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/searchinput"
tools:listitem="@android:layout/simple_spinner_item" />
<TextView
android:id="@+id/wronguserpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchinput"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:background="#FFEA39"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/searchbutton"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Search"
android:textSize="15sp"
android:layout_below="@+id/wronguserpassword"
android:layout_margin="10dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/searchresultview"
android:layout_below="@+id/searchbutton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="16sp"
android:layout_weight="1"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/textViewtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="16sp"
android:layout_weight="1"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/textViewid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textViewauthor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView"
android:layout_weight="1"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/textViewtitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
customiselistview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.apulibrary.E_Database" >
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#BDBDBD"
android:gravity="center"
android:text="APU Library"
android:textColor="#000000" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView01"
android:layout_below="@+id/TextView01"
android:layout_marginTop="2dp"
android:background="#BDBDBD"
android:gravity="center"
android:text="APU E-Database"
android:textColor="#000000" />
<EditText
android:id="@+id/searchinput"
android:layout_width="175dip"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:background="@drawable/searchbox"
android:ems="10"
android:textSize="15sp"
android:hint="Enter book title, author....." />
<Spinner
android:id="@+id/searchtype"
android:layout_width="265dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/searchinput"
android:layout_alignBottom="@+id/searchinput"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/searchinput"
tools:listitem="@android:layout/simple_spinner_item" />
<TextView
android:id="@+id/wronguserpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchinput"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:background="#FFEA39"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/searchbutton"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Search"
android:textSize="15sp"
android:layout_below="@+id/wronguserpassword"
android:layout_margin="10dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/searchresultview"
android:layout_below="@+id/searchbutton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="16sp"
android:layout_weight="1"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/textViewtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="16sp"
android:layout_weight="1"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/textViewid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textViewauthor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView"
android:layout_weight="1"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/textViewtitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
E_Database.java
package com.example.apulibrary;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
@SuppressLint("NewApi")
public class E_Database extends ActionBarActivity implements
View.OnClickListener{
private Button searchbutton;
private EditText searchinput;
private ListView searchresultview;
private ProgressDialog loading;
private String array_spinnersearchtype[];
public static String bookid = "";
public static String booktitle = "";
public static String author = "";
public static String isbn = "";
public static String publishers = "";
public static String publishedyear = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_e__database);
searchbutton = (Button) findViewById(R.id.searchbutton);
searchbutton.setOnClickListener(this);
searchresultview = (ListView) findViewById(R.id.searchresultview);
searchinput = (EditText) findViewById(R.id.searchinput);
array_spinnersearchtype = new String [7];
array_spinnersearchtype[0] = "Keyword";
array_spinnersearchtype[1] = "Title";
array_spinnersearchtype[2] = "Author";
array_spinnersearchtype[3] = "Subject";
array_spinnersearchtype[4] = "ISBN";
array_spinnersearchtype[5] = "Series";
array_spinnersearchtype[6] = "Call number";
Spinner searchtype = (Spinner)findViewById(R.id.searchtype);
ArrayAdapter adaptersearchtype = new ArrayAdapter(this,R.layout.spinner_layout, array_spinnersearchtype);
searchtype.setAdapter(adaptersearchtype);
}
public void getData(){
String id = searchinput.getText().toString().trim();
if (id.equals("")){
Toast.makeText(this, "Please enter an id",Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+searchinput.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(E_Database.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
for(int i = 0; i<result.length(); i++) {
JSONObject bookData = result.getJSONObject(i);
bookid = bookData.getString(Config.KEY_id);
booktitle = bookData.getString(Config.KEY_title);
author = bookData.getString(Config.KEY_author);
isbn = bookData.getString(Config.KEY_isbn);
publishers = bookData.getString(Config.KEY_publisher);
publishedyear = bookData.getString(Config.KEY_publishedyear);
ArrayList<HashMap<String, String>> bookList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "Book Id : " + bookid);
map.put("title", "Title : " + booktitle);
map.put("author", "Author : " + author);
bookList.add(map);
System.out.println(bookList);
SimpleAdapter simpleAdapter = new SimpleAdapter(this,bookList, R.layout.customiselistview, new String[]{"id", "title", "author"}, new int[]{R.id.textViewid, R.id.textViewtitle, R.id.textViewauthor});
searchresultview.setAdapter(simpleAdapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.e__database, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
getData();
}
}
package com.example.apulibrary;
导入android.annotation.SuppressLint;
导入android.app.ProgressDialog;
导入android.os.Bundle;
导入android.support.v7.app.ActionBarActivity;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.Spinner;
导入android.widget.Toast;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
@SuppressLint(“新API”)
公共类E_数据库扩展ActionBarActivity实现
View.OnClickListener{
私人按钮搜索按钮;
私人编辑文本搜索输入;
私有列表视图搜索结果视图;
私有对话框加载;
私有字符串数组_spinnersearchtype[];
公共静态字符串bookid=“”;
公共静态字符串booktitle=“”;
公共静态字符串author=“”;
公共静态字符串isbn=“”;
公共静态字符串发布者=”;
公共静态字符串publishedyear=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_e_____数据库);
searchbutton=(按钮)findViewById(R.id.searchbutton);
searchbutton.setOnClickListener(这个);
searchresultview=(ListView)findViewById(R.id.searchresultview);
searchinput=(EditText)findViewById(R.id.searchinput);
数组_spinnersearchtype=新字符串[7];
数组_spinnersearchtype[0]=“关键字”;
数组_spinnersearchtype[1]=“Title”;
数组_spinnersearchtype[2]=“作者”;
数组_spinnersearchtype[3]=“主题”;
数组_spinnersearchtype[4]=“ISBN”;
阵列_喷丝头SearchType[5]=“系列”;
数组_spinnersearchtype[6]=“呼叫号码”;
微调器searchtype=(微调器)findViewById(R.id.searchtype);
ArrayAdapter Adapter SearchType=新的ArrayAdapter(此,R.layout.spinner\u布局,array\u spinnersearchtype);
setAdapter(adaptersearchtype);
}
public void getData(){
字符串id=searchinput.getText().toString().trim();
if(id.equals(“”){
Toast.makeText(这是“请输入id”,Toast.LENGTH_LONG.show();
返回;
}
loading=ProgressDialog.show(这是“请稍候…”、“正在获取…”、false、false);
字符串url=Config.DATA_url+searchinput.getText().toString().trim();
StringRequest StringRequest=新的StringRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
loading.dispose();
showJSON(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(E_Database.this,error.getMessage().toString(),Toast.LENGTH_LONG.show();
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
私有void showJSON(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(Config.JSON_数组);
for(int i=0;iArrayList bookList在for循环之前实例化,并在for循环之后放置适配器
ArrayList<HashMap<String, String>> bookList = new ArrayList<HashMap<String, String>>();
for(int i = 0; i<result.length(); i++) {
JSONObject bookData = result.getJSONObject(i);
bookid = bookData.getString(Config.KEY_id);
booktitle = bookData.getString(Config.KEY_title);
author = bookData.getString(Config.KEY_author);
isbn = bookData.getString(Config.KEY_isbn);
publishers = bookData.getString(Config.KEY_publisher);
publishedyear = bookData.getString(Config.KEY_publishedyear);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "Book Id : " + bookid);
map.put("title", "Title : " + booktitle);
map.put("author", "Author : " + author);
bookList.add(map);
System.out.println(bookList);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this,bookList, R.layout.customiselistview, new String[]{"id", "title", "author"}, new int[]{R.id.textViewid, R.id.textViewtitle, R.id.textViewauthor});
searchresultview.setAdapter(simpleAdapter);
ArrayList bookList=new ArrayList();
对于(int i=0;i