其他分享
首页 > 其他分享> > android – listfragment中的多个listview

android – listfragment中的多个listview

作者:互联网

请与我裸露,因为我还在学习androidjava.

我一直在尝试在列表片段中实现两个列表视图,我已经在我的xml中声明了两个列表视图,并尝试在java文件中设置两个单独的数据集,但是用于第一个lisview的列表视图数据被覆盖第二.

如何使用下面的代码将其指向正确的列表视图?

列表应该去的片段的java:

package com.owais.shopsellswap;

import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class Fragment_My_Profile extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View myProfileView = inflater.inflate(R.layout.fragment_my_profile, container, false);


        return myProfileView;
    }

    // Store Arralist as hashmaps for the listview
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        ArrayList<HashMap<String,String>> list2 = new ArrayList<HashMap<String,String>>();
        // SimpleAdapter (listViewAdapter) links the array to the listview 
        private SimpleAdapter listViewAdapter;
        private SimpleAdapter listViewAdapter2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //HashMap links each line of data to the correct TextView
            HashMap<String,String> item;
            for(int i=0;i<userInfo.length;i++){
              item = new HashMap<String,String>();
              item.put( "line1", userInfo[i][0]);
              item.put( "line2", userInfo[i][1]);
              list.add( item );
            }

            HashMap<String,String> item2;
            for(int i=0;i<ListingsArray.length;i++){
              item2 = new HashMap<String,String>();
              item2.put( "line1", ListingsArray[i][0]);
              item2.put( "line2", ListingsArray[i][1]);
              item2.put( "line3", ListingsArray[i][2]);
              list2.add( item2 );
            }


            listViewAdapter = new SimpleAdapter(getActivity(), list,                    
                    R.layout.listview_layout_1,
                    new String[] { "line1","line2" },
                    new int[] {R.id.line_a, R.id.line_b});

            listViewAdapter2 = new SimpleAdapter(getActivity(), list2,
                    R.layout.listview_layout_3,
                    new String[] { "line1","line2", "line3" },
                    new int[] {R.id.line_a1, R.id.line_b1, R.id.line_c1});

            setListAdapter(listViewAdapter);
            setListAdapter(listViewAdapter2);

        }

        private String[][] userInfo =
            {{"User","Dummy"},
            {"Email Address","Dummy@dummymail.com"},
            {"User Type","Staff"},
            {"Contact Number","07111111111"}};

        private String[][] ListingsArray =
            {{"audi a3","brand new audi a3 with alloywheels, cd player", "£11000"},
            {"HTC One x","brand new android smartphone", "£450"},
            {"Acer Laptop","Acer Laptop with windows 7", "£300"},
            {"Sunglass","Oakley Sunglasses in great condition", "£100"}};
    }

片段的xml:

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/lightgrey"
        android:text="@string/userInfoHeader"
        android:textSize="15sp"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="184dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2" >
    </ListView>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/android:list"
        android:background="@color/lightgrey"
        android:text="@string/listingsHeader"
        android:textSize="15sp"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/android:list2"
        android:layout_width="fill_parent"
        android:layout_height="184dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >
    </ListView>

</RelativeLayout>

列表视图也为每个列表使用自定义布局,如果也需要代码,请告诉我并上传它.

解决方法:

您需要使用每个ListView相应地设置两个适配器….

ListView list = (ListView)findViewById(R.id.list);
ListView list2 = (ListView)findViewById(R.id.list2);

list.setAdapter(...)
list2.setAdapter(...)

…但是你没有两个ListViews在玩.每次调用setAdapter时都使用ListFragment,您将单个ListView设置为给定的内容.您需要将ListFragement调整为Fragment,并在Fragment中有两个单独的ListView,然后按照上面的说明设置每个ListView.

标签:android-listfragment,android,android-fragments,android-layout,android-listview
来源: https://codeday.me/bug/20191008/1873122.html