我的listView只显示一条3的记录

时间:2013-12-03 07:56:10

标签: android listview android-listview

我有一个列表视图,它总是只显示一条记录,我已经跟踪了我的程序并发现我的arrayList中有3条记录,我将它传递给listView适配器。在跟踪到列表视图的getView之后,我发现它执行了3次,位置为0!
我不知道有什么问题,如果可以,请帮帮我 activity_news.xml:

<RelativeLayout 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:background="@color/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".News" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ListView
            android:id="@+id/lvNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp" />
    </ScrollView>

</RelativeLayout>

这是listView的行模板:news_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/message_row_style"
    android:layout_gravity="right"
    android:gravity="right"
    android:orientation="vertical" >


    <LinearLayout 
        android:layout_marginTop="50dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">
    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/dark_purple"
        android:layout_gravity="right"
        android:gravity="right"
        android:background="@color/red"
         />

    <TextView
        android:id="@+id/txtDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/dark_purple"
        android:layout_gravity="right"
        android:background="@color/blue"
        android:gravity="right" />
    </LinearLayout>
</LinearLayout>

newsAdapter.java:

public class NewsAdapter extends BaseAdapter{

    private Context context;
    private ArrayList<News> list;
    private  Activity activity;

    public NewsAdapter(Context c,Activity activity,ArrayList<News> l) {
        Log.d("Ehsan", "News Adapter Constructor");
        context = c;
        list = l;
        this.activity = activity;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.news_row, parent,false);
        TextView title =  (TextView) row.findViewById(R.id.txtTitle);
        TextView date = (TextView) row.findViewById(R.id.txtDate);

        News temp = list.get(position);
        title.setText(temp.getTitle());
        date.setText(temp.getsDate());

        title.setTag(temp.getid());

        title.setOnClickListener(onClickListener);
        return row;
    }   

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            String date = null,title = null,description = null;
            int id=1;
            Intent i = null;
            News news = new News(activity);
            news.load(id);
            date = news.getsDate()+" - ";
            title = news.getTitle();
            description = news.getDescription();

            i = new Intent(activity,ShowInfo.class);
            i.putExtra("date", date);
            i.putExtra("title", title);
            i.putExtra("description", description);
            activity.startActivity(i);
            activity.finish();
        }
    };
}

2 个答案:

答案 0 :(得分:1)

在getView中进行以下更改

 public View getView(int position, View convertView, ViewGroup parent) {
           if(convertView == null){
             ViewHolder holder=new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView  = inflater.inflate(R.layout.news_row, parent,false);
            TextView title =  (TextView) row.findViewById(R.id.txtTitle);
            TextView date = (TextView) row.findViewById(R.id.txtDate);
             convertView.setTag(holder);
            }
            ViewHolder hold=(ViewHolder)convertView.getTag();
            News temp = list.get(position);
            hold.title.setText(temp.getTitle());
            hold.date.setText(temp.getsDate());
            title.setOnClickListener(onClickListener);
            return row;
        }

在适配器文件中添加此类

static class ViewHolder
        {

            TextView title;
            TextView date;

        }

答案 1 :(得分:0)

你应该在适配器的构造函数中调用super来更改你的适配器,如下所示:

public class NewsAdapter extends ArrayAdapter<News>{

    private Context context;
    private ArrayList<News> list;
    private  Activity activity;

    public NewsAdapter(Context c,Activity activity,ArrayList<News> l) {
        super(c,-1,l);//<-- **pass your item list to super**
        Log.d("Ehsan", "News Adapter Constructor");
        context = c;
        list = l;
        this.activity = activity;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.news_row, parent,false);
        TextView title =  (TextView) row.findViewById(R.id.txtTitle);
        TextView date = (TextView) row.findViewById(R.id.txtDate);

        News temp = list.get(position);
        title.setText(temp.getTitle());
        date.setText(temp.getsDate());

        title.setTag(temp.getid());

        title.setOnClickListener(onClickListener);
        return row;
    }   

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            String date = null,title = null,description = null;
            int id=1;
            Intent i = null;
            News news = new News(activity);
            news.load(id);
            date = news.getsDate()+" - ";
            title = news.getTitle();
            description = news.getDescription();

            i = new Intent(activity,ShowInfo.class);
            i.putExtra("date", date);
            i.putExtra("title", title);
            i.putExtra("description", description);
            activity.startActivity(i);
            activity.finish();
        }
    };
}