简体   繁体   English

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

[英]Android - Load string array values to custom list view adapter

I have created a custom list view adapter and items for the list view is stored at String.xml. 我创建了一个自定义列表视图适配器,列表视图的项目存储在String.xml中。 When I run the app I only shows a blank activity. 当我运行应用程序时,我只显示一个空白活动。

Please help me to fix this issue. 请帮我解决这个问题。

This is my custom adapter java class. 这是我的自定义适配器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;
}}

List View model class. 列表视图模型类。

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

Main Activity class where I attach adapter to the view. Main Activity类,我将适配器附加到视图。

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



}}

Since i am not 100% sure on what you want to do, ill show you a piece of my code and you can deduce from that what you need. 由于我不是100%肯定你想做什么,生病告诉你一段我的代码,你可以从中得出你需要的东西。 My DrawerListAdapter is an custom ListView adapter, and every item on that list consists of 3 objects - a String name, String imageurl, String description. 我的DrawerListAdapter是一个自定义ListView适配器,该列表中的每个项目都包含3个对象 - String名称, String imageurl, String description。

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

This is initialized by: 这是通过以下方式初始化:

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

and the drawer_list_item.xml just in case you are intrested: 和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>

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

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