如何在列表视图中显示两个字符串数组(不在res中)

时间:2016-03-24 13:19:45

标签: java android arrays string listview

我想在列表视图中的String[] arrayTitles之上显示String[] arrayDescriptions,这应该是子项。我该怎么做?发布此代码的工作版本将非常感激。

ListView listview = (ListView)findViewById(R.id.listview);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_list);

    String[] arrayTitles = formattedTitles.split(",");
    String[] arrayDescriptions = formattedDescriptions.split(",");

    for (int i = 0; i < arrayLocations.length; i++) {
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (context, android.R.layout.simple_list_item_1, arrayTitles);
        listview.setAdapter(arrayAdapter);
    }
}

2 个答案:

答案 0 :(得分:0)

使用两种类型的视图 我在getview()中的主逻辑下面发布了工作代码。

public class FriendsList extends BaseAdapter{
    private Context context;
    private final int Separater_Type = 0;
    private final int List_Type = 1;

    ArrayList< FriendsInfo> list=new ArrayList<FriendsInfo>();
    private ActiveList mActiveList = null;
    ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
    public FriendsList(Context context,ArrayList<FriendsInfo> list,ActiveList activeList){
        this.context=context;
        mActiveList = activeList;
        this.list=list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();

    }
    ViewHolder holder = null;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        int rowType;
        rowType = list.get(position).type;

        if (convertView == null) {

            holder = new ViewHolder();
            switch (rowType){
            case Separater_Type:
                convertView = View.inflate(context, R.layout.activelistheader, null);
                holder.headerName = (TextView) convertView.findViewById(R.id.list_separator);break;
            case List_Type:
                convertView = View.inflate(context, R.layout.rowlist, null);
                holder.avatar = (CircleImageView)convertView.findViewById(R.id.avatarimage);
                holder.headerName = (TextView) convertView.findViewById(R.id.name);
                holder.selectMark = (ImageView) convertView.findViewById(R.id.select_img);
                //holder.select_img = (ImageView)convertView.findViewById(R.id.select_img);
                break;
            }

            convertView.setTag(holder); 
        }

        else{
            holder = (ViewHolder) convertView.getTag();
        }

        if(list.get(position).type ==0){

            holder.headerName.setText(list.get(position).name);
        }else{

        }

        return convertView;
    }

    private class ViewHolder {
        CircleImageView avatar;
        TextView headerName;
        ImageView selectMark;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }
}

答案 1 :(得分:0)

我使用自定义适配器发布答案,您只需创建一个类CustomAdapter.java

    package com.example.your.package.name;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

    Context mContext;
    String[] arrayTitles;
    String[] arrayDescriptions;

    public CustomAdapter(Context mContext,String[] arrayTitles,String[] arrayDescriptions)
    {
        this.mContext = mContext;
        this.arrayTitles = arrayTitles;
        this.arrayDescriptions = arrayDescriptions;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(parent.getContext());
            v = vi.inflate(R.layout.item_list_row, null);
        }

        TextView tvTitle = (TextView) v.findViewById(R.id.txtTitle);
        TextView tvDesc = (TextView) v.findViewById(R.id.txtDesc);

        tvTitle.setText(arrayTitles[position]);
        tvDesc.setText(arrayDescriptions[position]);

        return v;
    }
}

创建一个item_list_row.xml

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

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:padding="5dp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/txtDesc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Description"
        android:paddingLeft="5dp"
        android:textStyle="normal"/>
</LinearLayout>

并将ActivityAdapter替换为Activity中的CustomAdapter(具有ListView) 你的onCreate方法看起来像

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

   ListView listview = (ListView)findViewById(R.id.listview);

    String[] arrayTitles = formattedTitles.split(",");
    String[] arrayDescriptions = formattedDescriptions.split(",");

        CustomAdapter arrayAdapter = new CustomAdapter(ListViewActivity.this, arrayTitles,arrayDescriptions);
        listview.setAdapter(arrayAdapter);
}

我认为它会正常工作,如果它不起作用让我建议你想要什么,我会编辑我的答案 谢谢