TexView不在自定义ListView中更新

时间:2017-08-13 03:41:16

标签: android listview

在我的应用程序中,用户按下按钮获得积分,他们使用该分数购买升级。在此列表视图中,用户购买升级,我想显示他们已购买的升级数量。

final ArrayList<HashMap<String, String>>  authorList = new ArrayList<>();


    //Setup adapter
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    listView.setAdapter(customListViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            firstoption = position;


        }
    });
    if (firstoption == 0) {
        nigg++;
        str = Integer.toString(nigg);
        bookPages[firstoption] = str;
    }
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {


                HashMap<String, String> data = new HashMap<>();
                data.put("title", bookTitles[i]);
                data.put("pages", bookPages[i]);
                data.put("author", authors[i]);


                authorList.add(data);
            }
            handler.postDelayed(this, 1000);
        }
    }, 1000);

这是我现在的代码,但由于某种原因它不会更新。我做错了什么?

CustomListView类

package com.example.navjeevenmann.mytycoon;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;


public class CustomListViewAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<HashMap<String, String>> books;
private static LayoutInflater inflater = null;


public CustomListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {

    mContext = context;
    books = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return books.size();
}

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if (convertView == null) {

        view = inflater.inflate(R.layout.program_list, null);

        TextView title = (TextView) view.findViewById(R.id.title);
        TextView author = (TextView) view.findViewById(R.id.textView);
        TextView pages = (TextView) view.findViewById(R.id.pages);
        ImageView image = (ImageView) view.findViewById(R.id.list_image);

        HashMap<String, String> mBook = new HashMap<>();



        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));


    }


    return view;
}
}

5 个答案:

答案 0 :(得分:0)

这就是问题所在:

HashMap<String, String> mBook = new HashMap<>();

这总是为空的,因为每次调用getView时都会重新初始化mBook,因此在每次填充后都会重置数据结构。

您已将主类中的数据HashMap作为HashMap的“books”列表传递到您的列表中。只需在mBooks声明后添加此内容

mBooks = books.get(position);

另请阅读listview / arrayadapter如何工作here

您可能还有一个问题,即您没有使用数据填充HashMap。在这里做一些调试

Data.put("title", bookTitles[i]);
System.out.println(bookTitles[i]);

如果你打印了任何东西,请告诉我。

答案 1 :(得分:0)

可以在AdapterClass的getView()方法中进行更改。在textview

上设置文本之前结束你的条件
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if (convertView == null) {

        view = inflater.inflate(R.layout.program_list, null);

        TextView title = (TextView) view.findViewById(R.id.title);
        TextView author = (TextView) view.findViewById(R.id.textView);
        TextView pages = (TextView) view.findViewById(R.id.pages);
        ImageView image = (ImageView) view.findViewById(R.id.list_image);



}
        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));





    return view;
}
}

关于如何将数据发送到适配器只需在这样的代码中进行更改...在arraylist中设置值后设置适配器

final ArrayList<HashMap<String, String>>  authorList = new ArrayList<>();


handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {


                HashMap<String, String> data = new HashMap<>();
                data.put("title", bookTitles[i]);
                data.put("pages", bookPages[i]);
                data.put("author", authors[i]);


                authorList.add(data);
            }
            handler.postDelayed(this, 1000);
        }
    }, 1000);

    //Setup adapter
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    listView.setAdapter(customListViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            firstoption = position;


        }
    });
    if (firstoption == 0) {
        nigg++;
        str = Integer.toString(nigg);
        bookPages[firstoption] = str;
    }

希望这会对你有所帮助......如果没有,请告诉我

答案 2 :(得分:0)

不要使用

View view = convertView;

因为您的convertView为null,请执行此操作

View view = inflater.inflate(R.layout.your_layout,parent,false);

HashMap<String, String> mBook = authorList.get(position);
text.setText(mBook.get("your_key");

答案 3 :(得分:0)

问题是您是要向authorList添加数据但不通知适配器。     所以内部处理程序在添加数据时只需通知适配器,以便适​​配器可以更新..

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {


                    HashMap<String, String> data = new HashMap<>();
                    data.put("title", bookTitles[i]);
                    data.put("pages", bookPages[i]);
                    data.put("author", authors[i]);


                    authorList.add(data);
                    customListViewAdapter.notifyDataSetChanged();

                }
                handler.postDelayed(this, 1000);
            }
        }, 1000);

希望它能奏效。

答案 4 :(得分:0)

  1. 更改getView代码,如下所示。
  2. 添加ViewHolder以获得更好的性能。

    public class CustomListViewAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<HashMap<String, String>> books;
        private static LayoutInflater inflater = null;
        private ViewHolder mHolder;
    
       public CustomListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {
    
       mContext = context;
       books = data;
       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
       }
    
    
      @Override
      public int getCount() {
        return books.size();
      }
    
      @Override
      public Object getItem(int position) {
         return position;
      }
    
      @Override
      public long getItemId(int position) {
        return position;
      }
    
      static class ViewHolder {
        TextView title;
        TextView author;
        TextView pages;
        ImageView image;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
    
        View view = convertView;
    
        if (convertView == null) {
    
            view = inflater.inflate(R.layout.program_list, null);
            mHolder = new ViewHolder();
            mHolder.title = (TextView) view.findViewById(R.id.title);
            mHolder.author = (TextView) view.findViewById(R.id.textView);
            mHolder.pages = (TextView) view.findViewById(R.id.pages);
            mHolder.image = (ImageView) view.findViewById(R.id.list_image);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        HashMap<String, String> mBook = books.get(position);
        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));
        return view;
     }
    }