2017年4月25日 星期二

listView的相關知識



1、
listView通常用SimpleAdapter、自定義BaseAdapter這兩種方法


2、
Android SimpleAdapter 的list刷新问题。
使用SimpleAdapter 创建的list,是不能通过notifyDataSetChanged 来更新的,因为SimpleAdapter 主要是用来创建静态的数据的列表,如果要实现动态更新数据,需要自己定义一个基于BaseAdapter的adapter,然后通过notifyDataSetChanged 来更新list。

If you look at the SimpleAdapter description it says it is "An easy adapter to map static data to views defined in an XML file." I've added the emphasis -- put simply, SimpleAdapater isn't built for use with data that changes; it handles static data only. If you can't use an ArrayAdapter because your data has more than a single bit of text, then you will either have to build your own custom ListAdapter, or put your data in a DB and use one of the CursorAdapters.


3、
也就是說如果要寫一個可能會刷新的ListView就要自定義介面。這個程式是我找到的例子。因為這篇還算是草稿,這邊只貼BaseAdapter的部份:檔案連結


import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.TextView;
import java.util.ArrayList;import java.util.HashMap;
public class lvButtonAdapter extends BaseAdapter {
    private class buttonViewHolder {
        ImageView appIcon;        TextView appName;        ImageButton buttonClose;    }

    private ArrayList, 
Object>> mAppList; private LayoutInflater mInflater; private Context mContext; private String[] keyString; private int[] valueViewID; private buttonViewHolder holder;
public lvButtonAdapter(Context c, ArrayList, Object>> appList, int resource, String[] from, int[] to) {
mAppList = appList; mContext = c; mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); keyString = new String[from.length]; valueViewID = new int[to.length]; System.arraycopy(from, 0, keyString, 0, from.length); System.arraycopy(to, 0, valueViewID, 0, to.length); }

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

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

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

public void removeItem(int position){
mAppList.remove(position); this.notifyDataSetChanged(); }

@Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
holder = (buttonViewHolder) convertView.getTag(); } else {
convertView = mInflater.inflate(R.layout.lvitem, null); holder = new buttonViewHolder(); holder.appIcon = (ImageView)convertView.findViewById(valueViewID[0]); holder.appName = (TextView)convertView.findViewById(valueViewID[1]); holder.buttonClose = (ImageButton)convertView.findViewById(valueViewID[2]); convertView.setTag(holder); }

HashMap, Object> appInfo = mAppList.get(position); if (appInfo != null) {
String aname = (String) appInfo.get(keyString[1]); int mid = (Integer)appInfo.get(keyString[0]); int bid = (Integer)appInfo.get(keyString[2]); holder.appName.setText(aname); holder.appIcon.setImageDrawable(holder.appIcon.getResources().getDrawable(mid)); holder.buttonClose.setImageDrawable(holder.buttonClose.getResources().getDrawable(bid)); holder.buttonClose.setOnClickListener(new lvButtonListener(position)); }
return convertView; }

class lvButtonListener implements View.OnClickListener {
private int position;
lvButtonListener(int pos) {
position = pos; }

@Override public void onClick(View v) {
int vid=v.getId(); if (vid == holder.buttonClose.getId())
removeItem(position); }
}
}


參考網頁

2017年4月20日 星期四

2017年4月14日 星期五

[c#]上傳程式網頁範例


因為我看到很多教學網頁都寫得很難懂,像是把上傳限制和上傳程式分開寫教學,所以我測試了一個實際上可以跑的精簡版本放在這邊,希望能加快寫程式查資料的速度。

註:
//假設主目錄在C:\TESTA\WebSite1,圖片資料夾在C:\TESTA\WebSite1\FileUploadDemo





--
前端:
擺至少三個元件如下:
--





--
後端:
簡單地說只有這一小段,註解是安裝的注意事項
--

//上傳excel檔案
    protected void uploadExcelFile()
    {
        //假設主目錄在C:\TESTA\WebSite1,圖片資料夾在C:\TESTA\WebSite1\FileUploadDemo
        String saveDir = "\\WebSite1\\FileUploadDemo\\";
        String appPath = Request.PhysicalApplicationPath;

        if (FileUpload1.HasFile)
        {
            String savePath = appPath + saveDir + FileUpload1.FileName;
            FileUpload1.SaveAs(savePath);
            Label5.Text = "上傳成功,檔名:" + savePath;
        }
        else
        {
            Label5.Text = "請先挑選檔案再上傳";
        }
    }





--
webconfig的部份:
--

   
   

--
執行結果:
成功的話上傳會有檔案,注意有20mb的限制(webconfig)
--




這是連結


2017年4月12日 星期三

[Android]詢問對話框



 這個例子是在主頁面按返回上頁鍵會跳出對話框,確定後離開程式








以下是程式碼:

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_BACK && this.getTitle().equals("") )
    {
        // 創建退出對話框        AlertDialog.Builder d = new AlertDialog.Builder(this)
                .setMessage("確定要退出嗎")
                .setCancelable(false);
        d.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                finish();            }
        });        d.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {

            }
        });        d.show();    }else if(keyCode == KeyEvent.KEYCODE_DEL){

    }else{
        finish();    }
    return false;}



[android]Text字體



https://dotblogs.com.tw/hanry/2012/05/15/72196

若使用Text需要運用到不同字體的時候,可以在Text裡proper找textStyle輸入

正常=normal

粗體=bold

斜體=italic文字

android:textStyle="bold"


以下的例子是實際上可能會看得到的排版,介紹如下:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:orientation="horizontal"    android:paddingBottom="10dp"    android:background="#BBBBBB"    android:layout_height="150dp">
            android:layout_width="150dp"        android:layout_height="150dp"        app:srcCompat="@mipmap/ic_launcher"        android:layout_marginStart="12dp"        android:id="@+id/image"        android:scaleType="centerCrop"        android:layout_alignParentBottom="true"        android:layout_alignParentStart="true" />
            android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">
                    android:text="TextView"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:textStyle="bold"            android:id="@+id/f_name"            android:layout_below="@+id/f_name"            android:layout_alignParentEnd="true" />
                    android:text="TextView"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="5dp"            android:layout_marginLeft="5dp"            android:layout_marginRight="5dp"            android:id="@+id/d_name"            android:layout_alignTop="@+id/image"            android:layout_alignParentEnd="true" />    


[Android]intent事件(兩種寫法)

有兩種intent的事件寫法,記錄在這邊:


1、
第一種是送值到下一個頁面,在下一個頁面收值
(主頁面)送出:
Intent intent=new Intent(this, PhotoDetail.class);intent.putExtra("photo", MainActivity.contactTemp.getPhoto() );this.startActivity(intent);

(頁面二)收值:
Uri imgUri=Uri.parse("file://" + getIntent().getStringExtra("photo"));



2、
第二種寫法是有回傳值的類型,例如這個:

(主頁面)送出:
startActivityForResult(new Intent(MainActivity.this, AddNotes.class), 1);

(主頁面)收回傳值
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle bundleResult = data.getExtras();
        String photo = bundleResult.getString("photo").toString();
    }
}




註:請記得在AndroidManifest.xml加activite,以這個程式為例就是以下的寫法:
android:name=".PhotoDetail">



這邊順便寫一寫如何引用照想功能的api:
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(it,100);
startActivityForResult(it,100);




2017年4月11日 星期二

onclick()在xml的加入教學


這個程式是onclick的筆記
android:onClick="onClick"

public void onClick(View view){
     Toast.makeText(AndroidOnClickActivity.this,
       "Button Clicked", Toast.LENGTH_LONG).show();
    }


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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click Me"
    android:onClick="onClick"
    />
</LinearLayout>




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.AndroidOnClick;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
 
public class AndroidOnClickActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
     
    public void onClick(View view){
     Toast.makeText(AndroidOnClickActivity.this,
       "Button Clicked", Toast.LENGTH_LONG).show();
    }
}