多个列表视图里面listfragment多个、视图、里面、列表

2023-09-06 18:59:27 作者:Vincent-征服

请裸陪我,因为我仍然在学习Android和Java的。

please bare with me as i am still learning android and java.

我一直在努力实现的列表片段中的两个列表视图,我说过在我的XML两个列表视图和试图建立两个独立的数据集在java文件,但针对第一lisview列表视图中的数据由第二覆盖。

i have been working on trying to implement two listviews inside a list fragment, i have stated two list views in my xml and tried to set two seperate sets of data in the java file but the list view data intended for the first lisview is overwritten by the second.

如何将我能够把它指向使用下面的code正确的列表视图?

how would i be able to point it to the correct listviews using the code below?

Java进行片段,其中列表应该去:

java for fragment where list should go:

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 for the fragment:

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

列表视图中也使用每个列表的自定义布局,如果需要的$ C $下,过多,请让我知道和虐待上传。

the list view also uses a custom layout for each list , if the code is needed for that too , please let me know and ill upload it.

推荐答案

您需要与相应地设置两个适配器每个的ListView ...

You need to set the two adapters accordingly with each ListView....

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

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

...但是你没有两个列表视图的发挥。在使用 ListFragment 每次调用 setAdapter 要设置一个时间的单的的ListView 给定的内容。您需要调整您的 ListFragement 是一个片段,有两个独立的列表视图中的片段中,然后将每个国家都有它自己的适配器,如上所述。

...however you do not have two ListViews in play. In using a ListFragment each time you call setAdapter you are setting a single ListView to the given contents. You would need to adjust your ListFragement to be that of a Fragment and have two separate ListViews within the Fragment, then set each with its own adapter as mentioned above.