繁体   English   中英

如何在ListView行内添加多个textView和动态ImageView

[英]How to add multiple textView and dynamic ImageView inside ListView Row

有人请以正确的方式指导我。

我有以下设计:main.xml

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

        <ListView android:layout_width="fill_parent"   
          android:layout_height="fill_parent"   
          android:id="@+id/topSongs">  
        </ListView>  

    </RelativeLayout>

list.xml

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

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/albumart_cocktail" />

        <TextView
            android:id="@+id/AlbumText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imageView2"
            android:layout_toRightOf="@+id/imageView2"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/songText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/AlbumText"
            android:layout_centerVertical="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

活动代码:

我有这个数组:

 String[] AlbumText = {"Maximum", "Shangai", "Cocktail",
            "Rowdy Rathore", "Bol Bachan"
    };
    String[] songText = {"Sunday", "Monday", "Tuesday",
              "Wednesday", "Thursday"
    };

并为特定歌曲提供5张图片(专辑封面)。 我正在这样设置数组:

setListAdapter(new ArrayAdapter<String>(this,
    R.layout.list, R.id.songText, songText));
setListAdapter(new ArrayAdapter<String>(this,
    R.layout.list, R.id.AlbumText, AlbumText));

我知道上述错误是错误的,我需要知道如何以正确的方式进行操作。

我的问题:我需要实现一个带有2个textView和1个专辑封面的listView,为此,我使用了list.xml文件中的相对布局。 我至少知道如何将一个数组发送到textView中,我真的不知道如何更改特定行的图像。

请有人以正确的方式指导我。

错误:

setListAdapter(new ArrayAdapter<String>(this,
    R.layout.list, R.id.songText, songText));
setListAdapter(new ArrayAdapter<String>(this,
    R.layout.list, R.id.AlbumText, AlbumText));

创建自定义适配器的建议/步骤:

  1. 创建一个自定义适配器并扩展ArrayAdapter或BaseAdapter
  2. 实现getView()方法。
  3. 遵循视图持有人的设计模式。

是的,如果您拥有有关歌曲或专辑的10个详细信息,那么您将创建10个Array还是Arraylist? 而是使用getter / setter方法创建一个自定义类,为特定项目创建对象,然后添加到ArrayList中。 因此,很容易管理除10个数组或ArrayList之外的单个ArrayList的单个详细信息。

对于您要实现的目标,我相信您将需要实现自定义适配器。
而且我认为将适配器设置两次是不正确的,因为只有最后一行才会生效。

您可以查看Android博客文章中的“ 构建自定义花式ListView”,以了解如何构建自定义适配器。

数组

String[] AlbumText = {"Maximum", "Shangai", "Cocktail", "Rowdy Rathore", "Bol Bachan"
};
String[] songText = {"Sunday", "Monday", "Tuesday","Wednesday", "Thursday"
};

 // add values to adapter class like this

MobileArrayAdapter adapter = new MobileArrayAdapter(this, AlbumText , songText );
        list.setAdapter(adapter);                

        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                      
              //here to use get values from adapter class and pass values use intent

                    Intent intent = new Intent(ListMobileActivity.this, Display.class);                         
                    intent.putExtra("AlbumText", one);
                    intent.putExtra("songText", two);                                    

                    startActivity(intent);          
                }
            }        
        });
    }

显示活动

            message = getIntent().getExtras().getString("AlbumText");
            message1 = getIntent().getExtras().getString("songText");                                          

            text.setText(message);
            text1.setText(message1);

为了在列表视图的单行中开发一个具有Imageview和多个textview的联系人列表,我使用以下xml来定义listview中的每一行。

<?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:orientation="vertical" >

<ImageView
    android:id="@+id/pIcon"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:contentDescription="@string/hello"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp">
</ImageView>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_toRightOf="@+id/pIcon"
    android:layout_alignBaseline="@+id/pIcon"
    android:layout_alignTop="@+id/pIcon" >

    <TextView
        android:id="@+id/pName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/pNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

我为列表视图定义了一个自定义适配器。 自定义适配器的代码是

public class ContactListAdapter extends BaseAdapter {

private ArrayList<ContactListItem> contactListItems;
private Context context;

public ContactListAdapter(ArrayList<ContactListItem> contactListItems,
        Context context) {
    this.contactListItems = contactListItems;
    this.context = context;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.contact_list_item, null);
    }
    // references to the list items
    TextView numTxt = (TextView) convertView.findViewById(R.id.pNum);
    TextView nameTxt = (TextView) convertView.findViewById(R.id.pName);
    ImageView peopleIcon = (ImageView) convertView.findViewById(R.id.pIcon);
    // set the value of the list items
    peopleIcon.setImageResource(contactListItems.get(position).getIcon());
    nameTxt.setText(contactListItems.get(position).getName());
    numTxt.setText(contactListItems.get(position).getNum());
    return convertView;
}

}

为了获得联系人列表项,我定义了另一个类。 该类的代码是

public class ContactListItem {
int pIcon;
String pNum;
String pName;

public ContactListItem() {
}

public ContactListItem(int pIcon, String pNum, String pName) {
    this.pIcon = pIcon;
    this.pNum = pNum;
    this.pName = pName;
}

public int getIcon() {
    return this.pIcon;
}

public String getNum() {
    return this.pNum;
}

public String getName() {
    return this.pName;
}

public void setIcon(int icon) {
    this.pIcon = icon;
}

public void setNum(String num) {
    this.pNum = num;
}

public void setName(String name) {
    this.pName = name;
}
}

列表视图是在一个片段中使用的,您可以轻松地在活动中使用它。

public class ContactFragment extends Fragment {
private ListView mContactList;

// array for the contact list
private String[] pNum;
private String[] pName;
private TypedArray pIcon;

private ArrayList<ContactListItem> contactListItems;
private ContactListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // get the names of people in the contact list
    pName = getResources().getStringArray(R.array.pNames);
    // get the numbers of the people in the contact list
    pNum = getResources().getStringArray(R.array.pNumbers);
    // get the photos of the people in the list
    pIcon = getResources().obtainTypedArray(R.array.pIcons);
    // Initialise the contact list adapter
    contactListItems = new ArrayList<ContactListItem>();
    // add the items to array list
    for (int i = 0; i < 5; i++) {
        contactListItems.add(new ContactListItem(
                pIcon.getResourceId(i, -1), pNum[i], pName[i]));
    }
    pIcon.recycle();
}

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

    View rootView = inflater.inflate(R.layout.contact_us_layout, container,
            false);

    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // set the title of the activity
    ((MainActivity) getActivity()).setTitle("CONTACT US");
    // get a reference to the contact list
    mContactList = (ListView) view.findViewById(R.id.contact_list);
    adapter = new ContactListAdapter(contactListItems, getActivity());
    mContactList.setAdapter(adapter);
}
}

如果有人希望看到该代码以便可以在活动中使用它,请发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM