android列表顯示
1. android 怎listview一行顯示3個數據
一般的SimpleAdapter就可以啊
private ListView lv1;//布局中的ListView
lv1 = (ListView) findViewById(R.id.sim_list);
List<Map<String,String>> mLists = new ArrayList<Map<String,String>>();
//為列表添加數據(為了方便數據都做成一樣的了)
for(int i=0;i<21;i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("title", "小宗");
map.put("info", "電台DJ");
map.put("age", "21");
mLists.add(map);
}
/**
* SimpleAdapter信息配置
* R.layout.simple_item對應一個ListView顯示的內容布局
* String對應map中放置的鍵值對名稱
* int對應內容布局的控制項id
*/
SimpleAdapter adapter = new SimpleAdapter(Context, mLists,
R.layout.simple_item, new String[] { "title", "info", "age"},
new int[] {R.id.title, R.id.info, R.id.age});
//為ListView添加對應的Adapter
lv1.setAdapter(adapter);
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:orientation="horizontal" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp" />
</LinearLayout>
想加多少,添加就行了
復雜點的就用BaseAdapter,不過單純想加數據的話,用SimpleAdapter簡單
2. android 中listview是怎麼用的
表的顯示需要三個元素:
1.ListVeiw 用來展示列表的View。
2.適配器 用來把數據映射到ListView上的中介。
3.數據 具體的將被映射的字元串,圖片,或者基本組件。
根據列表的適配器類型,列表分為三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最為簡單,只能展示一行字。SimpleAdapter有最好的擴充性,可以自定義出各種效果。SimpleCursorAdapter可以認為是SimpleAdapter對資料庫的簡單結合,可以方面的把資料庫的內容以列表的形式展示出來。
我們從最簡單的ListView開始:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @author allin
*
*/
public class MyListView extends Activity {
private ListView listView;
//private List<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
listView = new ListView(this);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
setContentView(listView);
}
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("測試數據1");
data.add("測試數據2");
data.add("測試數據3");
data.add("測試數據4");
return data;
}
}
上面代碼使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數據,要裝配這些數據就需要一個連接ListView視圖對象和數組數據的適配器來兩者的適配工作,ArrayAdapter的構造需要三個參數,依次為this,布局文件(注意這里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系統定義好的布局文件只顯示一行文字,數據源(一個List集合)。同時用setAdapter()完成適配的最後工作。運行後的現實結構如下 :
SimpleCursorAdapter
sdk的解釋是這樣的:An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。簡單的說就是方便把從游標得到的數據進行列表顯示,並可以把指定的列映射到對應的TextView中。
3. 如何將Android資料庫中表格的某一列在下拉列表中顯示
請參考SDK下APIDemo例子,裡面有詳細的介紹,路徑platforms/android-4(我的是1.6的)/samples/ApiDemos
,打開應用操作步驟如下Views-->List,裡面有很多中列表,你自己選擇;
關於新建item,如果你要實現自己的樣式,可以自定Adapter;你提問中android.R.layout.simple_spinner_item和別人的item是一個作用,就是列表中每一行的樣式,有什麼問題再hi我
4. android 如何讓應用程序在全部應用程序列表裡顯示跟隱藏!
之前有個客戶的需求特別怪,要求應用不在全部應用程序列表裡顯示,通過撥打指定的電話號碼形式啟動應用,開始的想發就是在manifest.xml里的第一個activity里不添加<action android:name=android.intent.action.MAIN / <category android:name=android.intent.category.LAUNCHER /這兩個intentfilter。發現在4.0以上的版本就會出現錯誤!4.0會認為你這個是不安全的信息,無法啟動。後來在PackageManager里發現了setComponentEnabledSetting這個方法,可以達到效果!看代碼吧!
5. android listView 顯示不全,為什麼
重寫LinearLayout顯示列表
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
/***
*
* @author FreePC
*
*/
public class LinearLayoutForListView extends LinearLayout
{
private BaseAdapter adapter;
private OnItemClickListener onItemClickListener;
/**
* 通過 Java代碼 實例化
* @param context
*/
public LinearLayoutForListView(Context context)
{
super(context);
//設置LinearLayoutForListView為垂直布局,否者默認為水平布局,容易疏忽導致子項顯示不全
LinearLayoutForListView.this.setOrientation(LinearLayout.VERTICAL);
}
/**
* 此構造函數可以允許我們通過 XML的方式注冊 控制項
* @param context
* @param attrs
*/
public LinearLayoutForListView(Context context, AttributeSet attrs)
{
super(context, attrs);
LinearLayoutForListView.this.setOrientation(LinearLayout.VERTICAL);
}
/**
* 設置適配器
*
* @param adpater
*/
public void setAdapter(BaseAdapter adpater)
{
this.adapter = adpater;
bindLinearLayout();
}
/**
* 獲取適配器Adapter
*
* @return adapter
*/
public BaseAdapter getAdpater()
{
return adapter;
}
/**
* 綁定布局:將每個子項的視圖view添加進此線性布局LinearLayout中
*/
public void bindLinearLayout()
{
int count = adapter.getCount();
for (int i = 0; i < count; i++)
{
View v = adapter.getView(i, null, null);
if (i != count - 1)
{ //添加每項item之間的分割線
v = addLine(v);
}
addView(v, i);
}
setItemClickListener();
Log.v("countTAG", "" + count);
}
/**
* 添加每項item之間的分割線
*
* @param view
* @return
*/
public View addLine(View view)
{
//分割線view
View lineView = new View(view.getContext());
// 將數據從dip(即dp)轉換到px,第一參數為數據原單位(此為DIP),第二參數為要轉換的數據值
float fPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 0.5, view.getResources().getDisplayMetrics());
int iPx = Math.round(fPx);
LayoutParams layoutParams = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, iPx);
lineView.setLayoutParams(layoutParams);
lineView.setBackgroundColor(view.getSolidColor());
LinearLayout ly = new LinearLayout(view.getContext());
ly.setOrientation(LinearLayout.VERTICAL);
ly.addView(view);
ly.addView(lineView);
return ly;
}
/**
* 設置點擊子項事件監聽對象
* @param onItemClickListener
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener)
{
this.onItemClickListener = onItemClickListener;
setItemClickListener();
}
/**
* 獲取點擊子項事件監聽對象
* @return
*/
public OnItemClickListener getOnItemClickListener()
{
return onItemClickListen