简体   繁体   English

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

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

Someone pls guide me in the right way. 有人请以正确的方式指导我。

I have the below Design: main.xml 我有以下设计: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 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>

Activity code: 活动代码:

I have this array: 我有这个数组:

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

And 5 images(album art) for the particular song. 并为特定歌曲提供5张图片(专辑封面)。 I'm setting the array like this: 我正在这样设置数组:

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

I know the above one is wrong, I need to know hw to do it in a proper way. 我知道上述错误是错误的,我需要知道如何以正确的方式进行操作。

My Question: I need to implement a listView with 2 textView and 1 album art, so for that I have used the relative layout inside the list.xml file. 我的问题:我需要实现一个带有2个textView和1个专辑封面的listView,为此,我使用了list.xml文件中的相对布局。 Atleast I got to know how to send one array into textView, I really don't know how to change the images for particular row. 我至少知道如何将一个数组发送到textView中,我真的不知道如何更改特定行的图像。

pls someone guide me in a right way. 请有人以正确的方式指导我。

Wrong: 错误:

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

Suggestion/Steps to create custom adapter: 创建自定义适配器的建议/步骤:

  1. Create a custom adapter and extends either ArrayAdapter or BaseAdapter 创建一个自定义适配器并扩展ArrayAdapter或BaseAdapter
  2. Implement getView() method. 实现getView()方法。
  3. Follow view holder design pattern. 遵循视图持有人的设计模式。

And yes, if you are having 10 details about your song or album, then Will you create 10 Array or Arraylist? 是的,如果您拥有有关歌曲或专辑的10个详细信息,那么您将创建10个Array还是Arraylist? Instead create a custom class with getter/setter methods, create object for particular item and add into ArrayList. 而是使用getter / setter方法创建一个自定义类,为特定项目创建对象,然后添加到ArrayList中。 So its easy to manage a single ArrayList other than 10 array or ArrayList for single detail. 因此,很容易管理除10个数组或ArrayList之外的单个ArrayList的单个详细信息。

For what you want to achieve I believe you will need to implement your custom adapter. 对于您要实现的目标,我相信您将需要实现自定义适配器。
And I believe setting the adapter twice is not correct, because only the last line will take effect. 而且我认为将适配器设置两次是不正确的,因为只有最后一行才会生效。

You may take a look over Building a Custom Fancy ListView in Android blog post to get an idea how to build a custom adapter. 您可以查看Android博客文章中的“ 构建自定义花式ListView”,以了解如何构建自定义适配器。

arrays 数组

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);          
                }
            }        
        });
    }

display activity 显示活动

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

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

To develop a contact list with an Imageview and multiple textviews in a single row of a listview I used the following xml as to define each row in the listview. 为了在列表视图的单行中开发一个具有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>

I defined a custom adapter for the listview. 我为列表视图定义了一个自定义适配器。 The code for the custom adapter is 自定义适配器的代码是

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;
}

}

To get the contact list items I defined another class. 为了获得联系人列表项,我定义了另一个类。 The code for the class is 该类的代码是

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;
}
}

The listview was used in a fragment you can easily use it inside an activity. 列表视图是在一个片段中使用的,您可以轻松地在活动中使用它。

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);
}
}

If anyone wants to see the code so that it can be used in an activity then please comment. 如果有人希望看到该代码以便可以在活动中使用它,请发表评论。

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

相关问题 如何在textview内添加imageview或在imageview内添加android的textview? - How to add imageview inside textview or add textview inside imageview, android? 如何在Android的ListView中添加ImageView,TextView和Button - How to add ImageView, TextView and Button in ListView in android 如何使用水平滚动视图在listview行上添加多个imageview - how to add multiple imageview on listview row with horizontal scroll view 如何在充满ImageView的GridView中添加TextView - How to add TextView inside a GridView filled with ImageView 如何在自定义ListView行中相应地对齐ImageView和TextView? - How to align accordingly an ImageView and a TextView in a custom ListView row? 如何在Android中将TextView / ImageView动态添加到ListView项 - How to dynamically add a TextView/ImageView to a ListView item in Android 如何添加依赖于ListView中TextView文本的ImageView - How to add ImageView's that are depending on the text of the TextView in a ListView 具有动态布局的ListView项(Imageview,TextView) - ListView item with dynamic layout(Imageview, TextView) 如何在自定义列表视图中添加textview和editview? - How to add textview and editview inside custom listview? 在列表视图行中分别突出显示嵌套的TextView和ImageView - Separately highlight nested TextView and ImageView in ListView row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM