1

以下是我当前用于 listView 的代码:

private class Asyncprojectlist extends AsyncTask<Void, Void, Void>{
        JSONArray JSar;
        JSONObject JSob;
        String project_title, project_sector;
        @Override
        protected void onPreExecute() {
            progressDialog =  new ProgressDialog(Allprojects.this);
            progressDialog.setCancelable(false);
            progressDialog.setMessage("Loading...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setProgress(0);
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            UserFunctions user = new UserFunctions();
            JSob = user.allprojects();
            try {
                JSar = JSob.getJSONArray("data");

            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            for(int i=0; i<JSar.length(); i++){
                try {
                    JSONObject newobj = JSar.getJSONObject(i);
                    project_title = newobj.getString("title");
                    project_sector = newobj.getString("sector");

                    HashMap<String, String> single = new HashMap<String, String>();
                    single.put("title", project_title);
                    single.put("sector", project_sector);

                    all_list.add(single);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
//          Log.i("json object", JSar.toString());
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            ListAdapter adapter = new SimpleAdapter(getApplicationContext(), all_list, R.layout.projectlist_frame, 
                    new String[]{"title","sector"}, new int[]{R.id.projecttitle, R.id.projectsector});
            setListAdapter(adapter);
            progressDialog.dismiss();
            list = getListView();
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View viewClicked,
                        int position, long id) {

                    Intent toindividual = new Intent(Allprojects.this, Individual_project.class);
                    try {
                        toindividual.putExtra("nid", JSar.getJSONObject(position).getString("id"));
                        startActivity(toindividual);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

}

这里调用了一个 api 来获取数据,并且它是all_list一个ArrayList<HashMap<>>用于SimpleAdapter分配值的位置,例如布局上的projectTitle和。这里的一切都很完美。但是我很难实现listView布局中的监听器。我相信需要一个自定义适配器。在我尝试构建自定义适配器时,我想不出一个工作代码。请帮助实现自定义适配器,以便 listView 可以实现复选框。projectStatuslistViewcheckBoxCheckChangeListener

4

2 回答 2

8

您可以为它创建一个自定义适配器。在此适配器中,您必须CheckChangeListener在方法checkbox中设置。getView

public class MyBaseAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;

    public MyBaseAdapter(Context context) {

        this.mContext = context;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.row_layout, null);

            holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
            holder.chkTick = (CheckBox) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final int pos = position;
        holder.txtName.setText("ABC");
        holder.chkTick.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    Toast.makeText(mContext, "Checked", Toast.LENGTH_SHORT).show(); 
                }
            }
        });

        return convertView;
    }
}

您也可以查看此链接 1链接 2了解更多详情

您可以将 hashMap 用于自定义适配器并按UI如下方式设置值

public class HashMapAdapter extends BaseAdapter {

    private HashMap<String, String> mData = new HashMap<String, String>();
    private String[] mKeys;
    public HashMapAdapter(HashMap<String, String> data){
        mData  = data;
        mKeys = mData.keySet().toArray(new String[data.size()]);
    }

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

    @Override
    public Object getItem(int position) {
        return mData.get(mKeys[position]);
    }

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

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        String key = mKeys[pos];
        String Value = getItem(pos).toString();

        //do your view stuff here

        return convertView;
    }
}
于 2013-08-19T08:25:20.750 回答
0

尝试使用设置列表适配器

ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(adapter);

代替

setListAdapter(adapter);
于 2015-07-14T13:19:44.830 回答