I have implemented a listview with checkbox where the checkbox is ticked whenever the user clicks on it. But I realized it's pretty inconvenient this way and I'd like to change it to whenever the user clicks on a row, the checkbox will be automatically ticked. My question is how can I do that? How can I change the code in listView.setOnItemClickListener such that my checkbox will be ticked? Any ideas?
This is my code so far:
MyCustomAdapter dataAdapter = null;
public static ArrayList countries = new ArrayList();;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isNetworkAvailable()) {
setContentView(R.layout.activity_show_countries);
ArrayList countries = new ArrayList();
countries.clear(); // refresh
//Generate list View from ArrayList
displayListView();
Button btn1 = (Button) findViewById(R.id.next);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(ShowCountries.this, ShowTypes.class));
}
});
}
else
{
startActivity(new Intent(ShowCountries.this, NoInternetConnection.class));
}
}
private void displayListView() {
//Array list of countries
countries.clear();//refresh
ArrayList<Country> countryList = new ArrayList<Country>();
Country country = new Country("POIs","Show POIs around me",false);
countryList.add(country);
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.country_info, countryList);
ListView listView = (ListView) findViewById(R.id.list_countries);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Country country = (Country) parent.getItemAtPosition(position);
if (countries.indexOf(country.getName()) !=-1) {
countries.remove(country.getName());
}
else
{
// thick checkbox
countries.add(country.getName());
}
// Toast.makeText(getApplicationContext(),
// "Clicked on Row: " + country.getName(),
// Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
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.country_info, 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 ;
Country country = (Country) cb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
if (countries.indexOf(cb.getText()) !=-1) {
country.setSelected(cb.isChecked());
countries.remove(cb.getText());
}
else
{
countries.add(cb.getText());
}
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.code.setText(" (" + country.getCode() + ")");
holder.name.setText(country.getName());
holder.name.setChecked(country.isSelected());
holder.name.setTag(country);
return convertView;
}
}
}
I guess there are couple of ways to do it, the one I use is:
Make a custom ListView using custom view which has a Checkbox then in getView() method use that Checkbox. Then make it implement setOnCheckChangeListener() to register the clicking.
And also don't forget to set value to Checkbox for example
Checkbox.setChecked(condition)
Checkbox.setOnCheckChangeListener()
I hope this helps
When your list item has been clicked, just call the toggle() function on your checkbox. And for your listview register a listselector to indicate focused and pressed states.
Related
I have created a listview that has an image and a button - I am using ArrayAdapter to view the items in the listview.
When I click on the button I would like to get the details of the item clicked.
So I tried the following:
pd = productList.get(position);
Where productList is ArrayList<ProductDetails> productList;
getproductdetailsbutton is a button in productListadapter extends ArrayAdapter<ProductDetails>
holder.getproductdetailsbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String productID = pd.getProductID();
String productName = pd. getProductName();
Log.d("Value", " Product Details " + productID + " " + productName);
}
});
When I click the button from the first item, I get details of the last item displayed on the screen in the log.
How do I get the details of clicked item for that position?
Thanks!
UPDATE: getVIEW Code:
#Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
Typeface tf = Typeface.createFromAsset(contextValue.getAssets(), fontPath);
Typeface tf2 = Typeface.createFromAsset(contextValue.getAssets(), fontPath2);
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.product_details_items, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.productimage);
holder.productgetdetails = (Button) convertView.findViewById(R.id.productgetdetails);
holder.productgetdetails.setTag(position);
holder.productgetdetails.setTypeface(tf2);
convertView.setTag(holder);
ProductDetails pd = productList.get(position);
if (pinexist.equalsIgnoreCase(contextValue.getString(R.string.pinexistvalue)))
{
Log.d("Value"," - ID " + pd.getProductID());
if (logdb.checkproductDetailsExists(pd.getProductID()) == 0)
{
holder.productgetdetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.e("Value", " = FINAL" + productList.get(position).getProductID());
}
});
}
else
{
holder.productgetdetails.setText(contextValue.getString(R.string.nodata));
}
}
else
{
//DO NOTHING
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
ProductDetails pd = productList.get(position);
Glide.with(getContext().getApplicationContext())
.load(pd.getProduct_image())
.placeholder(R.drawable.placeholder)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(holder.image);
return convertView;
}
I guess(actually I'm confident), you must be declaring pd as global variable, which gets updated by last item displayed, try to move pd into getView() and mark it as final so you can use it inside onclick as in
public void getView(){
final ProductDetails pd = productList.get(position);
// your code for click
}
When inflating your layout to view in Adapter
you can set the OnClickListener on the button
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_layout, null,true);
final Product pd = pdList.get(position);
//....
Button btn= (Button) = rowView.findViewById(R.id.getproductdetailsbutton);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String productID = pd.getProductID();
String productName = pd.getProductName();
Log.d("Value", " Product Details " + productID + " " + productName);
}
});
//..
return rowView;
};
Or you can handle ListView OnItemClickListener
I have a list view by clicking on the item in list view i will get another child list view . how can I get positions of parent list view by clicking on child items
Main Activity :
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
listView1.setTag(position);
list1 = (ListView) dialog.findViewById(R.id.list1);
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this, newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button) dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.Cancel1);
cancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
return false;
}
Parent List :
public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub
ViewHolder holder;
String item = null, qty = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflator.inflate(R.layout.itembumpingadapter, null);
holder.qty = (TextView) convertView.findViewById(R.id.qty);
holder.name = (TextView) convertView.findViewById(R.id.item);
holder.childText = (TextView) convertView
.findViewById(R.id.childitem);
holder.qtyChild = (TextView) convertView
.findViewById(R.id.qtychild);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.chckbox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
parentobjid = newListitems.get(position).getParentobjectid();
if (!parentobjid.isEmpty()) {
holder.name.setText(" " + newListitems.get(position).getItemnNameDisplay());
holder.name.setTextColor(Color.parseColor("#CC0000"));
holder.qty.setText(" " + String.valueOf(newListitems.get(position)
.getQuantityDisplay()));
holder.qty.setTextColor(Color.parseColor("#CC0000"));
} else {
holder.name.setText(newListitems.get(position).getItemnNameDisplay());
holder.qty.setText(String.valueOf(newListitems.get(position).getQuantityDisplay()));
holder.name.setTextColor(Color.parseColor("#FFFFFF"));
}
return convertView;
}
Child ListAdapter :
if (convertView == null) {
holder = new ViewHolder();
convertView = inflator.inflate(R.layout.itembumpingadapter, null);
holder.qty = (TextView) convertView.findViewById(R.id.qty);
holder.name = (TextView) convertView.findViewById(R.id.item);
holder.childText = (TextView) convertView
.findViewById(R.id.childitem);
holder.qtyChild = (TextView) convertView
.findViewById(R.id.qtychild);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.chckbox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
parentobjid = newListitems.get(position).getParentobjectid();
if (!parentobjid.isEmpty()) {
holder.name.setText(" " + newListitems.get(position).getItemnNameDisplay());
holder.name.setTextColor(Color.parseColor("#CC0000"));
holder.qty.setText(" " + String.valueOf(newListitems.get(position).getQuantityDisplay()));
holder.qty.setTextColor(Color.parseColor("#CC0000"));
} else {
holder.name.setText(newListitems.get(position).getItemnNameDisplay());
holder.qty.setText(String.valueOf(newListitems.get(position).getQuantityDisplay()));
holder.name.setTextColor(Color.parseColor("#FFFFFF"));
holder.qty.setTextColor(Color.parseColor("#FFFFFF"));
holder.checkbox.setChecked(false);
holder.checkbox.setTag(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int pos = (Integer) v.getTag();//Cast object to integer
newListitems.get(pos).setChecked(!newListitems.get(pos).isChecked());
}
});
if (newListitems.get(position).isChecked()) {
holder.name.setEnabled(true);
holder.name.setBackgroundColor(Color.parseColor("#DCDBDB"));
} else {
holder.name.setEnabled(false);
}
}
return convertView;
create two list object like
List<String> list_parent;
List<String> list_child;
on parent item click update list_child and assign to listview Adapter and set one flag for which listview is displaying (like set flag 0 if list_parent assign to adapter and 1 for child)
onBackPressed check if flag is 1 then assign list_parent to adapter and if flag is 0 then finish();
But remember every-time when you assign list object to adapter call notifyDataSetChanged
This is simple Listview code:
public class ListViewAndroidExample extends Activity {
ListView listView ;
int flag=0;//parent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_android_example);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values_parent = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
String[] values_child= new String[] { "Child 1",
"Child 2",
"Child 3",
"Child 4"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values_parent);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(flag=0)
{
flag=1;
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values_child);
}
else
{
flag=0;
}
}
});
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(flag=0)
finish();
else{
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values_parent);
}
}
}
I have a listview with switch button in each row. I store sum of checked switches in variable. In MainActivity where I start custom adapter I want to get a sum of checked switches. So how to pass value from custom adapter to parent activity? In the footer of listview I have a button "Dalej" I've create it in MainActivity, and want to get value (this sum) from adapter when user click on it?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cardio);
String[] cardio_1 = getResources().getStringArray(R.array.cardio_1);
ListView listView = (ListView) findViewById(R.id.listView);
View footer = getLayoutInflater().inflate(R.layout.cardio_footer, null);
listView.addFooterView(footer);
ListAdapter listAdapter = new Adapter_cardio(this, cardio_1);
cardio.addAll(Arrays.asList(cardio_1))
listView.setAdapter(listAdapter);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
});
Adapter
public Adapter_cardio(Context context, String[] cardios ) {
super(context, R.layout.cardio_row, cardios);
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("TAG", "tessst1");
zaznaczone = new ArrayList();
}
public String suma(String sum) {
return sum;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
String pytanie = getItem(position);
if (convertView==null){
convertView = inflater.inflate(R.layout.cardio_row, null);
holder = new ViewHolder();
holder.question = (TextView) convertView.findViewById(R.id.question);
holder.s = (Switch)convertView.findViewById(R.id.switch1);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.question.setText(pytanie);
//Model_cardio question = getItem(position);
//final boolean doCheck = (position == 4) || (position == 5);
holder.s.setTag(position);
holder.s.setOnCheckedChangeListener(null);
if (zaznaczone.contains(position) )
{
holder.s.setChecked(true);
}
else
{
holder.s.setChecked(false);
}
holder.s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
zaznaczone.add((Integer) buttonView.getTag());
} else {
zaznaczone.remove((Integer) buttonView.getTag());
}
}
});
sum = zaznaczone.size();
String x = "2";
Log.d("TAG_Switch_all", "suma = " + sum);
return convertView;
}
want to get value (this sum) from adapter when user click on it?
Create a method as public which return sum from Adapter_cardio class as:
public int getSum(){
return zaznaczone.size();
}
and call getSum method on Button click using listAdapter object as:
public void onClick(View v) {
Log.d("TAG_Switch_all", "suma = " + ((Adapter_cardio)listAdapter).getSum());
}
if listAdapter not accessible inside onClick method then declare it as final
I have a Listview that is showing a list .So on the click of the listview i have a customDialog.In that i am taking some values from the user.So want once the user enters the details and click on the ok button ,then i have to update the value of that item from the listview and when all the item of the listview has been updated then compare it with the previous value to check whether all the item are updated or not .Help me on this how could i do this
Activity Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_iween_booking_page);
intent = getIntent();
isReturn = (Boolean) intent.getExtras().get("isReturn");
searchParam = (HashMap<String,String>) intent.getExtras().get("searchParam");
listView = (ListView) findViewById(R.id.passengerList);
emailId = (TextView)findViewById(R.id.emailid);
continuebooking = (ImageView)findViewById(R.id.continuebooking);
firstName= (EditText)findViewById(R.id.firstName);
lastName =(EditText)findViewById(R.id.LastName);
mobileNumber =(EditText)findViewById(R.id.mobileNumber);
setTittle();
if(searchParam.get("NoOfChild").equals("0") && searchParam.get("NoOfInfant").equals("0")&& searchParam.get("NoOfAdult").equals("1")){
} else {
passengerList = getPassengerList(passengerInfo);
showPassengerListView(passengerList);
}
continuebooking.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(searchParam.get("NoOfChild").equals("0") && searchParam.get("NoOfInfant").equals("0") && searchParam.get("NoOfAdult").equals("1")){
if(firstName.getText().toString().trim().equalsIgnoreCase("")){
firstName.setError("Enter FirstName");
}
if(lastName.getText().toString().trim().equalsIgnoreCase("")){
lastName.setError("Enter LastName");
}
if(mobileNumber.getText().toString().trim().equalsIgnoreCase("")){
mobileNumber.setError("Enter Mobile No.");
}
}else{
int count = listView.getAdapter().getCount();
listData = new String[count];
for (int i = 0; i < count; i++) {
listData[i] = listView.getAdapter().getItem(i).toString();
}
for(int i=0;i<listView.getAdapter().getCount();i++){
for(int j=0;j<count;j++){
if(listData[j]==listView.getAdapter().getItem(i).toString()){
Log.d("listData data", listView.getAdapter().getItem(i).toString());
// View v=listView.getChildAt(i);
// TextView tv=(TextView) v.findViewById(android.R.id.text1);
// tv.setError("Please change the data");
}
}
}
}
}
});
}
private void showPassengerListView(final String[] passengerList) {
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, passengerList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// int itemPosition = position;
// String itemValue = (String) listView.getItemAtPosition(position);
View v=listView.getChildAt(position);
TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setError(null);
passengerInformationPopup(passengerList,position);
}
});
}
public void passengerInformationPopup(final String[] passengerList, final int position) {
final Dialog dialog= new Dialog(Test.this,R.style.Dialog_Fullscreen);
dialog.setContentView(R.layout.passenger_details_dialog);
final EditText firstNameDialog;
final EditText lastNameDialog;
ImageView continueBooking;
dateofBirth = (TextView)dialog.findViewById(R.id.dateofBirth);
firstNameDialog = (EditText)dialog.findViewById(R.id.firstName);
lastNameDialog =(EditText)dialog.findViewById(R.id.LastName);
continueBooking =(ImageView)dialog.findViewById(R.id.continuebooking);
if((passengerList[position].contains("Child"))|| (passengerList[position].contains("Infant"))){
dateofBirth.setVisibility(View.VISIBLE);
}else{
dateofBirth.setVisibility(View.GONE);
}
dateofBirth.setClickable(true);
dialog.show();
continueBooking.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
isSuccess= true;
if(firstNameDialog.getText().toString().trim().equalsIgnoreCase("")){
firstNameDialog.setError("Enter FirstName");
isSuccess= false;
}
if(lastNameDialog.getText().toString().trim().equalsIgnoreCase("")){
lastNameDialog.setError("Enter LastName");
isSuccess= false;
}
if((passengerList[position].contains("Child"))|| (passengerList[position].contains("Infant"))){
if(dateofBirth.getText().toString().trim().equalsIgnoreCase("")){
dateofBirth.setError("Date of Birth Can't be blank");
isSuccess= false;
}
}
if(isSuccess){
dialog.cancel();
View v=listView.getChildAt(position);
TextView tv= (TextView) v.findViewById(android.R.id.text1);
tv.setText(firstNameDialog.getText().toString().trim().toString()+" "+lastNameDialog.getText().toString().trim().toString());
}
}
});
}
passengerInformationPopup function i have to update the items values of the ListView .In on create continueBooking i have to check whether all the items are updated or not
Before Updation
After Updation
Never update ListView items directly. Update data in your storage and call notifyDataSetChanged on your adapter.
You will need an ArrayAdapter with the data for your list. Then when you manipulate the data in that array you can call .notifyDataSetChanged(); on the adabter to refresh your view.
Be aware that it have to be the same arraylist! so you cant call arraylist = new ArrayList(); that will destroy the reference. Instead use arraylist.clear(); and then arraylist.addAll(data);
Here is an example:
public class GroupsListAdapter extends ArrayAdapter<Object> {
/**
* Constructor
*
* #param context
* Context
* #param objects
* Array of objects to show in the list.
*/
public GroupsListAdapter(Context context, ArrayList<Object> objects) {
super(context, R.layout.row, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Object = getItem(position);
/* Initialize strings */
String nameText = group.getName();
boolean upToDate = group.isUpToDate();
/* Get the layout for the list rows */
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
}
rowView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff here
}
});
return rowView;
}
}
I have a multicolumn List view like the one shown in the image, i used custom adapters to populate this custom list.so the question is how to get data on click of submit button means when i click submit button i should get data like name, price and quantity of only checked checkbox....Thanx in advance.
In my Main xml i have a listview and in mainlist xml i have txtname, txtprice, edittext and checkbox and use efficient adapter.
i'm able to view data in list view, bt the problem is i m unable to save data on click of submit button... so plz help me out, with a sample code, bcz m new to android..
the following is my code..
public class Menu extends Activity {
ListView list;
Cursor cursorMenu;
Button btnPlaceOrder;
Button btnShowOrders;
String Descstr="";
String strtotal="";
List<String[]> lstSelectedItems = null;
DBAdapter db = new DBAdapter(this);
private String[] strName;
private String[] strPrice;
private String[] strDescription;
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
try {
return strName.length;
} catch (Exception e) {
//Toast.makeText(Menu.this, "No Data !", Toast.LENGTH_LONG).show();
return 0;
}
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menulist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txtItemName);
holder.text2 = (TextView) convertView
.findViewById(R.id.txtPrice);
holder.text3 = (TextView) convertView
.findViewById(R.id.txtDescription);
holder.etext3 = (EditText) convertView
.findViewById(R.id.txtQty);
holder.chk = (CheckBox) convertView
.findViewById(R.id.chkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(strName[position]);
holder.text2.setText(strPrice[position]);
holder.text3.setText(strDescription[position]);
return convertView;
}
class ViewHolder {
TextView text;
TextView text2;
TextView text3;
EditText etext3;
CheckBox chk;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnPlaceOrder = (Button) findViewById(R.id.btnPlaceOrder);
btnPlaceOrder.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
/*db.open();
cursorMenu = db.menu_getAllTitles();
int rowcount = cursorMenu.getCount();
System.out.println("---- +++++ " + rowcount);
System.out.println("---- column +++++ " + cursorMenu.getColumnCount());
int index = 0;
if (rowcount > 0) {
strName = new String[rowcount];
strPrice = new String[rowcount];
strDescription = new String[rowcount];
if (cursorMenu.moveToFirst()) {
do {
strName[index] = cursorMenu.getString(1);
strDescription[index] = cursorMenu.getString(2);
strPrice[index] = cursorMenu.getString(3);
Log.v(TAG, "Name-- " + strName[index] + "Price-- "
+ strPrice[index]);
index++;
} while (cursorMenu.moveToNext());
}
cursorMenu.close();
} else {
Toast.makeText(this, "No Data found", Toast.LENGTH_LONG).show();
}*/
list = (ListView) findViewById(R.id.lstMenu);
list.setAdapter(new EfficientAdapter(this));
System.out.println("--List Child count-----"+list.getChildCount());
System.out.println("--List count-----"+list.getCount());
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"You clciked " + strName[arg2] + "\t" + strPrice[arg2],
Toast.LENGTH_LONG).show();
}
});
}
}
http://www.vogella.de/articles/AndroidListView/article.html go through this example you can get every thing regards listview.