wuhudamahua
2016-05-20 13:22
采纳率: 27.3%
浏览 2.6k
已采纳

Android的listview怎样动态改变显示的数量

我做了一个简单的小程序,在一个界面上有一个按钮,然后点击按钮就增加一行listview,但是我
我的程序点击却没有反应,应该怎样解决,这是源代码
package com.example.dianjishiyan;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

ListView lv;
Button bt;
myad adaptper;
List<crime> a=new ArrayList<crime>();
int b=a.size()+1; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv=(ListView) findViewById(R.id.lv);
    bt=(Button) findViewById(R.id.bt);
    adaptper=new myad();
    lv.setAdapter(adaptper);

    }
class myad extends BaseAdapter{

    TextView tv;        
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return b;
    }

    @Override
    public String getItem(int position) {
        return null;
        //return a[position];
    }

    @Override
    public long getItemId(int position) {
            return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout ll=null;
        if(convertView!=null){
            ll=(LinearLayout) convertView;
        }else{
            ll=(LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.textview, null);
        }
        //String b=getItem(position);
        tv=(TextView) ll.findViewById(R.id.textView1);
        tv.setText("textview");
        return ll;
}

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub        
    crime c=new crime();
    a.add(c);
}

}

<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="match_parent"
android:orientation="vertical"
tools:context="com.example.dianjishiyan.MainActivity" >

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />


<Button 
    android:id="@+id/bt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="add"
    />

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_margin="5dp"
    android:gravity="left|center"
    android:textSize="30sp"/>

package com.example.dianjishiyan;

public class crime {

}
图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dev_fred 2016-05-20 16:08
    已采纳

    稍微修改了下你的代码,把显示数据的原理多研究研究

    public class MainActivity extends Activity implements View.OnClickListener {
    ListView lv;
    Button bt;
    MyAdapter adapter;
    List a = new ArrayList();
    //int b=a.size()+1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv=(ListView) findViewById(R.id.lv);
    bt=(Button) findViewById(R.id.bt);
    bt.setOnClickListener(this);
    //初始化一条数据,让listview默认显示的时候有一条数据
    a.add("第1条数据");
    adapter=new MyAdapter(a,MainActivity.this);
    lv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        //点击添加时,给 a 中添加一条数据,利用adapter.notifyDataSetChanged();刷新显示数据
        int lineNum = a.size() + 1;
        a.add("第" + lineNum + "条数据");
        adapter.notifyDataSetChanged();
    }
    
    class MyAdapter extends BaseAdapter {
    
        TextView tv;
        List<String> data;
        Context context;
    
        public MyAdapter(List<String> a,Context c) {
            this.data = a;
            this.context = c;
        }
    
        @Override
        public int getCount() {
            return this.data.size();
        }
    
        @Override
        public String getItem(int position) {
            return a.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CacheView cacheView = null;
            if (convertView == null) {
                cacheView = new CacheView();
                convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.textview, null);
                cacheView.dispalyTextView = (TextView)convertView.findViewById(R.id.textView1);
                convertView.setTag(cacheView);
            } else {
                cacheView = (CacheView) convertView.getTag();
            }
    
            cacheView.dispalyTextView.setText("textview_"+ data.get(position));
            return convertView;
        }
    
        class CacheView{
            TextView dispalyTextView;
        }
    }
    

    }

    打赏 评论
  • danielinbiti 2016-05-20 13:26
     public int getCount() {
            // TODO Auto-generated method stub
            return a.size();//b是不会变的
        }
            或者上面还是b不变,改变下面
            @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub        
        crime c=new crime();
        a.add(c);
            b=a.size();
    }
    
    打赏 评论
  • Grindstone_Fos 2016-05-20 13:34

    还有,在适配器的数据源改变后记得调用一下该适配器的notifydatasetchange方法喔。

    打赏 评论
  • 胖子程序员 2016-05-20 16:23

    记得validate

    打赏 评论
  • RoidCoder 2016-07-26 10:14

    list add数据后记得notifyDatasetchange来刷新listview

    打赏 评论
  • zhtcooler 2016-08-01 01:12

    list.add 方法

    打赏 评论

相关推荐 更多相似问题