0

I try to use HashMap<String, String>() to populate ListView. I'm supposed to fetch my site API to get the latest news json representation. By now I basically try to simulate dynamic data and put the news manually. The problem I faced is that it won't add news as expected but just repeats them. Sorry for bad explanation. Here's my code to clarify

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);

So when I run the app the list is populated twice with the last set of title and date.

My custom adapter code is

 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 Answers 2

1

I know this is old post, but I know what the question is asking ask he request for a loop, just put your hashMap into the loop as well and you will be fine. for your loop n count, count on your variables

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

                classList.add(hashMap);
            }
Sign up to request clarification or add additional context in comments.

Comments

0

It's because you add two times map which is a reference to the same object. After your first NewsArrayList.add((HashMap<String, String>) map);, you overwrite the values of title and date, that's why you end up with the same values twice.

To fix, just create another map like this:

// 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);

2 Comments

thanks for your answer but i'm supposed to do this in the loop. How can I go about it?
What do you mean in a loop? I just got your code you posted as an example. The point is, each map object you want to add to the list must be a new object. Don't reuse the same map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.