0

我想添加自定義適配器,我想補充這是我從網絡服務獲得該特定文件夾的文件夾圖像和名稱。如何使用ArrayAdapter添加自定義適配器?

請看看我的代碼:

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 []來獲得網絡響應,然後財產1的ID文件夾名稱。任何人都可以讓我知道如何添加自定義列表,其中顯示文件夾圖像和文件夾名稱或可以正確引導?由於

回答

0

首先沒有必要設置爲內循環的適配器。它會在您每次運行循環時將您的適配器設置爲列表視圖。你的代碼沒有錯,但也沒有效率。你應該通過Below方式設置你的ArrayAdapter Outside for循環。

 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。 希望它會幫助。

+0

我想自定義適配器,列表顯示。問題是,我想使用自定義適配器從Web服務顯示文件夾的文件夾圖像和名稱 – Shweta 2013-02-26 06:24:30

+0

@shiva:我已經編輯我的答案。 – 2013-02-26 06:28:01

+0

我明白了你的意思,但你有沒有看過我的問題? – Shweta 2013-02-26 07:28:01