Android - 将字符串数组值加载到自定义列表视图适配器

时间:2016-04-24 09:20:00

标签: java android listview custom-adapter

我创建了一个自定义列表视图适配器,列表视图的项目存储在String.xml中。当我运行应用程序时,我只显示一个空白活动。

请帮我解决这个问题。

这是我的自定义适配器java类。

public class UserAdapter extends ArrayAdapter<User> {
public UserAdapter(Context context, ArrayList<User> users, String[] number) {
    super(context, 0, users);
}

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

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_singlerow, parent, false);
    }

    TextView tvName = (TextView) convertView.findViewById(R.id.org_name);
    TextView tvNum = (TextView) convertView.findViewById(R.id.cn_num);
    // Populate the data into the template view using the data object
    tvName.setText(user.company_name);
    tvNum.setText(user.contact_num);
    return convertView;
}}

列表视图模型类。

public class User {
public String company_name;
public Integer contact_num;

public User(String company_name, Integer contact_num) {
    this.company_name = company_name;
    this.contact_num = contact_num;
}}

主要活动类,我将适配器附加到视图。

public class activity_one extends Activity {

String[] number;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);



 number = getResources().getStringArray(R.array.dummy_data);

    ArrayList<User> arrayOfUsers = new ArrayList<User>();

    UserAdapter adapter = new UserAdapter(this, arrayOfUsers,number);

    ListView listView = (ListView) findViewById(R.id.listView2);
    listView.setAdapter(adapter);



}}

1 个答案:

答案 0 :(得分:0)

由于我不能100%确定你想要做什么,所以我会向你展示我的一段代码,你可以从中得出你需要的东西。 我的DrawerListAdapter是自定义ListView适配器,该列表中的每个项目都包含3个对象 - String名称,String imageurl,String说明。< / p>

public class DrawerListAdapter extends ArrayAdapter<String> {

private final static int settingsCount = 4; // can ignore this
private Activity context;
// Next 3 lines: All data required for this adapter
private static ArrayList<String> itemname = null;
private static ArrayList<String> imgid;
private static ArrayList<String> itemDescription;

public DrawerListAdapter(Activity context, ArrayList<String> itemname, ArrayList<String> itemDescription, ArrayList<String> imgid) {
    // R.layout.drawer_list_item is a layout that represents ONE item
    // (not 100% sure, but i think the super class inflates this view itemname.size() times)
    super(context, R.layout.drawer_list_item, itemname);

    this.context=context;
    // saving the required data
    DrawerListAdapter.itemDescription = itemDescription;
    DrawerListAdapter.itemname=itemname;
    DrawerListAdapter.imgid=imgid;
}

@Override
public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.drawer_list_item, null,true);
    // the views that are present in R.layout.drawer_list_item
    TextView txtTitle = (TextView) rowView.findViewById(R.id.tvName);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.tvDescription);

    txtTitle.setText(itemname.get(position));
    extratxt.setText(itemDescription.get(position));
    // setting the image here of the list item here
    if (position < settingsCount) {
        imageView.setImageResource(Integer.parseInt(imgid.get(position)));
    } else {
        Glide.with(context).load(imgid.get(position)).into(imageView);
    }
    return rowView;

};
// need to override the getCount. this function just returns the amount of items in your list
 @Override
 public int getCount(){
     if (itemname != null)
       return itemname.size();
     return 0;
 }

这是通过以下方式初始化的:

     private void init() {
     mDrawerList = (ListView) findViewById(R.id.left_drawer);
     if (adapter == null) {
         ArrayList<String> itemname = new ArrayList<String>();
            itemname.add("item1");
            itemname.add("item2");
            itemname.add("item3");
            itemname.add("item4");

            ArrayList<String> imgid = new ArrayList<String>();
            imgid.add(R.drawable.pic1 + "");
            imgid.add(R.drawable.pic2 + "");
            imgid.add(R.drawable.pic4 + "");
            imgid.add(R.drawable.pic3 + "");

            ArrayList<String> itemDescription = new ArrayList<String>();
            itemDescription.add("desc1");
            itemDescription.add("desc2");
            itemDescription.add("desc3");
            itemDescription.add("desc4");


            adapter=new DrawerListAdapter(this, itemname, itemDescription, imgid);
            setOnItemClickListener(); // sets onClick for each item on the list
     }

     mDrawerList.setAdapter(adapter);
 }

和drawer_list_item.xml,以防你感兴趣:

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

<ImageView
    android:id="@+id/icon"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:padding="5dp" />

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/tvName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:padding="2dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#429bd9" />

<TextView
    android:id="@+id/tvDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:maxLines="2"
    android:textColor="#79e5a4" />

</LinearLayout>