Java 带有复选框、文本视图和按钮的Listview在android中无法正常工作?
我正在创建一个android应用程序,下面给出了我的应用程序的UI。 单击“提交”按钮时,我需要选中的复选框、值和id文本视图 示例大小未选中,cc(单选按钮)已选中。 记录在列表视图中动态填充,但我无法使其工作 复选框\u produoptionactivity.javaJava 带有复选框、文本视图和按钮的Listview在android中无法正常工作?,java,android,for-loop,Java,Android,For Loop,我正在创建一个android应用程序,下面给出了我的应用程序的UI。 单击“提交”按钮时,我需要选中的复选框、值和id文本视图 示例大小未选中,cc(单选按钮)已选中。 记录在列表视图中动态填充,但我无法使其工作 复选框\u produoptionactivity.java public class MainActivity extends Activity implements OnItemClickListener { ListView listview; Ar
public class MainActivity extends Activity implements OnItemClickListener {
ListView listview;
ArrayList<HashMap<String, String>> list_kategori ;
ProgressDialog pd;
ArrayAdapter<Model_checkbox> adapter;
List<String> idprdoptins = new ArrayList<String>();
List<Model_checkbox> nameprdoptn = new ArrayList<Model_checkbox>();
List<Model_checkbox> list = new ArrayList<Model_checkbox>();
Button btn_checkbox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkboxproduct_option);
listview = (ListView)findViewById(R.id.list);
btn_checkbox = (Button)findViewById(R.id.btn_productoption);
// TODO Auto-generated method stub
String id =CheckLogin();
if (id!=""){
loadproductoption(id);
}
btn_checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (Model_checkbox m : list) {
Log.i("Stack1", m.toString());
}
Log.d("Stack1",String.valueOf(Model_checkbox.class));
Toast.makeText(getApplicationContext(), "ada",Toast.LENGTH_LONG).show();
}
});
}
public void loadproductoption(String id) {
listproduct task= new listproduct();
task.execute(id);
}
private class listproduct extends AsyncTask<String, Void, String>{
protected void onPreExecute(){
pd = new ProgressDialog(Checkbox_ProducoptionActivity.this);
pd.setMessage("Please wait..");
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONObject jsonResult = HttpClientCustom.getRequest(Konfigurasi.strUrl+"api-v1/proop?id_user="+ params[0]);
return jsonResult.toString();
}
protected void onPostExecute(String result){
JSONObject jsonResponse=null;
JSONArray jObject=null;
list_kategori = new ArrayList<HashMap<String, String>>();
try {
jsonResponse = new JSONObject(new String(result));
if(jsonResponse.getString("status").equals("SUCCESS")) {
if (!jsonResponse.getString("total").equals("0")){
jObject = jsonResponse.getJSONArray("resultset");
for (int i = 0; i < jObject.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("idprd", jObject.getJSONObject(i).getString("id"));
map.put("id_user", jObject.getJSONObject(i).getString("id_user"));
map.put("namepd", jObject.getJSONObject(i).getString("name"));
setListnama(jObject.getJSONObject(i).getString("name"));
list_kategori.add(map);
}
adapter = new Adapter_Checkboxproductoption(Checkbox_ProducoptionActivity.this, getModel());
listview.setAdapter(adapter);
}
pd.dismiss();
} else if (jsonResponse.getString("status").equals("FAILED")) {
pd.dismiss();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pd.dismiss();
}
private List<Model_checkbox> getModel() {
return nameprdoptn;
}
public void setListnama(String name) {
nameprdoptn.add(new Model_checkbox(name));
}
}
// public class MyAdapter extends ArrayAdapter {
class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
public class Adapter_Checkboxproductoption extends ArrayAdapter<Model_checkbox> {
private final List<Model_checkbox> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public Adapter_Checkboxproductoption(Activity context, List<Model_checkbox> list) {
super(context, R.layout.list_checkbox, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.list_checkbox, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.rowTextView);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
viewHolder.checkbox.setTag(position);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
viewHolder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox checkbox = (CheckBox) v;
int getPosition = (Integer) checkbox.getTag();
list.get(getPosition).setSelected(checkbox.isChecked());
}
});
return convertView;
}
}
can't work in "Checkbox_ProducoptionActivity.java"
for (Model_checkbox m : list) {
Log.i("Stack1", m.toString());
MyAdapter.java
public class MainActivity extends Activity implements OnItemClickListener {
ListView listview;
ArrayList<HashMap<String, String>> list_kategori ;
ProgressDialog pd;
ArrayAdapter<Model_checkbox> adapter;
List<String> idprdoptins = new ArrayList<String>();
List<Model_checkbox> nameprdoptn = new ArrayList<Model_checkbox>();
List<Model_checkbox> list = new ArrayList<Model_checkbox>();
Button btn_checkbox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkboxproduct_option);
listview = (ListView)findViewById(R.id.list);
btn_checkbox = (Button)findViewById(R.id.btn_productoption);
// TODO Auto-generated method stub
String id =CheckLogin();
if (id!=""){
loadproductoption(id);
}
btn_checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (Model_checkbox m : list) {
Log.i("Stack1", m.toString());
}
Log.d("Stack1",String.valueOf(Model_checkbox.class));
Toast.makeText(getApplicationContext(), "ada",Toast.LENGTH_LONG).show();
}
});
}
public void loadproductoption(String id) {
listproduct task= new listproduct();
task.execute(id);
}
private class listproduct extends AsyncTask<String, Void, String>{
protected void onPreExecute(){
pd = new ProgressDialog(Checkbox_ProducoptionActivity.this);
pd.setMessage("Please wait..");
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONObject jsonResult = HttpClientCustom.getRequest(Konfigurasi.strUrl+"api-v1/proop?id_user="+ params[0]);
return jsonResult.toString();
}
protected void onPostExecute(String result){
JSONObject jsonResponse=null;
JSONArray jObject=null;
list_kategori = new ArrayList<HashMap<String, String>>();
try {
jsonResponse = new JSONObject(new String(result));
if(jsonResponse.getString("status").equals("SUCCESS")) {
if (!jsonResponse.getString("total").equals("0")){
jObject = jsonResponse.getJSONArray("resultset");
for (int i = 0; i < jObject.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("idprd", jObject.getJSONObject(i).getString("id"));
map.put("id_user", jObject.getJSONObject(i).getString("id_user"));
map.put("namepd", jObject.getJSONObject(i).getString("name"));
setListnama(jObject.getJSONObject(i).getString("name"));
list_kategori.add(map);
}
adapter = new Adapter_Checkboxproductoption(Checkbox_ProducoptionActivity.this, getModel());
listview.setAdapter(adapter);
}
pd.dismiss();
} else if (jsonResponse.getString("status").equals("FAILED")) {
pd.dismiss();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pd.dismiss();
}
private List<Model_checkbox> getModel() {
return nameprdoptn;
}
public void setListnama(String name) {
nameprdoptn.add(new Model_checkbox(name));
}
}
// public class MyAdapter extends ArrayAdapter {
class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
public class Adapter_Checkboxproductoption extends ArrayAdapter<Model_checkbox> {
private final List<Model_checkbox> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public Adapter_Checkboxproductoption(Activity context, List<Model_checkbox> list) {
super(context, R.layout.list_checkbox, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.list_checkbox, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.rowTextView);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
viewHolder.checkbox.setTag(position);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
viewHolder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox checkbox = (CheckBox) v;
int getPosition = (Integer) checkbox.getTag();
list.get(getPosition).setSelected(checkbox.isChecked());
}
});
return convertView;
}
}
can't work in "Checkbox_ProducoptionActivity.java"
for (Model_checkbox m : list) {
Log.i("Stack1", m.toString());
//公共类MyAdapter扩展了ArrayAdapter{
类视图持有者{
受保护的文本查看文本;
受保护复选框;
}
公共类适配器\u Checkboxproductoption扩展了ArrayAdapter{
私人最终名单;
私人最终活动背景;
布尔checkAll_标志=false;
布尔checkItem_标志=false;
公共适配器\u Checkboxproductoption(活动上下文,列表){
super(上下文,R.layout.list\u复选框,list);
this.context=上下文;
this.list=列表;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder ViewHolder=null;
if(convertView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
convertView=充气机。充气(R.layout.list_复选框,空);
viewHolder=新的viewHolder();
viewHolder.text=(TextView)convertView.findViewById(R.id.rowTextView);
viewHolder.checkbox=(复选框)convertView.findViewById(R.id.CheckBox01);
viewHolder.checkbox.setTag(位置);
convertView.setTag(viewHolder);
}否则{
viewHolder=(viewHolder)convertView.getTag();
}
viewHolder.text.setText(list.get(position.getName());
viewHolder.checkbox.setChecked(list.get(position.isSelected());
viewHolder.checkbox.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
复选框=(复选框)v;
int getPosition=(整数)checkbox.getTag();
list.get(getPosition).setSelected(checkbox.isChecked());
}
});
返回视图;
}
}
无法在“Checkbox\u produoptionactivity.java”中工作
用于(型号复选框m:列表){
Log.i(“Stack1”,m.toString());
任何主体都可以帮助我获取选中复选框的值和id吗?您需要做的就是获取/设置适配器中选中项的值。通过这种方式,我在游标适配器中完成了此操作,您可以根据需要更新以下代码
public class CursorAdapterList extends CursorAdapter {
private LayoutInflater inflater = null;
private DBAdapter db = null;
private Context ctx = null;
public CursorAdapterList(Context context, Cursor c, DBAdapter database) {
super(context, c);
ctx = context;
db = database;
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.txtGameNameInList);
CheckBox favBox = (CheckBox) view.findViewById(R.id.favbuttonInList);
tv.setText(cursor.getString(1));
//here I am getting value of already checked boxes from db
String flag = cursor.getString(3);
if (flag.equals("true")) {
favBox.setChecked(true);
} else {
favBox.setChecked(false);
}
favBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout parentView = (LinearLayout) v.getParent();
TextView clickedText = (TextView) parentView
.findViewById(R.id.txtGameNameInList);
//here you can get the Item value what your selcted in your list
String name = clickedText.getText().toString();
if (((CheckBox) v).isChecked()) {
Toast.makeText(ctx, name + " is Marked as Favorite Game!!",
Toast.LENGTH_SHORT).show();
//below method will store selected item value in db
db.markAsFavorite(name, "true");
} else {
db.markAsFavorite(name, "false");
}
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.list_row_custom, parent, false);
}
}
为了我的活动
public class Checkbox_ProducoptionActivity<country> extends Activity {
ListView list;
static Context mContext;
Button btnSave;
List<String> val = new ArrayList<String>();
ArrayList<HashMap<String, String>> list_kategori ;
ArrayList<String> stock_list = new ArrayList<String>();
ArrayList<String> stock2_list = new ArrayList<String>();
ProgressDialog pd;
EfficientAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkboxproduct_option);
list = (ListView) findViewById(R.id.ListView01);
btnSave = (Button)findViewById(R.id.btnSave);
mContext = this;
final String id =CheckLogin();
if (id!=""){
loadproductoption(id);
}
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences val_shared = getSharedPreferences("CHECKBOX_SHARED", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor cleareditor=val_shared.edit();
cleareditor.clear();
cleareditor.commit();
for (int i = 0; i <list.getCount() ; i++) {
View vListSortOrder;
vListSortOrder=list.getChildAt(i);
try{
TextView textval= (TextView)vListSortOrder.findViewById(R.id.TextView01);
CheckBox ckc=(CheckBox)vListSortOrder.findViewById(R.id.chkbox1);
EditText edit=(EditText)vListSortOrder.findViewById(R.id.txtbox);
if (ckc.isChecked()){
edit.getText().toString();
String temp1 = textval.getText().toString();
Toast.makeText(getApplicationContext(), "fuck"+textval.getText().toString(), Toast.LENGTH_LONG).show();
val.add(temp1);
}
}catch (Exception e) {
// TODO: handle exception
}
}
SharedPreferences customer_ident = getSharedPreferences("CHECKBOX_SHARED", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor=customer_ident.edit();
editor.putString("valuecheck", val.toString());
editor.commit();
// Toast.makeText(getApplicationContext(), "ada "+val, Toast.LENGTH_LONG).show();
Intent prdmenuact = new Intent(getApplicationContext(),CreateManageProductActivity.class);
prdmenuact.putExtra("idaction", "1");
startActivity(prdmenuact);
finish();
}
});
}
public void loadproductoption(String id) {
listproduct task= new listproduct();
task.execute(id);
}
private class listproduct extends AsyncTask<String, Void, String>{
protected void onPreExecute(){
pd = new ProgressDialog(Checkbox_ProducoptionActivity.this);
pd.setMessage("Please wait..");
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONObject jsonResult = HttpClientCustom.getRequest(Konfigurasi.strUrl+"api-v1/proop?id_user="+ params[0]);
return jsonResult.toString();
}
protected void onPostExecute(String result){
JSONObject jsonResponse=null;
JSONArray jObject=null;
list_kategori = new ArrayList<HashMap<String, String>>();
try {
jsonResponse = new JSONObject(new String(result));
if(jsonResponse.getString("status").equals("SUCCESS")) {
if (!jsonResponse.getString("total").equals("0")){
jObject = jsonResponse.getJSONArray("resultset");
for (int i = 0; i < jObject.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("idprd", jObject.getJSONObject(i).getString("id"));
map.put("id_user", jObject.getJSONObject(i).getString("id_user"));
map.put("namepd", jObject.getJSONObject(i).getString("name"));
list_kategori.add(map);
}
adapter = new EfficientAdapter(Checkbox_ProducoptionActivity.this,list_kategori );
list.setAdapter(adapter);
}
pd.dismiss();
} else if (jsonResponse.getString("status").equals("FAILED")) {
pd.dismiss();
}
} catch (JSONException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
pd.dismiss();
}
}
公共类复选框\u produoptionActivity扩展活动{
列表视图列表;
静态语境;
按钮btnSave;
List val=new ArrayList();
ArrayList_kategori;
ArrayList stock_list=新的ArrayList();
ArrayList stock2_list=新的ArrayList();
进展性帕金森病;
高效适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.checkboxproduct_选项);
列表=(ListView)findViewById(R.id.ListView01);
btnSave=(按钮)findviewbyd(R.id.btnSave);
mContext=这个;
最终字符串id=CheckLogin();
如果(id!=“”){
loadproductoption(id);
}
btnSave.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
SharedReferences val_shared=GetSharedReferences(“复选框_shared”,模式_WORLD_WRITEABLE);
SharedReferences.Editor cleareditor=val_shared.edit();
cleareditor.clear();
commit();
对于(int i=0;很抱歉,我的适配器使用ArrayAdapter和list,我不从数据库获取数据,所以我不使用DBAdapter数据库和游标适配器,但感谢您的回答。
for (int i = 0; i <list.getCount() ; i++) {
View vListSortOrder;
vListSortOrder=list.getChildAt(i);
try{
TextView textval= (TextView)vListSortOrder.findViewById(R.id.TextView01);
CheckBox ckc=(CheckBox)vListSortOrder.findViewById(R.id.chkbox1);
EditText edit=(EditText)vListSortOrder.findViewById(R.id.txtbox);
if (ckc.isChecked()){
edit.getText().toString();
String temp1 = textval.getText().toString();
Toast.makeText(getApplicationContext(), "fuck"+textval.getText().toString(), Toast.LENGTH_LONG).show();
val.add(temp1);
}
}catch (Exception e) {
// TODO: handle exception
}
}