please tell me how to set the check box on my listview i am already write code for checkbox but it cant show me on my listview
Spinner spin;
ListView lvl;
ArrayAdapter<String> adapter;
Context context;
ArrayList<String> data;
ArrayList<String> ItemsList;
String selectedFromList;
ArrayAdapter<String> adapter_subjects;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
data = new ArrayList<String>();
spin = (Spinner) findViewById(R.id.spinners);
context = this;
adapter_subjects = new ArrayAdapter<String>(spin.this,
android.R.layout.simple_spinner_item, data);
adapter_subjects
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_subjects);
adapter_subjects.clear();
new asynctaskLoadsubjects().execute();
/* lvl =(ListView)findViewById(R.id.list); */
ItemsList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(spin.this,
android.R.layout.simple_list_item_1, ItemsList);
lvl = (ListView) findViewById(R.id.list);
lvl.setAdapter(adapter);
adapter.clear();
new asyncFetchAllItems().execute();
lvl.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedFromList = (String) (lvl.getItemAtPosition(position));
Intent i = new Intent(getApplicationContext(),orders.class);
i.putExtra("Item name", selectedFromList);
startActivity(i);
Toast.makeText(getApplicationContext(), selectedFromList,
Toast.LENGTH_SHORT).show();
}
});
}
public class asynctaskLoadsubjects extends
AsyncTask<Void, JSONObject, ArrayList<hotelitem>> {
ArrayList<hotelitem> hotelitem = null;
@Override
protected ArrayList<hotelitem> doInBackground(Void... params) {
// TODO Auto-generated method stub
RestAPI api = new RestAPI();
try {
JSONObject jsonObject = api.Getitem();
JSONParser parser = new JSONParser();
hotelitem = parser.parseItemss(jsonObject);
} catch (Exception e) {
Log.d("AsyncLoadVehicleCompanies", e.getMessage());
}
return hotelitem;
}
@Override
protected void onPostExecute(ArrayList<hotelitem> result) {
for (int i = 0; i < result.size(); i++) {
data.add(result.get(i).getItemtype());
}
adapter_subjects.notifyDataSetChanged();
}
}
public class asyncFetchAllItems extends
AsyncTask<Void, JSONObject, ArrayList<ItemTable>> {
ArrayList<ItemTable> ItemTable = null;
@Override
protected ArrayList<ItemTable> doInBackground(Void... params) {
// TODO Auto-generated method stub
RestAPI api = new RestAPI();
try {
JSONObject jsonObject = api.Getallitems();
JSONParser parser = new JSONParser();
ItemTable = parser.parseitemdetails(jsonObject);
} catch (Exception e) {
Log.d("AsyncLoadVehicleCompanies", e.getMessage());
}
return ItemTable;
}
@Override
protected void onPostExecute(ArrayList<ItemTable> result) {
pDialog.dismiss();
for (int i = 0; i < result.size(); i++) {
ItemsList.add(" \n Item Name :" + result.get(i).getItemname()
+ "\n" + "\n price :"
+ result.get(i).getItemprice() + "\n");
}
adapter.notifyDataSetChanged();
Toast.makeText(context, "Loading Completed", Toast.LENGTH_SHORT)
.show();
}
}
public class MyCustomAdapter extends ArrayAdapter<checkTable> {
public ArrayList<checkTable> itemsList;
public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<checkTable> itemsList) {
super(context, textViewResourceId, itemsList);
this.itemsList = new ArrayList<checkTable>();
this.itemsList.addAll(itemsList);
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.abb_list, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
checkTable country = (checkTable) cb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
country.setSelected(cb.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
checkTable checkTable = itemsList.get(position);
holder.code.setText(" (" + checkTable.getitemprice() + ")");
holder.name.setText(checkTable.getitemname());
holder.name.setChecked(checkTable.isSelected());
holder.name.setTag(checkTable);
return convertView;
}
}
}
i am using reference of this site
please help me dear friends i am using this example for my example
Aucun commentaire:
Enregistrer un commentaire