simpleadapter效果不出
按老师的打上去,只不过把main.xml写到了之前写好的viewpager里的一个页卡下,没有效果,没有报错,不知道我哪里不对
按老师的打上去,只不过把main.xml写到了之前写好的viewpager里的一个页卡下,没有效果,没有报错,不知道我哪里不对
2016-04-20
你把这两个文件(activity_main.xml和MainActivity.java)和你的对比一下,看看少些了哪些
MainActivity.java
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements OnItemClickListener, OnScrollListener {
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
String[] arrData = new String[] { "apple", "banana", "orange", "pear" };
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrData);
listView.setAdapter(arrayAdapter);
dataList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 20; i ++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_lena);
map.put("text", "Item number : " + i);
dataList.add(map);
}
simpleAdapter = new SimpleAdapter(this, dataList, R.layout.item, new String[] {"pic", "text"}, new int[] {R.id.pic, R.id.text});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
String scrollStatString = "";
switch (scrollState) {
case SCROLL_STATE_FLING:
scrollStatString = "用户手指离开屏幕之前,由于用力滑了一下,视图仍在继续滑动";
Map<String, Object> map = new HashMap<String, Object>();
map.put("pic", R.drawable.ic_lena);
map.put("text", "新增项");
dataList.add(map);
simpleAdapter.notifyDataSetChanged();
break;
case SCROLL_STATE_IDLE:
scrollStatString = "视图已经停止滑动";
break;
case SCROLL_STATE_TOUCH_SCROLL:
scrollStatString = "手指没有离开屏幕,试图正在滑动";
break;
}
Toast.makeText(this, scrollStatString, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = "position=" + position + " item=" + listView.getItemAtPosition(position);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
MainActivity.java
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android1:id="@+id/listView1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
</ListView>
</LinearLayout>
举报