如何使用ArrayAdapter添加自定义适配器?

时间:2013-02-26 06:12:34

标签: android web-services android-arrayadapter custom-adapter

我正在尝试添加自定义适配器,我想添加文件夹图像和特定文件夹的名称,这是我从网络服务中获取的。

请参阅我的代码:

ListView listview;
LazyAdapter adapter;

private String[] folderslist;

String User_ID;
int i;

static String ID = "ID";
static String KEY_Name = "FolderName";
static String ParentID = "ParentID";
static String CreatedBy = "CreatedBy";                               

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://***.***.*.*/somethinghere/something.asmx";
private static final String TREEDATA_METHOD = "TreeData";
private static final String SOAP_ACTION_TREEDATA = "http://tempuri.org/TreeData";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homepage_layout);

    Intent Intent = getIntent();
    User_ID = Intent.getStringExtra("User_ID");

    Treedata();

    listview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            String item = (String) listview.getItemAtPosition(position);
            ID = item;          
            Intent mIntent = new Intent(getApplicationContext(), Files_Folders_Activity.class);
            mIntent.putExtra("ID", ID);

            startActivity(mIntent);

        }
    });

}

        public void Treedata(){
            try {

                SoapObject datarequest = new SoapObject(NAMESPACE, TREEDATA_METHOD);

                datarequest.addProperty("UserID", User_ID);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(datarequest);

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.debug = true;

                androidHttpTransport.call(SOAP_ACTION_TREEDATA, envelope);

                SoapObject folderResponse = (SoapObject)envelope.getResponse();

                Log.i("AllFolders", folderResponse.toString());

                folderslist = new String[folderResponse.getPropertyCount()];

                for(i=0; i<folderResponse.getPropertyCount(); i++) {

                    SoapObject SingleFolder = (SoapObject)folderResponse.getProperty(i);

                    Log.i("SingleFolder", SingleFolder.toString());

                    ID= SingleFolder.getProperty(0).toString();
                    KEY_Name = SingleFolder.getProperty(1).toString();
                    ParentID = SingleFolder.getProperty(2).toString();
                    CreatedBy= SingleFolder.getProperty(3).toString();                      

                    folderslist[i] = SingleFolder.getProperty(0).toString();

                    folderslist = new String[] {ID};

                   }

                    data = new ArrayList<FolderList>();
                    for (int i = 0; i < folderslist.length; i++) {

                    FolderList item = new FolderList(images[i], folderslist[i]);
                    data.add(item);

            }   
                    listview = (ListView)findViewById(R.id.listview);

                    LazyAdapter adapter = new LazyAdapter(this, R.layout.jpg_row, data);

                    listview.setAdapter(adapter);

                    listview.setOnItemClickListener(this);
            }

        }

LazyAdapter:

public class LazyAdapter extends ArrayAdapter<FolderList> {

Context context;

public LazyAdapter(Context context, int resourceId, List<FolderList> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    FolderList data = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.jpg_row, null);
        holder = new ViewHolder();

        holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imgIcon);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtTitle.setText(data.getTitle());
    holder.imageView.setImageResource(data.getIcon());

    return convertView;
}

}

我使用string []来获取web响应,然后使用属性1来获取id文件夹名称。任何人都可以告诉我如何添加自定义列表,其中显示文件夹图像和文件夹名称或者可以正确引导?感谢

1 个答案:

答案 0 :(得分:0)

首先不需要在for循环中设置适配器。每次运行循环时,它都会将适配器设置为listview。你的代码没有错,但效率也不高。你应该通过下面的方式设置你的ArrayAdapter外循环。

     for(i=0; i<folderResponse.getPropertyCount(); i++) {

                        SoapObject SingleFolder = (SoapObject)folderResponse.getProperty(i);

                        Log.i("SingleFolder", SingleFolder.toString());

                        ID= SingleFolder.getProperty(0).toString();
                        KEY_Name = SingleFolder.getProperty(1).toString();
                        ParentID = SingleFolder.getProperty(2).toString();
                        CreatedBy= SingleFolder.getProperty(3).toString();

                        System.out.println(ID);
                        System.out.println(KEY_Name);
                        System.out.println(ParentID);
                        System.out.println(CreatedBy);

                        folderslist[i] = SingleFolder.getProperty(0).toString();


                }
          listview = (ListView)findViewById(R.id.listview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,   android.R.layout.simple_list_item_1,android.R.id.text1, folderslist);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(this);

此外,如果您想使用文件夹图像和文件夹名称制作自定义列表,则应使用BaseAdater而不是ArrayAdapter。

您将从Custom Listview with BaseAdapter Sample Demo获得更好的创意如何在BaseAdapter中使用自定义列表。 希望它会有所帮助。