0%

这里指的Spinner显示的样式,并不是指在点击Spinner后弹出的选择框的样式,而是直接显示在屏幕上的那个按钮的样式。我有个项目需要使用Spinner,但使用默认的样式又与现有UI不匹配,需要稍稍改一下。 这个样式其实是由适配器指定的,如:

ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,list);

这里指定使用android.R.layout.simple_spinner_item,我们打开这个文件(当然,你要先下载Android源码),发现里面其实就是一个TextView,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" />

现在知道怎么做了吧?自己写个xml,加上一个TextView,id也是@android:id/text1,其他的就看你自己需要了。

只要两行代码:

StringSelection stsel = new StringSelection("String");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);

这两个类是awt中的,但在命令行程序中也可以直接使用

ResultSet没有直接的返回记录数量的方法,一直以为getFetchSize()能拿到返回的记录数量,其实不行。用下面的方法

rs.last(); 
int size = rs.getRow(); 
rs.beforeFirst();

原理:因为行数是从1开始的,所以最后一行的行数就是纪录总数。先移到最后一行,取行数,再移回第一行前。如果没有纪录,返回0

使用upper或lower函数,如,查询表table1,条件是actor字段模糊查询 select * from table1 where upper(actor) like '%AAA%'; 当然,我们得先在程序中把like的值也全部转为大写

使用Collections.sort(ArrayList list)方法。 ArrayList中的类型必须实现Comparable接口,重写compareTo方法,根据需要返回对比结果。如:

class People implements Comparable {
    //必须实现Comparable接口
    String name;
    int age;
 
    People(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    @Override
    public int compareTo(Object o) {
        //重写Comparable接口的compareTo方法 
        //先根据age排序,如果age相同,再根据name排序
        People p = (People) o;
        int result;
        if (age > p.age) {
            result = 1;
        } else if (age == p.age) {
            result=0;
        } else {
            result = -1;
        }
        if(result==0){
            result=name.compareTo(p.name);
        }
        return result;
    }
 
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Name:"+name+" Age:"+age;
    }
 
}

据说在下载远程HTML文件时使用Curl会比file_get_contents高效一点。

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
//设置URL,可以放入curl_init参数中
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1");
//设置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。 如果不加,即使没有echo,也会自动输出
$content = curl_exec($ch); 
//执行
curl_close($ch); 
//关闭
echo $content;

JSONObject不像JSONArray 可以通过索引来得到子元素,要得到子元素必须知道key,但是有时,key是个变量,我们根本就不知道key是什么值,这就需要迭代。

JSONObject obj=month.getJSONObject(i);
for (Iterator iter = obj.keys(); iter.hasNext();) {
String key = (String)iter.next();
JSONArray m=obj.getJSONArray(key);
}

2011.11.29,加上PHP的版本:

    $rely=json_decode($content)->rely;
    foreach ($rely as $value) {
        foreach ($value as $key=>$value2) {
            echo "$key"+"<br>";
            foreach ($value2 as $value3) {
                    echo $value3;
            }
        }
    }

JSON内容为: "rely":[{"2011":[10,11]}]

我在ExpandableListView下方放了一个TextView,但是当组全部展开,ExpandableListView的高度超过屏幕高度时,不管你怎么划屏幕,下面的TextView就是看不到,被隐藏了,ListView也有类似的问题,只要高度超过屏幕高度,其下方的控件就无法显示,而放在ExpandableListView上方的控件则固定在那,滚动ExpandableListView,它也不变,我希望的是它随着ExpandableListView的滚动隐藏掉.我想到的第一个办法就是把所有控件都放进ScrollView里,但事实证明不可行.翻看API文档,发现有个addFooterView和addHeaderView方法,问题解决了,把ExpandableListView上方的所有控件放进一个Layout里,再用addHeaderView方法加进ExpandableListView,所有ExpandableListView下方的控件放进一个Layout,用addFooterView方法添加进ExpandableListView.

ExpandableListView效果相当于一个分组的ListView,点击组,会收缩或展开该组下的子元素,如下图: ExpandableListView的用法与ListView和GridView,Gallery 类似,都是通过一个Adapter来显示. main.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">
    <ExpandableListView android:id="@+id/elv" android:indicatorRight="160dp"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
    </ExpandableListView>
    <!-- indicatorRight 设置那个小图标右边缘与 ExpandableListView左边缘的间距-->
</LinearLayout>

ElvAdapter.java

package com.test;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class ElvAdapter extends BaseExpandableListAdapter
{
    
    ArrayList objs;
    Context context;
    ElvAdapter(Context context,ArrayList objs)
    {
        this.objs=objs;
        this.context=context;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return objs.get(groupPosition).childs.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        //子元素的View
        TextView tv=new TextView(context);
        tv.setText(objs.get(groupPosition).childs.get(childPosition));
        tv.setLayoutParams(new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,ExpandableListView.LayoutParams.WRAP_CONTENT));
        return tv;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        // TODO Auto-generated method stub
        return objs.get(groupPosition).childs.size();
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        // TODO Auto-generated method stub
        return objs.get(groupPosition);
    }

    @Override
    public int getGroupCount()
    {
        // TODO Auto-generated method stub
        return objs.size();
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        //分组的View
        TextView tv=new TextView(context);
        tv.setText(objs.get(groupPosition).groupName);
        ExpandableListView.LayoutParams params=new ExpandableListView.LayoutParams(ExpandableListView.LayoutParams.FILL_PARENT,60);
        tv.setLayoutParams(params);
        return tv;
    }

    @Override
    public boolean hasStableIds()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return true;
    }
    

}
class ElvObject{
    String groupName="";
    ArrayList childs=new ArrayList();
    ElvObject(String groupName,ArrayList childs)
    {
        this.groupName=groupName;
        this.childs=childs;
    }
}

注意下面还有一个ElvObject类 主程序AndroidTestActivity.java

package com.test;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class AndroidTestActivity extends Activity
{
    /** Called when the activity is first created. */
    ExpandableListView elv;
    ElvAdapter adapter;
    ArrayList objs=new ArrayList();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        elv=(ExpandableListView)findViewById(R.id.elv);
        adapter=new ElvAdapter(this,objs);
        elv.setAdapter(adapter);
        ArrayList list=new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        objs.add(new ElvObject("英文",list));
        ArrayList list2=new ArrayList();
        list2.add("111");
        list2.add("222");
        list2.add("333");
        list2.add("444");
        objs.add(new ElvObject("数字",list2));
 
    }
}