【问题标题】:Alternative of List Within List?列表内列表的替代方案?
【发布时间】:2017-03-29 09:46:04
【问题描述】:

建议不要在任何地方使用列表中的列表,但我必须使用与列表中的列表相同的方法。有什么方法可以与列表中的列表相同。我看过可扩展列表视图this 但无法解决我的问题。我的要求看起来像

  **ListView**
         **TextView** 
         **TextView** 
         **ListView**(i have used Linear layout in here)

在我的布局中,我已将 Linear Layout 放置在内部 listView 的位置。Inner ListView(在我的情况下为线性布局)仅在 innerView 中设置一个 JSON 数据.我想在 Inner 中显示与外部 ListView 相同的内容。这怎么可能? fragment_fee

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/linear_receipt1_fee"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#008b8b"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/student_profile_fee_ReceiptIssuedDate"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text="Date"
                        android:textColor="#fff"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#FFFFFF" />

                    <TextView
                        android:id="@+id/student_profile_fee_ReceiptIssuedNumber"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text="Receipt number"
                        android:textColor="#fff"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/receipt1_fee"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#A4ACB0"
                        android:orientation="horizontal"
                        android:padding="5dp">

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:padding="5dp"
                            android:text="Description"
                            android:textSize="12dp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:padding="5dp"
                            android:text="Amount (Nrs) "
                            android:textSize="12dp"
                            android:textStyle="bold">


                        </TextView>

                    </LinearLayout>

////this is the replacement of ListView in Inner
                    <LinearLayout
                        android:id="@+id/student_profile_fee_linearlayout1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                       <TextView
                            android:id="@+id/student_profile_fee_description"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="left|center"
                            android:paddingLeft="70dp"
                            android:paddingTop="10dp"
                            android:text="Sports"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/student_profile_fee_amount"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="left|center"
                            android:paddingLeft="70dp"
                            android:paddingTop="10dp"
                            android:text="1000"
                            android:textSize="10dp" />


                    </LinearLayout>
  <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#C5CED1" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#DCE2E8"
                        android:orientation="horizontal"
                        android:padding="5dp">

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="70dp"
                            android:text="Total"
                            android:textSize="10dp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="70dp"
                            android:text="552"
                            android:textSize="10dp"
                            android:textStyle="bold" />

                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#C5CED1" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#DCE2E8"
                        android:orientation="horizontal"
                        android:padding="5dp">

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="70dp"
                            android:text="Status"
                            android:textSize="10dp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/student_profile_fee_status"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="70dp"
                            android:text="Paid"
                            android:textColor="#108B89"
                            android:textSize="10dp"
                            android:textStyle="bold" />

                    </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#C5CED1" />


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#DCE2E8"
                        android:orientation="horizontal"
                        android:padding="5dp">

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="70dp"
                            android:text="Received Date"
                            android:textSize="10dp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/student_profile_fee_FeeReceiptDate"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="70dp"
                            android:text="9 March,2017"
                            android:textSize="10dp"
                            android:textStyle="bold" />

                    </LinearLayout>
                </LinearLayout>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#fff" />


        </LinearLayout>
</LinearLayout>

student_fees_listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2E353D"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:padding="3dp"
                android:src="@mipmap/fee" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:text="Fees"
                android:textColor="#fff"
                android:textSize="17dp"
                android:textStyle="bold" />

        </LinearLayout>
    </LinearLayout>


    <ListView
        android:id="@+id/list_student_fees"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />


</LinearLayout>

CustomFeeListStudentAdapter

public class CustomFeeListStudentAdapter extends BaseAdapter {


    LinearLayout receipt1, receipt2, receipt3, receipt4;
    LinearLayout receipt1detail, receipt2detail, receipt3detail, receipt4detail;


    Context mContext;

    ArrayList<StudentFeeInformation> student_list;
    String TAG = "HomeTab_adapter";

    public CustomFeeListStudentAdapter(Context mContext, ArrayList<StudentFeeInformation> student_list) {
        super();
        this.mContext = mContext;
        this.student_list = student_list;
    }

    @Override
    public int getCount() {
        return student_list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int postion, View convertView, ViewGroup parent) {
        final Holder viewHolder;
        if (convertView == null) {
            // inflate the layout
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.fragment_fee, parent, false);

            // well set up the ViewHolder
            viewHolder = new Holder();
            viewHolder.student_profile_fee_status = (TextView) convertView.findViewById(R.id.student_profile_fee_status);
            viewHolder.student_profile_ReceiptIssuedDate = (TextView) convertView.findViewById(R.id.student_profile_fee_ReceiptIssuedDate);
            viewHolder.student_profile_ReceiptIssuedNumber = (TextView) convertView.findViewById(R.id.student_profile_fee_ReceiptIssuedNumber);
            viewHolder.student_profile_FeeReceivedDate = (TextView) convertView.findViewById(R.id.student_profile_fee_FeeReceiptDate);
            viewHolder.student_profile_Fee_Amount = (TextView) convertView.findViewById(R.id.student_profile_fee_amount);
            viewHolder.student_profile_Fee_Description = (TextView) convertView.findViewById(R.id.student_profile_fee_description);

        } else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (Holder) convertView.getTag();
        }

        viewHolder.student_profile_fee_status.setText(student_list.get(postion).getStatus());
        viewHolder.student_profile_ReceiptIssuedDate.setText(student_list.get(postion).getReceiptIssuedDate());
        viewHolder.student_profile_ReceiptIssuedNumber.setText(student_list.get(postion).getReceiptIssuedNumber());
        viewHolder.student_profile_FeeReceivedDate.setText(student_list.get(postion).getFeeReceivedDate());

        viewHolder.student_profile_Fee_Amount.setText(student_list.get(postion).getAmount());

        viewHolder.student_profile_Fee_Description.setText(student_list.get(postion).getFeeDescription());


        convertView.setTag(viewHolder);
        return convertView;
    }

    class Holder {
        TextView student_profile_fee_status;
        TextView student_profile_ReceiptIssuedDate;
        TextView student_profile_ReceiptIssuedNumber;
        TextView student_profile_FeeReceivedDate;
        TextView student_profile_Fee_Amount;
        TextView student_profile_Fee_Description;


    }
}

费用

public class Fee extends Fragment /*implements View.OnClickListener   */ {

    LinearLayout receipt1, receipt2, receipt3, receipt4;
    LinearLayout receipt1detail, receipt2detail, receipt3detail, receipt4detail;
    LinearLayout DescriptionAmount;


    TextView statustextView;
    ListView listViewfees, listviewfeedetail;
    List<StudentFeeInformation> yourData = new ArrayList<StudentFeeInformation>();

    public static final String Navigation_URL = "http://192.168.100.5:84/Api/financeApi/getAllFees";
    String Amount;
    String Descriptionlist, status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate;
    String master_id;

    LinearLayout linearLayout;

    //   View view1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.student_fees_listview, container, false);
        inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //    view1 = inflater.inflate(R.layout.student_fees_description_data, null);
        //    linearLayout = (LinearLayout) view1.findViewById(R.id.student_profile_fee_linearlayout1);

        // view1 = inflater.inflate(R.layout.student_fees_description_data, container, false);


        setHasOptionsMenu(true);
        receipt1 = (LinearLayout) view.findViewById(R.id.linear_receipt1_fee);

        //  receipt4 = (LinearLayout) view.findViewById(R.id.linear_receipt4_fee);

        //  receipt1detail = (LinearLayout) view.findViewById(R.id.receipt1_fee);
        //  receipt2detail = (LinearLayout) view.findViewById(R.id.receipt2_fee);
        //  receipt3detail = (LinearLayout) view.findViewById(R.id.receipt3_fee);
        //  receipt4detail = (LinearLayout) view.findViewById(R.id.receipt4_fee);

        //   receipt1.setOnClickListener(this);
        //   receipt2.setOnClickListener(this);
        //   receipt3.setOnClickListener(this);
        //   receipt4.setOnClickListener(this);


        //   receipt1detail.setVisibility(View.VISIBLE);
        //   receipt2detail.setVisibility(View.GONE);
        //   receipt3detail.setVisibility(View.GONE);
        //   receipt4detail.setVisibility(View.GONE);


        statustextView = (TextView) view.findViewById(R.id.student_profile_fee_status);
        SessionManagement sessionManagement = new SessionManagement(getContext());
        master_id = sessionManagement.getMasterId();
        listViewfees = (ListView) view.findViewById(R.id.list_student_fees);
        // listviewfeedetail = (ListView) view.findViewById(R.id.listtest);

        //  DescriptionAmount = (LinearLayout) view.findViewById(R.id.student_profile_fee_linearlayout1);
        // DescriptionAmount.addView(view);

        getUsersListData();


        return view;
    }


    private void getUsersListData() {


        String URL = Navigation_URL + "?id=" + master_id;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {


                            ArrayList<StudentFeeInformation> student_list = new ArrayList<>();
                            ArrayList<FeeDescriptionStudent> student_list_description = new ArrayList<>();

                            JSONArray jArray = new JSONArray(response);
                            //  studentFeeInformation = new StudentFeeInformation(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);

                                status = jsonObject.getString("Status");
                                DateofReceiptIssued = jsonObject.getString("DateOfReciept").substring(0, 10);
                                ReceiptNumber = jsonObject.getString("RecieptNo");
                                FeeReceivedDate = jsonObject.getString("recivedDate").substring(0, 10);


                                String Description = jsonObject.getString("Description");
                                JSONArray jArray1 = new JSONArray(Description);


                                for (int j = 0; j < jArray1.length(); j++) {


                                    JSONObject jsonObjectinner = jArray1.getJSONObject(j);
                                    Descriptionlist = jsonObjectinner.getString("des");
                                    Amount = jsonObjectinner.getString("Amount");


                                    //  TextView txtQuestion = (TextView) view1.findViewById(R.id.student_profile_fee_description);
                                    //  TextView tx = (TextView) view1.findViewById(R.id.student_profile_fee_amount);


                                    //  System.out.println(txtQuestion);
                                    //  System.out.println(tx);


                                    //  txtQuestion.setText(Descriptionlist);
                                    //  tx.setText(Amount);
                                    // linearLayout.addView(view1);


                                    Descriptionlist = jsonObjectinner.getString("des");
                                    Amount = jsonObjectinner.getString("Amount");


                                }

                                student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate, Descriptionlist, Amount));
                                //student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));

                            }

                            System.out.println("student_list size:" + student_list.size());
                            CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getActivity(), student_list);
                            listViewfees.setAdapter(customFeeListStudentAdapter);


                        } catch (JSONException e) {

                            System.out.println("This is not good");

                            e.printStackTrace();

                        }

                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show();

            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                return headers;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);


    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.dashboard, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                // do s.th.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

我看过很多 ExpandableListView 和 TableView 的例子 但无法将项目显示为 listView。我使用了线性布局 代替 ListView 但只显示一个 JSON 数据。在哪里 错误我无法得到它。

logcat

03-29 16:52:20.023 1558-1569/system_process I/WindowManager: Destroying surface Surface(name=com.example.user.mis/com.example.user.mis.activity.Login) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.AppWindowToken.notifyAppStopped:389 com.android.server.wm.WindowManagerService.notifyAppStopped:4456 com.android.server.am.ActivityStack.activityStoppedLocked:1252 com.android.server.am.ActivityManagerService.activityStopped:6895 
03-29 16:52:22.656 11984-11984/com.example.user.mis I/System.out: student_list size:3
03-29 16:52:22.656 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:22.656 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:22.672 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:22.672 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:22.676 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:0 getTeacherNameLate Charge
03-29 16:52:22.677 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:1 getTeacherNameMonthly Fee
03-29 16:52:22.678 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:2 getTeacherNameExtra Charge
03-29 16:52:22.678 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:22.679 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:0 getTeacherNameLate Charge
03-29 16:52:22.682 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:1 getTeacherNameMonthly Fee
03-29 16:52:22.684 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:2 getTeacherNameExtra Charge
03-29 16:52:23.039 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:23.039 11984-11984/com.example.user.mis I/System.out: 3
03-29 16:52:23.041 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:0 getTeacherNameLate Charge
03-29 16:52:23.042 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:1 getTeacherNameMonthly Fee
03-29 16:52:23.042 11984-11984/com.example.user.mis D/HomeTab_adapter: @@ postion:2 getTeacherNameExtra Charge
03-29 16:52:23.042 11984-11984/com.example.user.mis I/System.out: 3

【问题讨论】:

  • 你能不能再清楚一点你到底想要什么
  • 你能发布一些示例截图吗?你没有办法在listview中使用listview。
  • @GIBINTHOMAS 我会更新为什么我需要它
  • 据我了解尝试 HeaderListView
  • 所以您说每个数据都在使用相同的最后一项进行迭代,但数量与您的需要一样?

标签: android json listview


【解决方案1】:
  • 从那张截图看来,使用 listview 没有意义 在列表视图中。您可以使用Section header listview 或者只使用列表视图并为第一项设置背景颜色 适配器。

  • 在adapter中,根据条件,可以隐藏和显示子视图。

您必须在 for 循环中添加 student_list.add,如下所示:

     for (int j = 0; j < jArray1.length(); j++) {


      JSONObject jsonObjectinner = jArray1.getJSONObject(j);

      Descriptionlist = jsonObjectinner.getString("des");

      Amount = jsonObjectinner.getString("Amount"); 

      student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate, Descriptionlist, Amount));

     }

布局:

费用 -> 您应该在活动布局而不是适配器布局的列表视图顶部添加此费用标题。

在适配器中:

第一项:使用水平线性布局作为第一项并设置背景颜色为绿色。

第二项:使用水平线性布局作为第二项,并将背景颜色设置为银色或浅透明色。

第三项:使用水平线性布局作为第三项。

对于所有子项:第四、第五和第六项使用水平线性布局,并在其中放置两个文本视图。

【讨论】:

  • 这是我得到的截图。我想显示为描述金额 A 100 B 200 C 300 .....如列表中
  • 使用可展开的列表视图或者按照我在答案中提到的布局结构。
  • @user2025187 你能帮帮我吗。
  • @Ghimire 是的,你也可以这样做。这就是我在这个 answer.in 适配器中已经提到的,使用线性布局垂直作为父级。然后下面所有的线性布局都是水平的
【解决方案2】:

您可以按照Expandable Recycler view 中的说明使用可扩展的回收站视图

您需要做的就是为父母 1 和 6 定义孩子。对于其他职位,您可以简单地将他们视为没有孩子的父母。

您需要定义适当的模型类并在模型中传递数据。

【讨论】:

  • 如何使用 ExpandableList 视图?正如我在屏幕截图 2 中显示的那样。描述数据将像 ListView 一样列出。如果我将外部设置为 ExpandableListView,那么如何才能将所有列表项添加(即用于内部列表)
  • @Ghimire 所有内部物品似乎都是孩子。您可以使用可扩展列表视图实现您想要的。首先尝试使用可扩展列表视图运行示例并了解它们如何处理字符串数组。然后将 json 解析为该字符串数组。
  • @Stephen 我在想LinearLayout可以用rowView(代替内部列表)膨胀。如果是,那么我的问题就解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
相关资源
最近更新 更多