如何在ListView Row中添加多个textView和动态ImageView

时间:2012-08-03 10:55:57

标签: android android-layout android-listview android-arrayadapter

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

我有以下设计: 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,我真的不知道如何更改特定行的图像。

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

4 个答案:

答案 0 :(得分:0)

错:

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. 遵循视图持有人设计模式。
  4. 是的,如果你有关于你的歌曲或专辑的10个细节,那么你会创建10个数组或Arraylist吗?而是用getter / setter方法创建一个自定义类,为特定项创建对象并添加到ArrayList中。因此,对于单个细节,可以轻松管理除10个数组或ArrayList之外的单个ArrayList。

答案 1 :(得分:0)

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

您可以查看Building a Custom Fancy ListView in Android博文,了解如何构建自定义适配器。

答案 2 :(得分:0)

阵列

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

答案 3 :(得分:0)

要在列表视图的单行中使用Imageview和多个textview开发联系人列表,我使用以下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: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>

我为listview定义了一个自定义适配器。自定义适配器的代码是

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

listview用于一个片段,您可以在活动中轻松使用它。

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

如果有人想查看代码以便可以在活动中使用,请发表评论。