【问题标题】:How to set tag the checkbox from a string array android?如何从字符串数组android设置标记复选框?
【发布时间】:2023-03-09 01:12:01
【问题描述】:

我有一个活动,在同一个活动中,我有一个带有复选框的数组适配器类。 如您所见,我从 web 服务获取数据。因此,在解析 json 数据后,我在字符串数组 uid[] 中获得了用户 ID。现在我在 sendinvitesadapter 中有复选框。我必须用字符串数组uid []设置标记复选框。因此,当用户单击第一个复选框时,它应该被分配来自 uid[] 的第一个值等等...... 我怎样才能做到这一点。我知道可以使用 set tag 和 get tag 来完成。但我面临错误。它仅从 uid[] 中获取最后一个值,并且将相同的值分配给所有复选框。

public class qrusers extends ActivityGroup{
    EditText usersearch;
    ArrayList<Item> items = new ArrayList<Item>();
    ListView listView;
    static InputStream is = null;
    final ArrayList<String> list = new ArrayList<String>();
    CheckBox checkBox;
    String result;

    Button addusers;
    private String[] name,email,phone,date,company,combine;
    public static String[] uid;
    CheckBox qrcheckBox;
    sendivitesadapter sendadapter;
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

    }
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.qrusers);
    usersearch=(EditText)findViewById(R.id.usersearch);
    listView=(ListView)findViewById(R.id.qruserlist);
    addusers=(Button)findViewById(R.id.addusers);


    addusers.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


        }

    });

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()){

        saveThread saveTask= new saveThread();
        saveTask.execute(new String[] { "url"});
    }

}
public class sendivitesadapter extends ArrayAdapter<Item>{
    private Context context;
    private ArrayList<Item> items;
    private qrusers qrusers;
    private LayoutInflater vi;
    private String[] array;
    qrusers qrus;

    public sendivitesadapter(Context context,ArrayList<Item> items) {
        super(context, 0,items);

        this.context= context;
        this.qrusers =(qrusers) context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

    @Override
    public Item getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;

            final Item i = items.get(position);

            if (i != null) {
                if(i.isSection()){
                    SectionItem si = (SectionItem)i;
                    v = vi.inflate(R.layout.checkboxlist, null);

                    v.setOnClickListener(null);
                    v.setOnLongClickListener(null);
                    v.setLongClickable(false);

                    final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                    sectionView.setText(si.getTitle());

                }else{
                    sendItem ei = (sendItem)i;
                    v = vi.inflate(R.layout.checkboxlist, null);
                    final TextView title = (TextView)v.findViewById(R.id.contactname);
                    final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
                     checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
                    //checkBox.setTag(position);
                    //items.addAll(uid);


                //    Log.e("IDDDDDDD", text);
                    //checkBox.setTag("12");
                    checkBox.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String s= (String)v.getTag();
                            Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                        }
                    });
                    if (title != null) 
                        title.setText(ei.contactname);
                    if(subtitle != null)
                        subtitle.setText(ei.companyname);

                }
            }
            return v;
        }




}










public void replaceContentView(String id, Intent newIntent) {
    View view = getLocalActivityManager().startActivity(id,
            newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    this.setContentView(view);
}   
class saveThread extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... urls) {
        Context context;
        String response = "";

        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse response1;

            try {
                response1 = client.execute(httpGet);
                HttpEntity entity = response1.getEntity();

                is = entity.getContent();

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"), 8);

                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                result = sb.toString();
                System.out.println(result);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }
        return result;

}

    @Override
    protected void onPostExecute(String result) {
        JSONArray jarray;

        try {
            jarray= new JSONArray(result);

            name= new String[jarray.length()];
            company=new String[jarray.length()];
            uid=new String[jarray.length()];
            for (int i=0;i<jarray.length();i++){



                JSONObject jobj = jarray.getJSONObject(i);
                name[i]=    jobj.getString("Name");
                company[i]=jobj.getString("Company");
                uid[i]=jobj.getString("UserID");
                System.out.println(uid[i]);



                items.add(new sendItem(name[i], company[i], qrcheckBox));

                sendadapter  = new sendivitesadapter(qrusers.this,items);
                listView.setAdapter(sendadapter);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onPostExecute(result);
    }
}
}

【问题讨论】:

  • 你所有的 id 都是唯一的吗?
  • 是的...我从 web 服务获取 id。
  • 尝试对复选框使用 set id,获取 id 以获取值
  • 我不能,因为它是一个字符串数组而不是 int。
  • 你想要将整个数组设置为一个复选框

标签: android arrays checkbox android-arrayadapter


【解决方案1】:
public class sendivitesadapter extends ArrayAdapter<Item>{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
private String[] array;
qrusers qrus;

public sendivitesadapter(Context context,ArrayList<Item> items) {
    super(context, 0,items);

    this.context= context;
    this.qrusers =(qrusers) context;
    this.items = items;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

@Override
public Item getItem(int position) {
    // TODO Auto-generated method stub
    return super.getItem(position);
}

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                sendItem ei = (sendItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);
                final TextView title = (TextView)v.findViewById(R.id.contactname);
                final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
                 checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
            //////////////////////here use the setTag() method////////////////////
                checkBox.setTag(uid[position]);
             ///////////////////////////
                checkBox.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String s= (String)v.getTag();
                        Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                    }
                });
                if (title != null) 
                    title.setText(ei.contactname);
                if(subtitle != null)
                    subtitle.setText(ei.companyname);

            }
        }
        return v;
    }




   }

【讨论】:

  • 我想设置标签 uid[] 到 settag 方法。那么如何设置标签而不是位置呢?
【解决方案2】:
// try to changed this peace of code 
1. pass uid array to adapter
@Override
protected void onPostExecute(String result) {
            JSONArray jarray;

            try {
                jarray= new JSONArray(result);

                name= new String[jarray.length()];
                company=new String[jarray.length()];
                uid=new String[jarray.length()];
                for (int i=0;i<jarray.length();i++){



                    JSONObject jobj = jarray.getJSONObject(i);
                    name[i]=    jobj.getString("Name");
                    company[i]=jobj.getString("Company");
                    uid[i]=jobj.getString("UserID");
                    System.out.println(uid[i]);



                    items.add(new sendItem(name[i], company[i], qrcheckBox));

                }
                sendadapter  = new sendivitesadapter(qrusers.this,items,uid);
                listView.setAdapter(sendadapter);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            super.onPostExecute(result);
}

2.assign udid array as local array
public sendivitesadapter(Context context,ArrayList<Item> items,String[] uIds) {
            super(context, 0,items);

            this.context= context;
            this.qrusers =(qrusers) context;
            this.items = items;
            this.uIds = uIds;
            vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
3.use uid loacl array
checkBox.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
              Log.e("IDDDDDDDDDDDDDDDDDDDDDD", uIds[position]);
          }
});

【讨论】:

  • 还有一个问题,现在我可以将复选框的选中值存储在字符串数组中吗?
  • 实际上我想将值发送到 webservice,所以如果选中了两个复选框,它应该将 id 发送到 webservice
  • oky 在适配器中声明一个哈希映射,并将键作为 id 并将值作为布尔值 true 或 false 初始哈希映射在每个键后缀中具有 false 值,当复选框检查更改每个 id 的反射时,如复选框选中然后放置值为真,否则为假...
猜你喜欢
  • 2021-11-07
  • 1970-01-01
  • 2018-09-03
  • 2014-09-20
  • 1970-01-01
  • 2012-11-05
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多