Adding radio button to listview - android

I would like to add a radio button to my existing listview so that only one radio button needs to be selected at a time.
ItemDetails:
public class ItemDetails {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getImageNumber() {
return imageNumber;
}
public void setImageNumber(int imageNumber) {
this.imageNumber = imageNumber;
}
private String name ;
private String itemDescription;
private String price;
private int imageNumber;
}
Adapter:
public class ItemListBaseAdapter extends BaseAdapter {
private static ArrayList<ItemDetails> itemDetailsrrayList;
private Integer[] imgid = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6
};
private LayoutInflater l_Inflater;
public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
itemDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Object getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.item_details_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
// imageLoader.DisplayImage("http://192.168.1.28:8082/ANDROID/images/BEVE.jpeg", holder.itemImage);
View row = convertView;
CheckedTextView checkBox = (CheckedTextView) row.findViewById(R.id.checkstate);
checkBox.setChecked(false);
return convertView;
}
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
ImageView itemImage;
CheckedTextView checkBox;
}
}
ItemDetails.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/photo"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp">
<TextView android:id="#+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"/>
<TextView android:id="#+id/price"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/itemDescription"/>
</RelativeLayout>
</RelativeLayout>
ListView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background ="#CCCCCC">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
</LinearLayout>
I have seen many SO anwsers but none of them got working in my case saying to add
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Can anyone say me how to acheive this ?

For complex ListView item, you should do it by yourself. Just add boolean isSelected in your ItemDetails and then checkBox.setChecked(item.isSelected); in your getView() in ItemListBaseAdapter. Now when an item is clicked in ListView just change your the data in itemDetailsrrayList and call notifyDataSetChanged().

I have made my getView() method.Just make radio button instead of simple button.I Hope this helps.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View productListView = null;
productListView = convertView;
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (productListView == null)
productListView = inflater.inflate(R.layout.menu_productlist_list,
parent, false);
productListTitle = (TextView) productListView
.findViewById(R.id.productListTitle);
String title = menuProductListCollection.get(position).getPD_TITLE();
productListTitle.setText(title);
productListCategoryButton = (ImageButton) productListView
.findViewById(R.id.productListCategoryButton);
// Perform Button action on click of each row's button
productListCategoryButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do your stuff here
}
});
// Perform action on click of each row
productListView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do your stuff here
}
});
return productListView;
}

I think you should try both of this,
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
And in xml file you need to add,
<ListView
...
android:choiceMode="singleChoice"
... />
Also add this in your radio-button's xml,
<RadioButton
...
android:clickable="false"
android:focusable="false"
... />
Make HashMap m and initialize it
for (HashMap<String, Object> m :list_data) //make data of this view should not be null (hide )
m.put("checked", false);
Now, setViewBinder to adapter
adapter.setViewBinder(new SimpleAdapter.ViewBinder()
{
public boolean setViewValue(View view, Object data, String textRepresentation)
{
if (data == null) //if 2nd line text is null, its textview should be hidden
{
view.setVisibility(View.GONE);
return true;
}
view.setVisibility(View.VISIBLE);
return false;
}
});
It will work like charm.
Check this Example link

Related

compare listview time with currenttime

There are 3 textviews and 1 button in my listview.One of textview contains time.I want button must get clicked after 20mins of intime.I want to compare Intime with current time and if difference >20 mins then button must get clicked.Following is my code-
MainActivity.java
public class MainActivity extends Activity {
TextView txtno,txtname,txtintime;
ListView listView;
ArrayList<Model> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtno=(TextView)findViewById(R.id.no);
txtname=(TextView)findViewById(R.id.name);
txtintime=(TextView)findViewById(R.id.intime);
listView = (ListView) findViewById(R.id.list);
data=new ArrayList<>();
data.add(new Model(1,"ABC","11:45:50 AM"));
data.add(new Model(2,"PQR","11:50:50 AM"));
data.add(new Model(1,"XyZ","12:45:50 PM"));
data.add(new Model(1,"SHjhsj","04:45:50 PM"));
Custom c=new Custom(this,data);
listView.setAdapter(c);
}
}
Model.java
public class Model {
int no;
String name,intime;
public Model(int no, String name, String intime) {
this.no = no;
this.name = name;
this.intime = intime;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntime() {
return intime;
}
public void setIntime(String intime) {
this.intime = intime;
}
}
Custom.java
public class Custom extends BaseAdapter {
Activity a;
ArrayList<Model> data;
public Custom(Activity a, ArrayList<Model> data) {
this.a = a;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
public class Viewholder {
TextView srno, name, intime;
Button end;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
Viewholder viewholder = null;
if (convertView == null) {
viewholder = new Viewholder();
LayoutInflater li = a.getLayoutInflater();
convertView = li.inflate(R.layout.list_item, viewGroup, false);
viewholder.srno = (TextView) convertView.findViewById(R.id.cno);
viewholder.name = (TextView) convertView.findViewById(R.id.cname);
viewholder.intime = (TextView) convertView.findViewById(R.id.ctime);
viewholder.end = (Button) convertView.findViewById(R.id.cend);
convertView.setTag(viewholder);
}else {
viewholder=(Viewholder)convertView.getTag();
}
final Model model = data.get(i);
viewholder.srno.setText(valueOf(data.get(i).getNo()));
viewholder.name.setText(valueOf(data.get(i).getName()));
viewholder.intime.setText(valueOf(data.get(i).getIntime()));
viewholder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(a, data.get(i).getName()+" Exited", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.abc.timer.MainActivity">
<TextView
android:layout_width="70dp"
android:layout_height="50dp"
android:id="#+id/no"
android:text="CustNo"
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:id="#+id/name"
android:layout_toRightOf="#+id/no"
android:text="Name"
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:id="#+id/intime"
android:layout_toRightOf="#+id/name"
android:text="InTime"
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_width="70dp"
android:layout_height="50dp"
android:id="#+id/exit"
android:text="Exit"
android:layout_toRightOf="#+id/intime"
android:textSize="20dp"
android:textColor="#000"/>
<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#+id/no"
android:id="#+id/list"
></ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="#+id/cno"
android:gravity="center"/>
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="#+id/cname"
android:gravity="center"/>
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:id="#+id/ctime"
android:gravity="center"/>
<Button
android:layout_width="90dp"
android:layout_height="40dp"
android:text="End"
android:id="#+id/cend"
android:background="#c14c4a"
android:gravity="center"/>
</LinearLayout>
Use an AlarmManager with your specified time and the moment alarm will trigger you can enable the button, hence enabling the click event for the button.
Edit:
Either you can go ahead with handler object and use handler.postDelayed() method or there itself you can create a Timer Object and schedule the timer task within that object and do the stuff.
Assuming that u know how to convert the time strings into timestamps,modify ur adapter like this:
public class Custom extends BaseAdapter {
Activity a;
ArrayList<Model> data;
public Custom(Activity a, ArrayList<Model> data) {
this.a = a;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
public class Viewholder {
TextView srno, name, intime;
Button end;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
Viewholder viewholder = null;
if (convertView == null) {
viewholder = new Viewholder();
LayoutInflater li = a.getLayoutInflater();
convertView = li.inflate(R.layout.list_item, viewGroup, false);
viewholder.srno = (TextView) convertView.findViewById(R.id.cno);
viewholder.name = (TextView) convertView.findViewById(R.id.cname);
viewholder.intime = (TextView) convertView.findViewById(R.id.ctime);
viewholder.end = (Button) convertView.findViewById(R.id.cend);
convertView.setTag(viewholder);
}else {
viewholder=(Viewholder)convertView.getTag();
}
final Model model = data.get(i);
viewholder.srno.setText(valueOf(data.get(i).getNo()));
viewholder.name.setText(valueOf(data.get(i).getName()));
viewholder.intime.setText(valueOf(data.get(i).getIntime()));
final Runnable r = new Runnable() {
public void run() {
viewholder.end.setEnabled(true);
}
};
if(yourObjectTimestamp - currentTimeStamp > 20){ //this u need to do on ur own
viewholder.end.setEnabled(true); // I assumed u know how to convert times to timestamps
}else{
viewholder.end.setEnabled(false);
long timeDiffInMillis = yourObjectTimestamp - currentTimeStamp;
handler.postDelayed(r,timeDiffInMillis );
}
viewholder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(a, data.get(i).getName()+" Exited", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}

use Custome ListView with TextView And CheckBox With Single Selection of CheckBox

Hear i use Custom ListView with TextView and CheckBox.
But i want that Single Selection in CheckBox At a time
One CheckBox Selected then other one is Deselect
using BaseAdapter
but This code not Work Properly ..
Please Give me Suggestion ..thnks
#Override
public View getView(final int position, View view, ViewGroup parent) {
Integer selected_position = -1;
holder = new ViewHolder();
final Items itm = rowItem.get(position);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = layoutInflater.inflate(R.layout.activity_custom_list,
parent, false);
holder.tvItemName = (TextView) view.findViewById(R.id.textView1);
holder.check = (CheckBox) view.findViewById(R.id.checkBox1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvItemName.setText(itm.getItems());
if (position == selected_position)
holder.check.setChecked(true);
else
holder.check.setChecked(false);
holder.check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (holder.check.isChecked()) {
selected_position = position;
} else {
selected_position = -1;
}
notifyDataSetChanged();
}
});
return view;
}}
use Custom List-View with Text-View And Check-Box With Single Selection of Check Box
So many try then finally i got the Solution I hope it is Useful Code to you All...
This code Help you to create custom List-view with Text-view and Check-box then you select one check-box and if you select another one the first should automatically be Deselected.....Thank You...
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.listviewdemo2.MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
activity_custom_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.listviewdemo2.CustomListActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray" >
<TextView
android:id="#+id/textView1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="4dp"
android:layout_weight="0.39"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="4dp"/>
</LinearLayout> </LinearLayout>
String.xml
<string-array name="name">
<item>Laptop</item>
<item>Mobile</item>
<item>Desktop</item>
<item>TV</item>
<item>Pendrive</item>
<item>Router</item>
<item>Notebook</item>
<item>Tablet</item>
<item>I-pad</item>
<item>Bluetooth</item>
<item>HomeTheator</item>
</string-array>
MainActivity.java
String[] ItemName;
List<Items> rowItem;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItem = new ArrayList<Items>();
ItemName = getResources().getStringArray(R.array.name);
for(int i = 0 ; i < ItemName.length ; i++)
{
Items itm = new Items(ItemName[i]);
rowItem.add(itm);
}
list = (ListView) findViewById(R.id.listView1);
CustomListActivity adapter = new CustomListActivity(this, rowItem);
list.setAdapter(adapter);
}
Items.java
public class Items {
private String items;
private boolean selected;
public Items(String items) {
this.items = items;
}
public String getItems() {
return items;
}
public void setItemName(String name) {
this.items = name;
}
public boolean getSelected() {
return selected;
}
public boolean setSelected(Boolean selected) {
return this.selected = selected;
}}
CustomListActivity.java
public class CustomListActivity extends BaseAdapter {
Context context;
List<Items> rowItem;
View listView;
boolean checkState[];
ViewHolder holder;
public CustomListActivity(Context context, List<Items> rowItem) {
this.context = context;
this.rowItem = rowItem;
checkState = new boolean[rowItem.size()];
}
#Override
public int getCount() {
return rowItem.size();
}
#Override
public Object getItem(int position) {
return rowItem.get(position);
}
#Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
public class ViewHolder {
TextView tvItemName;
CheckBox check;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
holder = new ViewHolder();
final Items itm = rowItem.get(position);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (view == null) {
listView = new View(context);
listView = layoutInflater.inflate(R.layout.activity_custom_list,
parent, false);
holder.tvItemName = (TextView) listView
.findViewById(R.id.textView1);
holder.check = (CheckBox) listView.findViewById(R.id.checkBox1);
listView.setTag(holder);
} else {
listView = (View) view;
holder = (ViewHolder) listView.getTag();
}
holder.tvItemName.setText(itm.getItems());
holder.check.setChecked(checkState[position]);
holder.check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for(int i=0;i<checkState.length;i++)
{
if(i==position)
{
checkState[i]=true;
}
else
{
checkState[i]=false;
}
}
notifyDataSetChanged();
}
});
return listView;
}}
Show The Output :-
If you want a custom ListView with single choice CheckBox you can use something like this:
https://stackoverflow.com/a/12003125/5778152

Error changing position a Checkbox in XML

I am getting an error when trying to implement a Custom Spinner Multi Selection. I get ONLY an error when changing the position Checkbox in XML (I want to put the Checkbox to right).
ERROR
10-15 11:42:17.238: E/AndroidRuntime(29962): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.CheckBox
10-15 11:42:17.238: E/AndroidRuntime(29962): at com.dropdownlistdemo.DropDownListAdapter.getView(DropDownListAdapter.java:68)
ERROR LINE
holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
XML OK
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/SelectOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/rojo" />
</LinearLayout>
XML FAIL
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/SelectOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/rojo" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA
public class DropDownListAdapter extends BaseAdapter {
private ArrayList<String> mListItems;
private LayoutInflater mInflater;
private TextView mSelectedItems;
private static int selectedCount = 0;
private static String firstSelected = "";
private ViewHolder holder;
private static String selected = "";
public static String getSelected() {
return selected;
}
public void setSelected(String selected) {
DropDownListAdapter.selected = selected;
}
public DropDownListAdapter(Context context, ArrayList<String> items,
TextView tv) {
mListItems = new ArrayList<String>();
mListItems.addAll(items);
mInflater = LayoutInflater.from(context);
mSelectedItems = tv;
}
#Override
public int getCount() {
return mListItems.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.drop_down_list_row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.SelectOption);
holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(mListItems.get(position));
final int position1 = position;
holder.chkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
if(DropDownListDemo.checkSelected[position])
holder.chkbox.setChecked(true);
else
holder.chkbox.setChecked(false);
return convertView;
}
private class ViewHolder {
TextView tv;
CheckBox chkbox;
}
}

how to insert an image into a listview?

I would like to insert a small image to the right of each item in a listview
basically my app should do so as soon as the user clicks on an item in the list view, the image becomes visible, otherwise it must remain invisible.
below is my activity with its XML
Activity
public class EpisodiActivity extends Activity {
public class ViewModel {
private String url;
private String name;
public ViewModel(String url, String name) {
this.url = url;
this.name = name;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.episodi_activity);
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
ListView mylist = (ListView) findViewById(R.id.listView1);
// And in this loop we create the ViewModel instances from
// the name and url and add them all to a List
List<ViewModel> models = new ArrayList<ViewModel>();
for (int i = 0; i < episodi.length; i++) {
String name = episodi[i];
String url = "No value";
if (i < urls.length) {
url = urls[i];
}
ViewModel model = new ViewModel(url, name);
models.add(model);
}
// Here we create the ArrayAdapter and assign it to the ListView
// We pass the List of ViewModel instances into the ArrayAdapter
final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(this, android.R.layout.simple_list_item_1, models);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
// Here we get the ViewModel at the given position
ViewModel model = (ViewModel) arg0.getItemAtPosition(position);
// And the url from the ViewModel
String url = model.getUrl();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
}
XML
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/pubblicita"
android:cacheColorHint="#ffd700"
android:background="#drawable/sfondobottone" />
I think you want this kind of output in listview
text with image in listview
You can use custom listview . Make a class which extends BaseAdapter class
here is the exmaple that i am using
Your BaseAdapter
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FrontListBaseAdapter extends BaseAdapter {
private static ArrayList<FrontDetails> itemDetailsrrayList;
private LayoutInflater l_Inflater;
public FrontListBaseAdapter(Context context, ArrayList<FrontDetails> results) {
itemDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Object getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
// get the views in frontview xml file where you have
// define multiple views that will appear in listview each row
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.frontview, null);
holder = new ViewHolder();
holder.Image = (ImageView) convertView.findViewById(R.id.adminpic1);
holder.MsgType = (TextView) convertView.findViewById(R.id.msgtype1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.Image.setImageResource(R.drawable.mainlogo); // you can set your setter here
holder.MsgType.setText(itemDetailsrrayList.get(position).getMsgType());
return convertView;
}
// holder view for views
static class ViewHolder {
ImageView Image;
TextView MsgType;
}
}
your FrontDetails class where you will make getters and setters and this class will be used in final ArrayList resultse = new ArrayList();
import android.graphics.Bitmap;
public class FrontDetails {
public int getImage() {
return image;
}
public void setImage(int imageN) {
this.image = imageN;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String text) {
this.MsgType = text;
}
private int image;
private String MsgType;
}
your frontview.XML where you put your multiple views that will be in each row or your layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_margin="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp" >
<ImageView
android:id="#+id/adminpic1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/msgtype1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:textSize="1sp"
android:text="MsgType" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
and your listview in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/sync"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sync" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
</ListView>
</LinearLayout>
now in your main activity
final ArrayList<FrontDetails> resultse = new ArrayList<FrontDetails>();
FrontListBaseAdapter asdf = new FrontListBaseAdapter(context, resultse);
lv1.setAdapter(new FrontListBaseAdapter(Front.this, resultse));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Object o = lv1.getItemAtPosition(position);
FrontDetails obj_itemDetails = (FrontDetails)o;
Toast.makeText(context, "You have chosen " + ' ' + obj_itemDetails.getMsgType(), Toast.LENGTH_LONG).show();
}
});
EDIT:
From here i learned Custom Listview its a simple exmaple with image
http://www.javasrilankansupport.com/2012/05/android-listview-example-with-image-and.html
http://www.javacodegeeks.com/2012/10/android-listview-example-with-image-and.html
Use custom listview with BaseAdapter
Your Adapter
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
Your list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="#string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16dp" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_toRightOf="#+id/icon"
android:paddingLeft="10dp"
android:textColor="#3399FF"
android:textSize="14dp" />
</RelativeLayout>
Your Single Row item class
public class RowItem {
private int imageId;
private String title;
private String desc;
public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return title + "\n" + desc;
}
}
List view implementation
listView = (ListView) findViewById(R.id.list);
CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
I can give some tips ,but unfortunately couldn't help you by example..
First of all create one custom adapter(extends BaseAdapter) followed by one custom layout..
Here the custom layout contains the textview and one image view(by default invisible) at the right.
Just customize your list view with your adapter and put text inside TextView through get view()..
At last in your listItemClickListener make the image visible by its position.
You can set here on xml like this
android:visibility="visible"
or
android:visibility="invisible"
or
android:visibility="gone"
Java program:
ImageView imgView = (ImageView)findViewById(R.id.custom);
set your ImageView like this
imgView .setVisibility(View.VISIBLE);
imgView .setVisibility(View.INVISIBLE);
imgView .setVisibility(View.GONE);
Difference between INVISIBLE and GONE.
INVISIBLE - The widget will be invisible but space for the widget will be show.
GONE - Both space and widget is invisible.
Now you can hook your setOnItemClickListener()
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
imgView .setVisibility(View.VISIBLE);
}
});

Custom List View with OnClickListener

I have custom ListView layout with a TextView and CheckBox. Everything works fine.
What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.
I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.
MAIN ACTIVITY
package com.example.smsplanner;
public class SMSPlanner extends ListActivity {
ListView contactsListView;
private String TAG = "SMSPlanner"; CheckBox check;
int count;
List<ContactInfo> list = new ArrayList<ContactInfo>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ph = new String[3];
phType = new String[3];
LoadContactListFromPhone();
ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(contactadAdapter);
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
TextView tx =(TextView)v.findViewById(R.id.firstname);
TextView ph =(TextView)v.findViewById(R.id.phone);
Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
void LoadContactListFromPhone()
{
loadlistandreturns();
}
void call()
{
Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
toast.show();
}
}
CUSTOM ADAPTER
public class ContactsAdapter extends ArrayAdapter<ContactInfo>
{
private final Activity context;
int resourceid;
List<ContactInfo> list = null;
public ContactsAdapter(Activity context, List<ContactInfo> list) {
super(context, R.layout.contactrow, list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
}
LAYOUT
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:orientation="vertical" >
<TextView
android:id="#+id/firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgStyle2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<CheckBox
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RadioGroup>
</LinearLayout>
1. Bydefault all the Rows of the ListView are enabled to listen to click....
You must implement onItemClickListener() for the ListView....
See this example:
http://www.mkyong.com/android/android-listview-example/
you can use a CheckedTextView, or you can create a Checkable Layout, like this one :
http://tokudu.com/2010/android-checkable-linear-layout/
i think you should make adapter as
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}

Categories

Resources