Android中ListView實現展示列表資料
阿新 • • 發佈:2019-01-10
1、在activity_main.xml中新增一個ListView
<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="wrap_content" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lvList"> </ListView> </LinearLayout>
2、新建一個layout檔案用來作為list的一行格式檔案
3、<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="wrap_content" android:orientation="horizontal" tools:context="${relativePackage}.${activityClass}" > <!-- dip指的是畫素,sp指的是字型大小 --> <TextView android:id="@+id/tvId" android:layout_width="30dip" android:layout_height="wrap_content" android:textSize="25sp" android:gravity="left" /> <TextView android:id="@+id/tvName" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tvAge" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
package com.zlz.androidxml; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import com.zlz.androidxml.domain.Person; import com.zlz.androidxml.service.ParseService; import com.zlz.androidxml.service.PullParseServiceImpl; import com.zlz.androidxml.service.SaxParseServiceImpl; public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnPull).setOnClickListener(this); } @Override public void onClick(View v) { List<Person> persons = new ArrayList<Person>(); for(int i =1;i<4;i++){ Person person = new Person(); person.id = i; person.name = "lizhi"+i; person.age = 12+i; persons.add(person); } popListView(persons); } //將List放到一個grid裡面展示 private void popListView(List<Person> persons) { List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; //需要將物件封裝為一個map的集合 for (Person p : persons) { map = new HashMap<String, Object>(); map.put("id", p.id); map.put("name", p.name); map.put("age", p.age); ls.add(map); } //使用標籤ListView存放 ListView lvList = (ListView) findViewById(R.id.lvList); //將list中每個物件虛擬為一個Item,然後再存入Grid中的每一行,listitemlayout就相當於一個item ListAdapter adapter = new SimpleAdapter(this, ls, R.layout.listitemlayout, new String[] {"id","name","age"}, new int[] {R.id.tvId,R.id.tvName,R.id.tvAge}); lvList.setAdapter(adapter); } }
將檔案放在TableLayout中
1、在activity_main.xml中新增Table_layout
<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="wrap_content"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btnPull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pull" />
</LinearLayout>
<!--strechColumns 保證列平分-->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2"
android:id="@+id/tlLayout"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ID"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NAME"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AGE"
/>
</TableRow>
</TableLayout>
</LinearLayout>
2、在MainActivity中迴圈建立Row然後新增到Table上
</pre><pre name="code" class="java">package com.zlz.androidxml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.zlz.androidxml.domain.Person;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSax).setOnClickListener(this);
findViewById(R.id.btnDom).setOnClickListener(this);
findViewById(R.id.btnPull).setOnClickListener(this);
}
@Override
public void onClick(View v) {
List<Person> persons = new ArrayList<Person>();
for(int i =1;i<4;i++){
Person person = new Person();
person.id = i;
person.name = "lizhi"+i;
person.age = 12+i;
persons.add(person);
}
//獲取TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.tlLayout);
int childrenCount = tl.getChildCount();
// 防止每次查詢重複新增,所以每次拼裝為table時,除了表頭,其他的全部幹掉
for (int i = childrenCount - 1; i > 0; i--) {
View view = tl.getChildAt(i);
tl.removeView(view);
}
while (cursor.moveToNext()) {
TableRow row = new TableRow(this);
TextView idView = new TextView(this);
idView.setText(cursor.getString(cursor.getColumnIndex("id")));
row.addView(idView);
TextView nameView = new TextView(this);
nameView.setText(cursor.getString(cursor.getColumnIndex("name")));
row.addView(nameView);
TextView ageView = new TextView(this);
ageView.setText(cursor.getString(cursor.getColumnIndex("age")));
row.addView(ageView);
// 將每一行新增到table上
tl.addView(row);
}
}
}