【问题标题】:subitem of a listview not showing in arrayadapter列表视图的子项未显示在 arrayadapter 中
【发布时间】:2018-02-14 17:30:38
【问题描述】:

我正在寻找每个项目看起来像的列表/网格视图:

|-----|   |--------------------------|
| H1  |   |   Short Desc1            |
|-----|   |--------------------------|    //Preferred View
|------------------------------------|
|    Long Description                |
|------------------------------------|

或喜欢:

|------| |------------------------------|
|      | |   Short Desc1                |
|  H1  | |------------------------------|
|      |                                  //Optional View
|      | |------------------------------|
|------| |______Long Description________|

首先,我从 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"
    android:background="@color/colorPrimaryLight">


    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:scrollbars="vertical">

    </ListView>

</RelativeLayout>

在 android studio 的设计视图中,显示可接受的输出为: 但是,使用这个 java 代码:

public class FourthFragment extends ListFragment {

    String names[] ={"A", "B", "C", "D"};

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View myFragmentView = inflater.inflate(R.layout.fragment_fourth, container, false);
//        TextView tv = (TextView) myFragmentView.findViewById(R.id.textView1);
//        tv.setText("My Header");

        setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names));
        return myFragmentView;
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // on click display the item in toast
        Toast.makeText(getActivity(), (String)l.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
    }
    static FourthFragment newInstance(int num) {
        FourthFragment fourthFragment = new FourthFragment();

        Bundle args = new Bundle();
        args.putInt("num", num);
        fourthFragment.setArguments(args);

        return fourthFragment;
    }

}

它只显示菜单,没有任何子菜单,如:

首选第一张图片,如何获得输出? 或者,简单地说,我的代码中缺少什么?

我从pagerAdapter 调用此函数为:

        case 1:
            return FourthFragment.newInstance(4);

【问题讨论】:

    标签: android android-fragments android-arrayadapter


    【解决方案1】:

    您需要通过扩展 BaseAdapter 来创建自己的适配器。您使用的是 ArrayAdapter,布局是这里提到的 android 的预定义布局
    setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(),android.R.layout.simple_list_item_1,names));

    举个例子

    所以你的那个项目的 XML 布局如下

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector"
        android:orientation="horizontal"
        android:padding="5dip" >
    
        <!--  ListRow Left sied Thumbnail image -->
        <LinearLayout android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip"
            android:layout_alignParentLeft="true"
            android:background="@drawable/image_bg"
            android:layout_marginRight="5dip">
    
            <ImageView
                android:id="@+id/list_image"
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:src="@drawable/rihanna"/>
    
        </LinearLayout>
    
        <!-- Title Of Song-->
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Rihanna Love the way lie"
            android:textColor="#040404"
            android:typeface="sans"
            android:textSize="15dip"
            android:textStyle="bold"/>
    
        <!-- Artist Name -->
        <TextView
            android:id="@+id/artist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:textColor="#343434"
            android:textSize="10dip"
            android:layout_marginTop="1dip"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Just gona stand there and ..." />
    
        <!-- Rightend Duration -->
        <TextView
            android:id="@+id/duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@id/title"
            android:gravity="right"
            android:text="5:45"
            android:layout_marginRight="5dip"
            android:textSize="10dip"
            android:textColor="#10bcc9"
            android:textStyle="bold"/>
    
         <!-- Rightend Arrow -->
         <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    
        </RelativeLayout>
    

    到目前为止,我们已经完成了列表视图的部分设计。创建一个新的类适配器

    public class LazyAdapter extends BaseAdapter {
    
    private Activity activity;
    private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public LazyAdapter(Activity a, ArrayList&lt;HashMap&lt;String, String&gt;&gt; d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }
    
    public int getCount() {
        return data.size();
    }
    
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);
    
        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
    
        HashMap&lt;String, String&gt; song = new HashMap&lt;String, String&gt;();
        song = data.get(position);
    
        // Setting all values in listview
        title.setText(song.get(CustomizedListView.KEY_TITLE));
        artist.setText(song.get(CustomizedListView.KEY_ARTIST));
        duration.setText(song.get(CustomizedListView.KEY_DURATION));
        imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
        return vi;
    }
    }
    

    在您的活动中:

    // All static variables
    static final String URL = "https://api.androidhive.info/music/music.xml";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    
    ListView list;
    LazyAdapter adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
    
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element
    
        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes &lt;song&gt;
        for (int I = 0; I &lt; nl.getLength(); i++) {
            // creating new HashMap
            HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key =&gt; value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
    
            // adding HashList to ArrayList
            songsList.add(map);
        }
    
        list=(ListView)findViewById(R.id.list);
    
        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);
        list.setAdapter(adapter);
    
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                    int position, long id) {
    
            }
        });
    }}
    

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      相关资源
      最近更新 更多