如何在 Android Studio 中创建动态 ListView
Posted
技术标签:
【中文标题】如何在 Android Studio 中创建动态 ListView【英文标题】:How to create a dynamic ListView in AndroidStudio 【发布时间】:2021-01-14 15:08:59 【问题描述】:我一直在寻找有关 android/Java 方法的教程来创建具有动态条目的 ListView。我目前正在尝试将String
放入ListView 的每次迭代中。我需要有人告诉我我的课出了什么问题。代码本身一直运行到带有 ListViews 的页面,但它对填充 ListViews 没有任何作用。如果有人在 Android 应用程序编程方面说得很好,并且知道这个问题的答案,请在下面为答案做出贡献。
package com.taxiez.app;
import android.app.Activity;
import android.icu.util.Currency;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import io.grpc.Context;
import static com.taxiez.app.Items.getCurrencyLocaleMap;
import static com.taxiez.app.R.layout.activity_lists;
public class lists extends AppCompatActivity
ListView listViewP;
ListView listViewB;
ArrayList<String> mpTitle = getInfo( getFilesDir() + "/tax_personal.xml" );
ArrayList<String> mbTitle = getInfo( getFilesDir() + "/tax_business.xml" );
public lists() throws ParserConfigurationException, SAXException, ParseException, IOException
// so our images and other things are set in array
// now paste some images in drawable
public static ComponentActivity newComponentActivity()
return new androidx.activity.ComponentActivity( R.layout.content_search );
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.content_search);
listViewP = (ListView) findViewById(R.id.view_personal);
listViewB = (ListView) findViewById(R.id.view_business);
// now create an adapter class
MyAdapter adapterP = new MyAdapter(this, mpTitle);
listViewP.setAdapter(adapterP);
MyAdapter adapterB = new MyAdapter(this, mbTitle);
listViewB.setAdapter(adapterB);
// there is my mistake...
// now again check this..
//for (int i = 0; i < mpTitle.size() ; i++)
adapterP.addAll( mpTitle );
adapterB.addAll( mbTitle );
// now set item click on list view
class MyAdapter extends ArrayAdapter<String>
android.content.Context context;
ArrayList<String> rTitle;
MyAdapter (android.content.Context c, ArrayList<String> title)
super(c, R.layout.content_search, R.id.textView1, title);
this.context = c;
this.rTitle = title;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.listed_views, parent, false);
TextView myTitle = row.findViewById(R.id.textView1);
myTitle.setText(rTitle.get(position));
return row;
public ArrayList<String> getInfo ( String file ) throws ParserConfigurationException, ParseException, IOException, SAXException
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
ArrayList<String> biz = null;
DocumentBuilder db = dbf.newDocumentBuilder();
Reader reader = new FileReader( file );
InputSource sax = new InputSource();
sax.setCharacterStream( reader );
Document dom = db.parse( sax );
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getChildNodes();
if (nl != null)
int length = nl.getLength();
for (int i = 0; i < length; i++)
if (nl.item( i ).getNodeType() == Node.ELEMENT_NODE)
Element el = (Element) nl.item( i );
//Toast.makeText( this, el.getTagName(), Toast.LENGTH_SHORT ).show();
if (el.getNodeName().contains( "info" ))
String Date = el.getElementsByTagName( "Date" ).item( 0 ).getTextContent();
String Donate = el.getElementsByTagName( "Donate" ).item( 0 ).getTextContent();
String Seller = el.getElementsByTagName( "Seller" ).item( 0 ).getTextContent();
String StoreNo = el.getElementsByTagName( "Store" ).item( 0 ).getTextContent();
String RcptNo = el.getElementsByTagName( "ReceiptNo" ).item( 0 ).getTextContent();
String Item = el.getElementsByTagName( "Item" ).item( 0 ).getTextContent();
String ItemQuantity = el.getElementsByTagName( "ItemQuantity" ).item( 0 ).getTextContent();
String ItemPrice = el.getElementsByTagName( "ItemPrice" ).item( 0 ).getTextContent();
String description = el.getElementsByTagName( "Description" ).item( 0 ).getTextContent();
String taxAmount = el.getElementsByTagName( "TaxAmount" ).item( 0 ).getTextContent();
String totalAmount = el.getElementsByTagName( "TotalAmount" ).item( 0 ).getTextContent();
String income = el.getElementsByTagName( "Income" ).item( 0 ).getTextContent();
String method = el.getElementsByTagName( "Method" ).item( 0 ).getTextContent();
String now = el.getElementsByTagName( "Now" ).item( 0 ).getTextContent();
Items items = new Items(method, Seller, Item, description,ItemQuantity,ItemPrice,totalAmount,taxAmount,Date);
// CREATE STRINGS
String v = items.listString( items );
biz.add( 0, v );
//v.add( multi );
return biz;
【问题讨论】:
【参考方案1】:这是实现动态Listview的一种方式点击here!
【讨论】:
【参考方案2】:我认为这是问题所在:
ArrayList<String> biz = null;
您正在将 [biz.add( 0, v );] 添加到空引用!
将初始值改为new ArrayList();
ArrayList<String> biz = new ArrayList<>();
【讨论】:
听到这个消息很难过以上是关于如何在 Android Studio 中创建动态 ListView的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android Studio 1.4 中创建横向布局
如何在 Android Studio 中创建自定义图像按钮?
在 Android Studio 中创建一个包含多个对象的列表视图