【问题标题】:How to add multiple textView and dynamic ImageView inside ListView Row如何在 ListView Row 中添加多个 textView 和动态 ImageView
【发布时间】:2012-08-03 10:55:57
【问题描述】:

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

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

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

【问题讨论】:

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


【解决方案1】:

错误:

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 个数组还是 Arraylist? 而是创建一个具有 getter/setter 方法的自定义类,为特定项目创建对象并添加到 ArrayList 中。因此,管理单个 ArrayList 而不是 10 个数组或 ArrayList 以获取单个详细信息很容易。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      数组

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

      【讨论】:

        【解决方案4】:

        为了在列表视图的单行中开发一个包含 Imageview 和多个文本视图的联系人列表,我使用以下 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>
        

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多