ListView里面的ListView项

时间:2018-01-04 17:55:17

标签: android android-layout listview android-adapter listview-adapter

我想在列表视图项中使用列表视图。代码如下所示。

MainActivity.java

String[][] topicLists = {{"one,one","one,two","one,three","one,four","one,five"}
                            ,{"two,one","two,two","two,three","two,four"}
                            ,{"three,one","three,two","three,three","three,four","three,five","three,six"}
                            ,{"four,one","four,two","four,three"}};

    ListView listView = (ListView) findViewById(R.id.recyclerView);
    CardsAdapter cardsAdapter = new CardsAdapter(this, topicLists);
    listView.setAdapter(cardsAdapter);

CardsAdapter.java

String[][] allTopics = {};

public CardsAdapter(@NonNull Context context, String[][] unitsAndTopics) {
    super(context, 0,unitsAndTopics);
    allTopics = unitsAndTopics;
}

@Nullable
@Override
public Object getItem(int position) {
    ArrayList<String> all = new ArrayList<String>();
    for (int i = 0; i <allTopics[position].length ; i++) {
        all.add(allTopics[position][i]);
        Log.e("",allTopics[position][i]);
    }

    return all;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View listItem = convertView;
    if (listItem==null)
        listItem = LayoutInflater.from(getContext()).inflate(R.layout.card_view,parent,false);

    ArrayList<String> currentItem = (ArrayList<String>) getItem(position);

    ListView topic = (ListView) listItem.findViewById(R.id.listView);
    ListAdapter listAdapter = new ListAdapter(getContext(),currentItem);
    topic.setAdapter(listAdapter);

    return listItem;
}

ListAdapter.class

    public ListAdapter(@NonNull Context context, ArrayList<String> topics) {
    super(context, 0,topics);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View listItem = convertView;
    if (listItem==null)
        listItem = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);

    final String currentItem = (String) getItem(position);

    TextView topic = (TextView) listItem.findViewById(R.id.topic_text_view);
    topic.setText(currentItem);

    Button search = (Button) listItem.findViewById(R.id.search_button);
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                String term = currentItem;
                intent.putExtra(SearchManager.QUERY, term);
                getContext().startActivity(intent);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    });

    return listItem;
}

The list live have only first element

在上图中,所有数组中唯一的第一个元素出现在列表中。休息都没有出现 任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

您的代码正常工作,请点击此处

enter image description here

我制作第一个listview项目xml文件高度修复150dp(card_view),其正常工作是所有代码

1)card_view:

<?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="200dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

2)ListViewAdapter:

import android.app.SearchManager;
    import android.content.Context;
    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.TextView;

    import java.util.ArrayList;


    public class ListNewAdapter extends BaseAdapter{

        private static final String TAG = "ListNewAdapter";
        private final ArrayList<String> topics;
        private final Context context;

        public ListNewAdapter(@NonNull Context context, ArrayList<String> topics) {
           this.context = context;
           this.topics = topics;
        }

        @Override
        public int getCount() {
            Log.d(TAG, "getCount:"+topics.size());
            return topics.size();
        }

        @Override
        public Object getItem(int i) {
            return topics.get(i);
        }

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

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            View listItem = convertView;
            if (listItem==null)
                listItem = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

            final String currentItem = (String) getItem(position);

            TextView topic = (TextView) listItem.findViewById(R.id.topic_text_view);
            topic.setText(currentItem);


            return listItem;
        }
    }

3)CardAdapter:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;


public class CardsAdapter extends BaseAdapter {

    private static final String TAG = "CardsAdapter";
    private final Context context;
    String[][] allTopics = {};

    public CardsAdapter(@NonNull Context context, String[][] unitsAndTopics) {
        this.context = context;
        allTopics = unitsAndTopics;
    }

    @Override
    public int getCount() {
        return allTopics.length;
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        ArrayList<String> all = new ArrayList<String>();
        for (int i = 0; i <allTopics[position].length ; i++) {
            all.add(allTopics[position][i]);
            Log.d(TAG, "getItem:"+all);
        }

        return all;
    }

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View listItem = convertView;
        if (listItem==null)
            listItem = LayoutInflater.from(context).inflate(R.layout.card_view,parent,false);

        ArrayList<String> currentItem = (ArrayList<String>) getItem(position);

        ListView topic = (ListView) listItem.findViewById(R.id.listView);
        ListNewAdapter listNewAdapter = new ListNewAdapter(context,currentItem);
        topic.setAdapter(listNewAdapter);

        return listItem;
    }
}