下滑列表

新建APP類負責全局的上下文類

public class App extends Application {
public static App appcontext;

@Override
public void onCreate() {
    super.onCreate();
    appcontext=this;
}
}

主界面自己搭建,R.layou是下面的界面名

//將數組傳給適配器,  ?為LsitView的名字
            ?.setAdapter(new ListAdapter(array));

布局

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bf.com.myapplication.Activity.TelInfoActivity">
 <ListView
  android:id="@+id/list_tel_select"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
 </ListView>
  </LinearLayout>

單個Item的布局自己定義

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
 <TextView
   android:id="@+id/tv_telinfo_name"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"

   android:textSize="20dp"/>
<TextView
    android:id="@+id/tv_telinfo_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignParentRight="true"
    android:textSize="20dp"/>
</RelativeLayout>

新建一個適配器

public class ListAdapter extends BaseAdapter{
public List<TelInfo> data;
//代表傳過來的數組
public ListAdapter(List<TelInfo> data){
    this.data=data;
}
@Override
//數組的長度
public int getCount() {
    return data==null?0:data.size();
}
@Override
public Object getItem(int position) {
    return position;
}

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

@Override
//對劃出去的布局進行復用,進行的優化
public View getView(int position, View convertView, ViewGroup parent) {
    viewholder holder=null;
    if (convertView==null){
        //R.layout.tel_info是單個item對應地址
        convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.tel_info,null);
        holder=new viewholder(convertView);
        convertView.setTag(holder);
    }else {
        holder= (viewholder) convertView.getTag();
    }
      TelInfo telInfo=data.get(position);
      holder.name.setText(telInfo.getName());
      holder.number.setText(telInfo.getNumber());
    return holder.convertView;
}
public class viewholder{

    public View convertView;
    public   TextView name;
    public   TextView number;
    //對Id進行復用,進行的優化
    public viewholder(View convertView) {
        name = (TextView) convertView.findViewById(R.id.tv_telinfo_name);
        number = (TextView) convertView.findViewById(R.id.tv_telinfo_number);
        this.convertView = convertView;
    }
}
}

有個實例方便數組的各種參數的調用

public class TelInfo {
private  String name;
private  String number;
//要將闡述傳送過來
public TelInfo(String name, String number) {

    this.name = name;
    this.number = number;
}

public String getName() {
    return name;
}

public String getNumber() {
    return number;
}

public void setName(String name) {this.name = name;}

public void setNumber(String number) {this.number = number;}

}

一般人們會從數據庫讀取文件,所以寫一個類方便調用

public class DB {
private static final String DB_PATH = "data/data/com.bf.com.myapplication/databases";
private static final String DB_NAME = "commonnum.db";
//代表只調用一次,節省時間
static {   copyDB();  }
//可以將輸入流傳進來,也可以新建一個Activity來承接
public static void copyDB() {
    InputStream input = null;
    FileOutputStream output = null;
    try {
        //注意要用這個App.appcontext是要新建一個APP類,在后面
        input = App.appcontext.getAssets().open("db/commonnum.db");
        File file = new File(DB_PATH);
        if (!file.exists()) file.mkdirs();
        output = new FileOutputStream(DB_PATH + File.separator + DB_NAME);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = input.read(buffer)) != -1) {
            output.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (output != null) output.close();
            if (input != null) input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
//將本地數據庫的東西傳送給實例,并返回一個主界面一個數組,讓主界面傳個適配器
public static List<TelInfo> readTelInfo(int idx) {
    List<TelInfo> array = new ArrayList<>();
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + "/" + DB_NAME, null);
    Cursor cursor = db.rawQuery("select * from table"+idx+";", null);
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String number = cursor.getString(cursor.getColumnIndex("number"));
        TelInfo select = new TelInfo(name, number);
        array.add(select);
    }
    cursor.close();
    return array;
}
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容