2014-06-19 67 views
0

長話短說,我正在進行一次API調用,以檢索名稱列表。以編程方式調用API並添加按鈕

對於每個檢索的名字,我想添加一個RelativeLayout(有一些孩子)到一個父LinearLayout。相對佈局看起來是這樣的:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/border_bottom" 
    android:clickable="true"> 

    <ImageView 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_weight="11.79" 
     android:background="#58585a" 
     android:id="@+id/imageView" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textView2" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="26dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Small Text" 
     android:id="@+id/textView3" 
     android:layout_below="@+id/textView2" 
     android:layout_alignStart="@+id/textView2" /> 

</RelativeLayout> 

我想改變「小字體」和「大文本」字段的一些數據,我從查詢服務器接收。

要實現這一目標,最好的方法是什麼?我可以將上述佈局存儲在單獨的XML文件中,並快速更改文本值,並將其放入現有的線性佈局中?

+3

這看起來像是一個ListView的教科書。你有沒有考慮過使用一個? – matiash

+0

我做過了,但由於幾個原因,它不太適合我的使用案例。在這種情況下,我有一個ScrollView,它包含一個LinearLayout,我希望爲從數據庫中拉出的每個項目插入上面的代碼。 – opticon

+0

這仍然聽起來像一個ListView的教科書案例... :)嗯,從技術上講,你描述的方法是可行的,但它看起來像'ListView'已經做了什麼(可能更有效率,例如不創建所有兒童觀看,重複使用,&c)。 – matiash

回答

1

LISTING 1:

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="wrap_content" > 

    <ImageView 
    android:id="@+id/listImage" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_margin="10dp" 
    android:background="#ffcccccc" /> 

    <TextView 
    android:id="@+id/listTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/listImage" 
    android:layout_toRightOf="@+id/listImage" 
    android:text="A List item title" 
    android:textSize="16sp" 
    android:textStyle="bold" /> 

    <TextView 
    android:id="@+id/listDescription" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/listTitle" 
    android:layout_marginTop="5dp" 
    android:maxLines="4" 
    android:layout_toRightOf="@+id/listImage" 
    android:text="The List item description" 
    android:textSize="14sp" />  
</RelativeLayout> 

清單2:

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:gravity="center" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@android:id/list" 
      android:fadingEdge="vertical" 
      android:fadingEdgeLength="10dp" 
      android:longClickable="true" 
      android:listSelector="@drawable/list_selector_background" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </ListView> 
<!-- -->               
     <TextView 
      android:id="@android:id/empty" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Loading feed data..." /> 
<!-- -->               
</LinearLayout> 
#1 Listview with id of "list" 
#2 TextView with id of "empty" 

清單3:

ListActivity爲動態的ListView -

public class DynamicListViewActivity extends ListActivity { 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.list);        

      ImageListAdapter adapter = new ImageListAdapter(this); 
      setListAdapter(adapter);         

      LoadFeedData loadFeedData = new LoadFeedData(adapter); 
      loadFeedData.execute();          
     } 
} 

清單4:

用於裝載供給數據

LoadFeedData類 -

public class LoadFeedData extends 
      AsyncTask<void, void,="" arraylist<entry="">> {  

     private final String mUrl =        
     "URL_QUERING_TO_SERVER"; 

     private final ImageListAdapter mAdapter; 

     public LoadFeedData(ImageListAdapter adapter) {   
      mAdapter = adapter; 
     } 

     private InputStream retrieveStream(String url) { 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = null; 
      httpGet = new HttpGet(url); 

      HttpResponse httpResponse = null; 
      try { 
      httpResponse = client.execute(httpGet); 
      HttpEntity getResponseEntity = httpResponse.getEntity(); 
      return getResponseEntity.getContent(); 
      } catch (IOException e) { 
      httpGet.abort(); 
      } 
      return null; 
     } 

     @Override 
     protected ArrayList<Entry> doInBackground(Void... params) { 
      InputStream source = retrieveStream(mUrl);     
      Reader reader = null; 
      try { 
      reader = new InputStreamReader(source); 
      } catch (Exception e) { 
      return null; 
      } 
      Gson gson = new Gson(); 
      SearchResult result = gson.fromJson(reader,SearchResult.class);  
      return result.getFeed().getEntry(); 
     } 

     protected void onPostExecute(ArrayList<Entry> entries) { 
      mAdapter.upDateEntries(entries);      
     } 
} 

清單5:

ImageListAdapter用於填充與數據和圖像的ListView -

public class ImageListAdapter extends BaseAdapter { 

     private Context mContext; 

     private LayoutInflater mLayoutInflater;        

     private ArrayList<Entry> mEntries = new ArrayList<Entry>();   

     private final ImageDownloader mImageDownloader;      

     public ImageListAdapter(Context context) {       
      mContext = context; 
      mLayoutInflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mImageDownloader = new ImageDownloader(context); 
     } 

     @Override 
     public int getCount() { 
      return mEntries.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return mEntries.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, 
      ViewGroup parent) {           
      RelativeLayout itemView; 
      if (convertView == null) {           
      itemView = (RelativeLayout) mLayoutInflater.inflate(
         R.layout.list_item, parent, false); 

      } else { 
      itemView = (RelativeLayout) convertView; 
      } 

      ImageView imageView = (ImageView) 
      itemView.findViewById(R.id.listImage);       
      TextView titleText = (TextView) 
      itemView.findViewById(R.id.listTitle);       
      TextView descriptionText = (TextView) 
      itemView.findViewById(R.id.listDescription);     

      String imageUrl = mEntries.get(position).getContent().getSrc(); 
      mImageDownloader.download(imageUrl, imageView);     

      String title = mEntries.get(position).getTitle().get$t(); 
      titleText.setText(title); 
      String description = 
      mEntries.get(position).getSummary().get$t(); 
      if (description.trim().length() == 0) { 
      description = "Sorry, no description for this image."; 
      } 
      descriptionText.setText(description); 

      return itemView; 
     } 

     public void upDateEntries(ArrayList<Entry> entries) { 
      mEntries = entries; 
      notifyDataSetChanged(); 
     } 
} 

大!這樣您將從服務器加載動態數據並將其顯示在列表視圖中。 很顯然,您可以更改其中的一些或更多以添加適合您的應用程序。 乾杯!

+0

我不得不修改這個,但這讓我走上了正確的道路!謝謝! – opticon

0

通過這種方法,如果您收到包含10個以上項目的列表,您的應用將具有非常低的性能。使用ListView和您的自定義適配器可以實現所有目標的概率爲99%。在這種情況下,android只有在屏幕上顯示時纔會創建視圖。在你的情況下,即使他們不需要,視圖也會存在。

雖然如果您仍然想使用您的方法,您可以通過此Id找到TextView並更改其文本。

近似代碼

... 
LayoutInflater inflater = LayoutInflater.from(getContext()); 
customView = inflater.inflate(%Your_layout_id%, null); // Put id of your xml layout file 

largeTextView = (TextView)customView.findViewById(R.id.textView2); 
largeTextView.setText(%your_large_text%); 
TextView smallTextView = (TextView)customView.findViewById(R.id.textView3); 
smallTextView.setText(%your_small_text%); 
... 
相關問題