android 点击列表,动态显示对应的内容
发布网友
发布时间:2022-05-14 00:52
我来回答
共4个回答
热心网友
时间:2023-11-15 01:44
android点击列表,动态显示对应的内容,可以点击之后,跳转到详情页面进行显示,主要是用intent进行跳转,也可以携带数据bundle,key-value键值对来保存。代码如下:
public class HandlerDemo extends Activity implements OnScrollListener {
private ListView mListView;
LinearLayout loadingLayout;
private Thread mThread;
private ListViewAdapter adapter;
private int startIndex = 1;// 从第1条开始
private int size = 10;// 每次下载十条数据
private List<News> newsList;
List<Map<String, String>> data ;
/*
* 设置布局显示属性
*/
private LayoutParams mLayoutParams = new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
private LayoutParams ffLayoutParams = new LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.news_main);
data=new ArrayList<Map<String, String>>();
addView();
}
private void addView() {
if (startIndex == 1) {
newsList = new ArrayList<News>();
newsList = getNewsList();
}
getdata(newsList);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
progressBar = new ProgressBar(this);
layout.addView(progressBar, mLayoutParams);
TextView textView = new TextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, ffLayoutParams);
layout.setGravity(Gravity.CENTER);
loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout, mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
// 得到一个ListView用来显示条目
mListView = (ListView) findViewById(R.id.listView);
mListView.addFooterView(loadingLayout);
adapter = new ListViewAdapter();
mListView.setAdapter(adapter);
mListView.setOnScrollListener(this);
mListView.setTextFilterEnabled(true);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (mThread == null || !mThread.isAlive()) {
mThread = new Thread() {
@Override
public void run() {
newsList = new ArrayList<News>();
newsList = getNewsList();
getdata(newsList);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
}
};
mThread.run();
}
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 1) {
startIndex = startIndex + size;
Log.v("startindex", startIndex + "");
mListView.removeFooterView(loadingLayout);
mThread.stop();
adapter.count += size;
adapter.notifyDataSetChanged();
return;
}
}
};
class ListViewAdapter extends BaseAdapter {
int count = 10;
@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.news_item, null);
TextView textView = (TextView) convertView
.findViewById(R.id.textNewsTitle);
textView.setText((data.get(position)).get("title"));
return convertView;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
private List<Map<String, String>> getdata(List<News> list) {
if (list == null)
return null;
for (News news : list) {
Map<String, String> map = new HashMap<String, String>();
map.put("title", news.getTitle());
data.add(map);
}
return data;
}
/*
* 获取网络数据 注:我是访问本机的一个新闻服务,使用asp.net技术来实现的
* 这个是项目是一个基于android的资讯播报软件
*/
private List<News> getNewsList() {
String path = "http://10.0.2.2/getNewsList.aspx";
String xmlStr = "<?xml version='1.0' encoding='utf-8'?><source><categoryIds>1,3,7</categoryIds><startIndex>"
+ startIndex
+ "</startIndex><detail>2</detail><count>"
+ size
+ "</count></source>";
NewsConnector newsConnector = new NewsConnector();
List<News> list = new ArrayList<News>();
list = newsConnector.getNewsList(path, xmlStr);
return list;
}
}
热心网友
时间:2023-11-15 01:45
你现在你的内容和标提自己本身应该是有关联的吧?
不然你也没法根据标题找内容啊。我建议你直接把你的内容和标题存放在一个实体类中,用实体类的标题显示,你点击这个标题直接读取实体类里面的内容,这样不用你再去根据标题找一次内容。
热心网友
时间:2023-11-15 01:45
dreamwever cs6 有移动手机网站模板你可以参考i下!
热心网友
时间:2023-11-15 01:46
多研究下listView吧.推荐使用adapter继承BaseAdapter.
用好listView,这个就简单了.