简体   繁体   English

如何将数据放入XML的ListView中?

[英]How to put data into ListView of XML?

I just started learning Android and I am trying to create a simple view where a portion of the main XML is a listview. 我刚开始学习Android,并且尝试创建一个简单视图,其中主要XML的一部分是列表视图。

Here is my main.xml 这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:id="@+id/Menu">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/menu_item_1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="129dp" />

    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_toEndOf="@+id/Menu"
        android:layout_toRightOf="@+id/Menu"
        android:layout_alignParentTop="true"
        android:id="@+id/Banner">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Account Log"
            android:layout_gravity="center"
            android:inputType="none"
            android:textStyle="bold"/>
    </FrameLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Log"
        android:layout_toRightOf="@+id/Menu"
        android:layout_below="@+id/Banner"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:choiceMode="none"
        android:clickable="false"
        android:contextClickable="true"
        android:fadeScrollbars="true" />

</RelativeLayout>

Assuming that I have a function called getDatafromFile() which returns an ArrayList of data, how do I put those data into my listView? 假定我有一个名为getDatafromFile()的函数,该函数返回数据的ArrayList,如何将这些数据放入listView中?

I've heard of using ArrayAdapter but the examples I've seen puts ListView to the entire layout. 我听说过使用ArrayAdapter,但是我看到的示例将ListView放入整个布局。

Full code of create list view 创建列表视图的完整代码

public class MainActivity extends AppCompatActivity {

ArrayList<String>arrayList=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); //here is the layout where your views exists

    for (int i=0;i<=20;i++){
        arrayList.add("List Items"+i);   //assume we passed the data in arraylist
    }
    ListView listView=(ListView)findViewById(R.id.log); //your listview

    ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);//the second argument is a layout file that have a textview

    listView.setAdapter(arrayAdapter);



}
}

The best way to put data in your ListView is by making an ArrayAdapter Class. 将数据放入ListView的最佳方法是制作ArrayAdapter类。 I always recommend you to try using a custom adapter class so that you can change it as per your requirement. 我总是建议您尝试使用自定义适配器类,以便可以根据需要进行更改。 You can create a class that extends ArrayAdapter. 您可以创建扩展ArrayAdapter的类。 Then pass the arraylist that you want inside your list view to that adapter constructor. 然后将想要在列表视图内的arraylist传递给该适配器构造函数。 For Example, below is a custom adapter class 例如,下面是一个自定义适配器类

public class CustomAdapter extends ArrayAdapter<DataInfo> {


ArrayList<DataInfo> itemList =new ArrayList<>();
Context context;
LayoutInflater inflater;
public CustomAdapte(Context context, ArrayList<VegInfo> list) {
    super(context, R.layout.list_item, list);
    this.context = context;
    itemList = list;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //getting an item from the list
    DataInfo item = itemList.get(position);
    if(convertView==null)
        convertView = inflater.inflate(R.layout.list_item,parent,false);

        //displaying some data from your data info in a textview
    TextView textView = (TextView) convertView.findViewById(R.id.tv);
    textView.setText(item.subitem);


    return convertView;
}
}

Inside your Activity, You can set the data to that adapter. 在活动中,您可以将数据设置为该适配器。

 CustomAdapter yourAdapter = new CustomAdapte(activity,yourArrayList);
 yourListView.setAdapter(yourAdapter);

Now, your list view have the data inside the array list. 现在,您的列表视图将数据包含在数组列表中。 Whenever there is a change in the array list, it can be reflected in the list view just by calling 只要数组列表发生更改,只需调用以下命令,即可将其反映在列表视图中

yourAdapter.notifydatasetchanged();

Here list_item is a custom XML file that you should create for a single list item view. 这里list_item是您应该为单个列表项视图创建的自定义XML文件。

Hope this helps you. 希望这对您有所帮助。 If not please tell me and let me make it more clear. 如果没有,请告诉我,让我更清楚。 happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM