Android系统。使用HashMap for listView

时间:2014-03-28 09:56:33

标签: android listview hashmap

我尝试使用HashMap<String, String>()填充ListView。我应该获取我的网站API以获取最新的新闻json表示。到现在为止,我基本上尝试模拟动态数据并手动输入新闻。我面临的问题是,它不会像预期的那样添加新闻,而只是重复它们。对不起,解释不好。这是我的代码澄清

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "ru.rateksib.sendmessage.MESSAGE";

EditText editText;
ListView newsList;
Map<String, String> map;
ArrayList<HashMap<String, String>> NewsArrayList;
NewsArrayAdapter arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View convertView = (View) inflater.inflate(R.layout.news_dialog, null);
    builder.setView(convertView);
    builder.setTitle("Новости компании");

    // set up list view inside alertDialog
    newsList = (ListView) convertView.findViewById(R.id.dialogNewsList);

    // map
    NewsArrayList = new ArrayList<HashMap<String, String>>();
    map = new HashMap<String, String>();


    map.put("title", "New branch opened!");
    map.put("date", "28.03.2014");
    NewsArrayList.add((HashMap<String, String>) map);   

    map.put("title", "Second one!");
    map.put("date", "28.03.2014");
    NewsArrayList.add((HashMap<String, String>) map);


    // custom adapter
    arrayAdapter = new NewsArrayAdapter(NewsArrayList, this);
    newsList.setAdapter(arrayAdapter);

因此,当我运行应用程序时,列表将使用最后一组标题和日期填充两次。

我的自定义适配器代码是

 public class NewsArrayAdapter extends BaseAdapter {

    ArrayList<HashMap<String,String>> names;
    Context ctxt;
    LayoutInflater inflater;

    public NewsArrayAdapter(ArrayList<HashMap<String,String>> newsArrayList, Context c) {
        names = newsArrayList;
        ctxt = c;
        inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return names.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return names.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        // TODO Create the cell (View) and populate it with an element of the array
        if (view == null) {
//          view = inflater.inflate(android.R.layout.simple_list_item_1, viewGroup, false);
            view = inflater.inflate(R.layout.dialog_news_list_item, viewGroup, false);
        }

        TextView title = (TextView) view.findViewById(R.id.title);
        TextView date = (TextView) view.findViewById(R.id.date);
        title.setText(names.get(position).get("title"));
        date.setText(names.get(position).get("date"));
        return view;
    }

}

2 个答案:

答案 0 :(得分:1)

我知道这是老帖子,但我知道问题是问他要求一个循环,只是把你的hashMap放入循环中你会没事的。对于你的循环计数,依靠你的变量

 for(int i = 0; i< n; i++){
                HashMap<String, String> hashMap = new HashMap<>();
                map.put("title", $title);
                map.put("date", $date);

                classList.add(hashMap);
            }

答案 1 :(得分:0)

这是因为你添加了两次map,这是对同一个对象的引用。在您的第一个NewsArrayList.add((HashMap<String, String>) map);之后,您会覆盖titledate的值,这就是您最终使用相同值两次的原因。

要修复,只需创建另一个这样的地图:

// map
NewsArrayList = new ArrayList<HashMap<String, String>>();

map1 = new HashMap<String, String>();
map1 .put("title", "New branch opened!");
map1 .put("date", "28.03.2014");
NewsArrayList.add((HashMap<String, String>) map1);   

map2 = new HashMap<String, String>();
map2.put("title", "Second one!");
map2.put("date", "28.03.2014");
NewsArrayList.add((HashMap<String, String>) map2);