0

我正在使用地图将值传递给自定义适配器。如何获取自定义适配器中的值。我的应用程序显示空白屏幕,没有任何错误。

文件

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

public class archive extends Fragment {
    public archive() {
    }

    private archiveadapter tableRowAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("ListFragment", "onCreate()");
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.v("ListFragment", "onActivityCreated().");
        Log.v("ListsavedInstanceState", savedInstanceState == null ? "true"
                : "false");

        // Generate list View from ArrayList
        displayListView();

    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater
                .inflate(R.layout.moderationrequests, container, false);

        return v;
    }

    private void displayListView() {

        List<String> dialogtitle = new ArrayList<String>();
        dialogtitle
                .add("Thief nabbed selling stolen mower to owner: Stupid, Clever or Calculated?");
        dialogtitle
                .add("Will software development as a skilled profession ever become obsolete and possible by the average person?");

        List<String> membername = new ArrayList<String>();
        membername.add("By Pegasus");
        membername.add("By Andrew");

        List<String> postedon = new ArrayList<String>();
        postedon.add("6 months ago on Current Affairs");
        postedon.add("6 months ago on Current Affairs");

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        int count = dialogtitle.size();
        for (int i = 0; i < dialogtitle.size(); i++) {
            map = new HashMap<String, String>();
            map.put("dialogtitle", dialogtitle.get(i));
            map.put("membername", membername.get(i));
            map.put("postedon", postedon.get(i));
            list.add(map);
        }


        tableRowAdapter = new archiveadapter(getActivity(),
                R.layout.dialog_archive_list, list);
        ListView listView = (ListView) getView().findViewById(R.id.list_view);

        listView.setAdapter(tableRowAdapter);


    }
}

适配器

   public class archiveadapter extends ArrayAdapter< List<Map<String,String>>> {
        private final Context context;
        private final List<Map<String, String>> names;
        private Button deleteButton = null;

        public archiveadapter(Context context, int itemResId,  List<Map<String,String>> names) {
            super(context, itemResId);
            this.context = context;
            this.names = names;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // LayoutInflater inflater = context.getLayoutInflater();
            // View rowView = inflater.inflate(R.layout.mod_req_list, null, true);

            View rowView = convertView;
            if (rowView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                rowView = inflater.inflate(R.layout.dialog_archive_list, null);

            }
            TextView archivetitle = (TextView) rowView
                    .findViewById(R.id.dialogarchive_title);

            Map<String, String> map = names.get(position);
            //String link = map.get("Link");


            archivetitle.setText(map.get("dialogtitle"));
            TextView membername = (TextView) rowView.findViewById(R.id.membername);
            membername.setText(names.get(1).toString());
            TextView votes = (TextView) rowView.findViewById(R.id.postedon);
            votes.setText(names.get(2).toString());

            return rowView;
        }

    }

有什么想法或建议吗?谢谢,

4

1 回答 1

1

你有这个

 map.put("dialogtitle", dialogtitle.get(i));
 map.put("membername", membername.get(i));
 map.put("postedon", postedon.get(i));

在适配器覆盖中getCount()

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

getView

  Map<String, String> map = names.get(position);
  archivetitle.setText(map.get("dialogtitle"));
  TextView membername = (TextView) rowView.findViewById(R.id.membername);
  membername.setText(map.get("membername"));
  TextView votes = (TextView) rowView.findViewById(R.id.postedon);
  votes.setText(map.get("postedon"));

编辑:

最好使用 aViewHolder来实现平滑滚动和性能。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // LayoutInflater inflater = context.getLayoutInflater();
        // View rowView = inflater.inflate(R.layout.mod_req_list, null, true);
        ViewHolder vh = new ViewHolder();
       // View rowView = convertView;
        if (convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(context);
            convertView= inflater.inflate(R.layout.dialog_archive_list, null);
            vh.archivetitle = (TextView) convertView.findViewById(R.id.textView1);
            vh.membername = (TextView) convertView.findViewById(R.id.textView2);
            vh.votes = (TextView) convertView.findViewById(R.id.textView3);
            convertView.setTag(vh);
        }


    else
    {
        vh= (ViewHolder) convertView.getTag();

    }
        Map<String, String> map = names.get(position);
        vh.archivetitle.setText(map.get("dialogtitle"));
        vh.membername.setText(map.get("membername"));
        vh.votes.setText(map.get("postedon"));

        return convertView;
    }
     static class ViewHolder
        {
            TextView archivetitle,membername,votes;
        }
于 2013-07-12T05:22:04.817 回答