这个程序是有点小题大做。问题是这样的,我程序中没有给listView加上Onclicklistener监听啊,为什么运行后,我随意点击任何一个ListView的items时候都弹出footer呢?
看程序吧:
主程序:
package com.example.NotifyDataSetChangeDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity implements View.OnClickListener{
private List<Article> list;
private ListView listView;
private MyAdapter adapter;
private Button loadBtn, loadMoreBtn,loadingBtn;
private Handler handler;
private LayoutInflater mInflater;
private View footer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
loadBtn.setOnClickListener(this);
loadMoreBtn.setOnClickListener(this);
}
private void initView() {
loadBtn = (Button) findViewById(R.id.load);
loadMoreBtn = (Button) findViewById(R.id.loadMore);
loadingBtn = (Button) findViewById(R.id.loading);
list = new ArrayList<Article>();
listView = (ListView) findViewById(R.id.listView);
adapter = new MyAdapter(list, MyActivity.this);
handler = new Handler();
mInflater = LayoutInflater.from(MyActivity.this);
footer = mInflater.inflate(R.layout.footer, null);
footer.setVisibility(View.GONE);
listView.addFooterView(footer);
}
private void CreateDatas() {
if (list.size()==0){
for (int i = 0; i < 10; i++) {
Article article = new Article();
article.setTitle("I am a dog" + i);
article.setContent("Yes,you are a big dog," + i);
list.add(article);
}
}else {
Toast.makeText(MyActivity.this,"You had loaded the original datas,please stop!",Toast.LENGTH_SHORT).show();
}
}
private void CreateMoreDatas() {
int nowSize = list.size();
for (int i = nowSize; i < nowSize+5; i++) {
Article article = new Article();
article.setTitle("I am new" + i);
article.setContent("you are old man,guy," + i);
list.add(article);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.load:
CreateDatas();
listView.setAdapter(adapter);
break;
case R.id.loadMore:
footer.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
CreateMoreDatas();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
footer.setVisibility(View.GONE);
}
}, 3000);
}
}
}
适配器:
package com.example.NotifyDataSetChangeDemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List<Article> list;
private Context context;
private LayoutInflater mInflater;
public MyAdapter(List<Article> list, Context context) {
this.list = list;
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
convertView = mInflater.inflate(R.layout.items, null);
holder = new ViewHolder(convertView);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.content = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(list.get(position).getTitle());
holder.content.setText(list.get(position).getContent());
return convertView;
}
}
class ViewHolder{
View view;
TextView title;
TextView content;
public ViewHolder(View view) {
this.view = view;
}
}
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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/load"
android:text="加载数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/loadMore"
android:text="加载更多"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
footer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/loading"
android:text="now loading...."
android:textSize="30dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:textSize="25dp"
android:textStyle="bold"
android:maxLines="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/content"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
System problem, I used to use the CM system, but it will be fine if I replace it with the original one. What a mistake
I think you don’t need to look carefully to know that the code you posted will not cause the situation you mentioned. If you change the code, just post the problematic code and discuss it honestly. Seek truth from facts.