2014-03-28 6 views
0

HashMap<String, String>()을 사용하여 ListView을 채우려고합니다. 나는 최신 뉴스 json 표현을 얻기 위해 내 사이트 API를 가져와야한다. 이제 기본적으로 동적 데이터를 시뮬레이션하고 수동으로 뉴스를 작성하려고합니다. 내가 직면 한 문제는 예상대로 뉴스를 추가하지 않고 반복 만한다는 것입니다. 나쁜 설명에 대해 유감입니다. 여기에 내가 응용 프로그램을 실행할 때 그래서 목록이 제목과 날짜의 마지막 세트로 두 번 채워집니다Android. listView에 HashMap 사용

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

을 명확히 내 코드입니다.

내 사용자 정의 어댑터 코드는 같은 객체에 대한 참조입니다 두 번 map를 추가 때문입니다

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

} 

답변

0

입니다. 처음으로 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); 
+0

대답 주셔서 감사하지만 나는 루프 에서이 일을하기로되어있어. 어떻게해야할까요? –

+0

루프에서 무엇을 의미합니까? 방금 게시 한 코드가 있습니다. 요컨대, 목록에 추가하려는 각지도 개체는 새 개체 여야합니다. 동일한 맵을 다시 사용하지 마십시오. –

1

을하지만, 나는 그가 단지 루프로 해시 맵을 넣어, 루프를 요청 ASK 질문을 요구하고 알고 너도 괜찮을거야. 귀하의 루프 n 카운트, 귀하의 변수에 대한 계산

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

       classList.add(hashMap); 
      }