返回列表 發帖

Android App - Multi-line List

Multi-line List 指清單中的每一個單項包含多行資料

共有三個Java 檔,
MultilineslistActivity.Java 內容如下

package sample.multilineslist;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MultilineslistActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ArrayList<SearchResults> searchResults = GetSearchResults1();
        
        final ListView lv1 = (ListView) findViewById(R.id.ListView01);
        lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
     }
   
    private ArrayList<SearchResults> GetSearchResults1(){
     ArrayList<SearchResults> results = new ArrayList<SearchResults>();
     
     SearchResults sr1 = new SearchResults();
     sr1.setName("明治 青提朱古力橡皮糖96克");
     sr1.setBarcode("35264158293     $12.50");
     sr1.setLocation("K04-L07-R03");
     results.add(sr1);
     
     sr1 = new SearchResults();
     sr1.setName("萬樂珠 薄荷味37.5克");
     sr1.setBarcode("23564526895     $16.00");
     sr1.setLocation("K04-L07-R04");
     results.add(sr1);
     
     sr1 = new SearchResults();
     sr1.setName("瑞士糖 黑加侖味條裝49克");
     sr1.setBarcode("45298562145     $20.50");
     sr1.setLocation("K04-L07-R05");
     results.add(sr1);
     
     sr1 = new SearchResults();
     sr1.setName("瑞士糖 草莓口味條裝49克");
     sr1.setBarcode("54698212566     $25.00");
     sr1.setLocation("K04-L07-R06");
     results.add(sr1);
     
     sr1 = new SearchResults();
     sr1.setName("萬樂珠 薄荷味37.5克");
     sr1.setBarcode("74585236215     $30.00");
     sr1.setLocation("K04-L08-R04");
     results.add(sr1);
     
     sr1 = new SearchResults();
     sr1.setName("爽浪 無糖香口珠 橙薑味28克");
     sr1.setBarcode("85412569521     $28.50");
     sr1.setLocation("K04-L08-R05");
     results.add(sr1);
     
     sr1 = new SearchResults();
     sr1.setName("爽浪 無糖香口珠 檸蜜味28克");
     sr1.setBarcode("8545845215     $25.00");
     sr1.setLocation("K04-L08-R06");
     results.add(sr1);

     sr1 = new SearchResults();
     sr1.setName("爽浪 無糖香口珠 西柚薄味28克");
     sr1.setBarcode("56451258652     $12.50");
     sr1.setLocation("K04-L09-R03");
     results.add(sr1);

     sr1 = new SearchResults();
     sr1.setName("爽浪無糖香口珠 超涼薄荷味28克");
     sr1.setBarcode("41526521456     $16.00");
     sr1.setLocation("K04-L09-R04");
     results.add(sr1);
   
    sr1 = new SearchResults();
     sr1.setName("寶珠 迷你珍寶珠 5支裝 30克");
     sr1.setBarcode("58478787965     $20.50");
     sr1.setLocation("K04-L09-R05");
     results.add(sr1);
      
     sr1 = new SearchResults();
     sr1.setName("利口樂潤喉糖 檸檬香草50克");
     sr1.setBarcode("12141265231     $25.00");
     sr1.setLocation("K04-L03-R06");
     results.add(sr1);
            
     sr1 = new SearchResults();
     sr1.setName("利口樂潤喉糖 接骨木花香草50克");
     sr1.setBarcode("84578789845     $30.00");
     sr1.setLocation("K04-L03-R04");
     results.add(sr1);
   
     sr1 = new SearchResults();
     sr1.setName("利口樂潤喉糖 黑加侖子香草50克");
     sr1.setBarcode("45445651221     $28.50");
     sr1.setLocation("K04-L03-R05");
     results.add(sr1);
   
     sr1 = new SearchResults();
     sr1.setName("得力素 檸檬糖10pc");
     sr1.setBarcode("96569874747     $25.00");
     sr1.setLocation("K04-L03-R06");
     results.add(sr1);   

     return results;
    }
}

MyCustomBaseAdapter.java 內容如下

package sample.multilineslist;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyCustomBaseAdapter extends BaseAdapter {

private static ArrayList<SearchResults> searchArrayList;


private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {

searchArrayList = results;

mInflater = LayoutInflater.from(context);
}

public int getCount() {
return searchArrayList.size();
}

public Object getItem(int position) {
return searchArrayList.get(position);
}

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


public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {

  convertView = mInflater.inflate(R.layout.custom_row_view, null);
  holder = new ViewHolder();
  holder.txtName = (TextView) convertView.findViewById(R.id.name);
  holder.txtBarcode = (TextView) convertView.findViewById(R.id.barcode);
  holder.txtLocation = (TextView) convertView.findViewById(R.id.location);
  convertView.setTag(holder);
  } else {
  holder = (ViewHolder) convertView.getTag();
  }

  holder.txtName.setText(searchArrayList.get(position).getName());
  holder.txtBarcode.setText(searchArrayList.get(position).getBarcode());
  holder.txtLocation.setText(searchArrayList.get(position).getLocation());
  return convertView;
}

static class ViewHolder {
  TextView txtName;
  TextView txtBarcode;
  TextView txtLocation;
}
}


SearchResults.java  內容如下:

package sample.multilineslist;

public class SearchResults {
private String name = "";
private String barcode = "";
private String location = "";

public void setName(String name) {

  this.name = name;
}

public String getName() {
  return name;
}

public void setBarcode(String barcode) {
  this.barcode = barcode;
}

public String getBarcode() {

  return barcode;
}

public void setLocation(String location) {
  this.location = location;
}

public String getLocation() {
  return location;
}
}

在 res/layout 下加入以下檔案
custom_row_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
  
  <TextView android:id="@+id/name"
  android:textSize="18sp"
  android:textStyle="bold"
  android:textColor="#FFFF00"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
  
<TextView android:id="@+id/barcode"
  android:textSize="16sp"
  android:textStyle="normal"   
  android:textColor="#5BEF8C"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

<TextView android:id="@+id/location"
  android:textSize="16sp"
  android:textColor="#FFFFFF"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

返回列表