Xml Custom ListView只显示一个元素

时间:2022-05-19 03:37:28

I have xml layout which will display the element that i get from database. When i put all the element to the ListView, only the last element will be shown. I had tested my java class with System.out.println(bookList); and all my data were shown. So I guess the problem should be on my xml file but I could not find out which part causing the problem.

我有xml布局,它将显示我从数据库中获取的元素。当我将所有元素放到ListView时,只显示最后一个元素。我用System.out.println(bookList)测试了我的java类;并显示了我的所有数据。所以我想问题应该在我的xml文件上,但我找不到导致问题的部分。

activity_e_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>

customiselistview.xml

<?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();
}
}

1 个解决方案

#1


1  

ArrayList bookList instantiate before for loop and adapter place after for loop. Do something like this.

ArrayList 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);

#1


1  

ArrayList bookList instantiate before for loop and adapter place after for loop. Do something like this.

ArrayList 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);