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;
}
}
Related
i"m posting my adapter class and layout. can anyone suggest me how can i get rid from this lagging problem.
i tried view holder but it didn't work.
so any other solution possible then tell me fast.
my layout files are as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_marginBottom="5dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<View
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="#drawable/circle2"/>
<com.hitesh.custom.Custom_font
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="13sp"
android:layout_gravity="center"
android:text=""/>
</FrameLayout>
<com.hitesh.custom.Custom_Roboto_Regular
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="25sp" />
</LinearLayout>
<com.hitesh.custom.Custom_Roboto_Light
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:lineSpacingExtra="1dp"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and adapter
public class Rules_d_adapter extends BaseAdapter{
private Context context;
List<String> question =new ArrayList<String>();
List<String> answer =new ArrayList<String>();
public Rules_d_adapter(Context con,List<String> Lines,List<String> Lines2)
{question=Lines;
answer=Lines2;
context = con;
}
#Override
public int getCount() {
return question.size();
}
#Override
public Object getItem(int i) {
return question.size();
}
#Override
public long getItemId(int i) {
return question.size();
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rules_d_adapter, viewGroup, false);
Custom_Roboto_Light tv2 =
(Custom_Roboto_Light)view.findViewById(R.id.tv2) ;
Custom_Roboto_Regular tv = (Custom_Roboto_Regular)
view.findViewById(R.id.tv1) ;
tv.setText(question.get(i));
tv2.setText(answer.get(i));
return view;
}
}
if you still happy with ListView then you can take idea from below
code
here i am using Cursor but you can use ArrayList in place of Cursor
public class Custom_Adapter_Products extends BaseAdapter {
Context context;
Cursor mCursor;
static int[] intArr;
DatabaseAdapter db;
public ImageLoader imageLoader;
public Custom_Adapter_Products(Context context, Cursor mCursor){
this.context = context;
this.mCursor = mCursor;
this.intArr = new int[this.mCursor.getCount()];
imageLoader=new ImageLoader(context);
}
#Override
public int getCount() {
return mCursor.getCount();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
try{
ViewHolder holder;
this.mCursor.moveToPosition(position);
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = vi.inflate(R.layout.lv_row_for_products, null);
holder = createViewHolder(convertView);
convertView.setTag(holder);
holder.btnMinus.setTag(holder);
holder.btnAdd.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
ViewHolder holder = (ViewHolder) v.getTag();
// Do whatever you want
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder
{
public TextView tvProductName;
public TextView tvPrice;
public TextView tvMRP;
public TextView tvMOQ;
public TextView tvPercent;
public TextView tvQuantity;
public ImageView imgProduct;
public ProgressBar pBar;
public Button btnMinus;
public Button btnAdd;
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
try{
holder.tvProductName = (TextView) v.findViewById(R.id.tvProductName);
holder.tvPrice = (TextView) v.findViewById(R.id.tvPrice);
holder.tvMRP = (TextView) v.findViewById(R.id.tvMRP);
holder.tvMOQ = (TextView) v.findViewById(R.id.tvMOQ);
holder.tvQuantity = (TextView) v.findViewById(R.id.tvQuantity);
holder.imgProduct = (ImageView) v.findViewById(R.id.imgProduct);
holder.pBar = (ProgressBar) v.findViewById(R.id.img_pb);
holder.btnMinus = (Button) v.findViewById(R.id.btnMinus);
holder.btnAdd = (Button) v.findViewById(R.id.btnAdd);
holder.tvPercent = (TextView) v.findViewById(R.id.tvPercent);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return holder;
}
}
Am trying to make a ui with flipview as in this tutorial. IThe tutorial deals with activities, but i want it in fragments case. That ie, the flip effect as well as its parent is a fragment. When i use activity it works, but on using fragment,only the empty textview shows up. Can anyone help me with this?
This is some part of the code
page.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff3333" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/banner"
android:id="#+id/head"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:id="#+id/header"
android:gravity="center"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/des"
android:textSize="30dp"
android:gravity="center"/>
</FrameLayout>
fragment_news_feed :
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</merge>
NewsFeedFragment.java
public class NewsFeedFragment extends Fragment implements NewsFlipAdapter.Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener{
private FlipView mFlipView;
private NewsFlipAdapter mAdapter;
int i=0,no_of_items=7;
public NewsFeedFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout wrapper = new LinearLayout(getActivity());
View view = inflater.inflate(R.layout.fragment_news_feed, wrapper, true);
setUpView(view);
return wrapper;
}
private void setUpView(View view) {
mFlipView = (FlipView) view.findViewById(R.id.flip_view);
mAdapter = new NewsFlipAdapter(getActivity());
mAdapter.setCallback(this);
mFlipView.setAdapter(mAdapter);
mFlipView.setOnFlipListener(this);
mFlipView.peakNext(false);
mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
mFlipView.setOnOverFlipListener(this);
}
#Override
public void onPageRequested(int page) {
mFlipView.smoothFlipTo(page);
}
#Override
public void onFlippedToPage(FlipView v, int position, long id) {
Log.i("pageflip", "Page: " + position);
if(position > mFlipView.getPageCount()-3 && mFlipView.getPageCount()<30){
mAdapter.addItems(5);
}
}
#Override
public void onOverFlip(FlipView v, OverFlipMode mode,
boolean overFlippingPrevious, float overFlipDistance,
float flipDistancePerPage) {
Log.i("overflip", "overFlipDistance = "+overFlipDistance);
}
NewsFlipAdapter.java
public class NewsFlipAdapter extends BaseAdapter {
public interface Callback {
public void onPageRequested(int page);
}
static class Item {
static long id = 0;
long mId;
public Item() {
mId = id++;
}
long getId() {
return mId;
}
}
private LayoutInflater inflater;
private Callback callback;
private List<Item> items = new ArrayList<Item>();
public NewsFlipAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return items.get(position).getId();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.page, parent, false);
holder.heading = (TextView) convertView.findViewById(R.id.header);
holder.description = (TextView) convertView.findViewById(R.id.des);
holder.header = (ImageView) convertView.findViewById(R.id.head);
// holder.firstPage = (Button) convertView.findViewById(R.id.first_page);
// holder.lastPage = (Button) convertView.findViewById(R.id.last_page);
// holder.firstPage.setOnClickListener(this);
// holder.lastPage.setOnClickListener(this);
// convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView heading;
ImageView header;
TextView description;
}
/* #Override
public void onClick(View v) {
switch(v.getId()){
case R.id.first_page:
if(callback != null){
callback.onPageRequested(0);
}
break;
case R.id.last_page:
if(callback != null){
callback.onPageRequested(getCount()-1);
}
break;
}
}*/
public void addItems(int amount) {
TextView text;
for (int i = 0; i < amount; i++) {
items.add(new Item());
}
notifyDataSetChanged();
}
public void addItemsBefore(int amount) {
for (int i = 0; i < amount; i++) {
items.add(0, new Item());
}
notifyDataSetChanged();
}
I am not a 100 % sure about the following explanation, so please correct me if I am wrong.
The <merge> tag merges the inflated layout with it's parent view.
It generally doesn't seem to sit well with fragments as explained in more detail here.
So try to replace merge with LinearLayout in your fragment_newsfeed_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</LinearLayout>
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;
}
}
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);
}
});
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