Test.java:
public class Test extends Activity{
/** Called when the activity is first created. */
protected Dialog mSplashDialog;
private static final String[] country = { "Iceland", "India", "Indonesia","Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia","Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania","Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD","EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD","LTL ", "EUR"};
ListView l1;
private OnItemClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyStateSaver data = (MyStateSaver) getLastNonConfigurationInstance();
if (data != null) {
// Show splash screen if still loading
if (data.showSplashScreen) {
showSplashScreen();
}
setContentView(R.layout.main);
// Rebuild your UI with your saved state here
} else {
showSplashScreen();
setContentView(R.layout.main);
final ListView l1 = (ListView) findViewById(android.R.id.list);
l1.setAdapter(new EfficientAdapter(this));
l1.setItemsCanFocus(true);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Object listItem = l1.getItemAtPosition(position);
Log.v("MyApp", "get onItem Click position= " + position);
Toast.makeText(getApplicationContext(), listItem.toString(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Valid", Toast.LENGTH_SHORT).show();
}
});
}
}
#Override
public Object onRetainNonConfigurationInstance() {
MyStateSaver data = new MyStateSaver();
// Save your important data here
if (mSplashDialog != null) {
data.showSplashScreen = true;
removeSplashScreen();
}
return data;
}
/**
* Removes the Dialog that displays the splash screen
*/
protected void removeSplashScreen() {
if (mSplashDialog != null) {
mSplashDialog.dismiss();
mSplashDialog = null;
}
}
/**
* Shows the splash screen over the full Activity
*/
protected void showSplashScreen() {
mSplashDialog = new Dialog(this, R.style.SplashScreen);
mSplashDialog.setContentView(R.layout.splashscreen);
mSplashDialog.setCancelable(false);
mSplashDialog.show();
// Set Runnable to remove splash screen just in case
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
removeSplashScreen();
}
}, 3000);
}
/**
* Simple class for storing important data across config changes
*/
private class MyStateSaver {
public boolean showSplashScreen = false;
// Your other important fields here
}
public static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return country.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview, null);
convertView.setClickable(true);
convertView.setFocusableInTouchMode(true);
convertView.setFocusable(true);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
holder.text3 = (TextView) convertView.findViewById(R.id.TextView03);
holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(curr[position]);
holder.text2.setText(country[position]);
holder.text3.setText(country[position]);
holder.text4.setText(country[position]);
return convertView;
}
static class ViewHolder {
TextView text4;
TextView text;
TextView text2;
TextView text3;
}
}}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/theater">
<ListView
android:id="#android:id/list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:listSelector="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:layout_below = "#+id/catagory"/>
<Button
android:id="#+id/GPS"
android:background="#drawable/fire"
android:layout_marginTop="20px"
android:layout_height="70px"
android:layout_width="80px"
android:layout_alignParentLeft="true"/>
<EditText
android:id="#+id/search"
android:layout_width="300px"
android:layout_height="70px"
android:layout_marginTop="20px"
android:hint="Search theater"
android:textSize="30px"
android:inputType="text"
android:layout_toRightOf="#+id/GPS"/>
<Button
android:text="OK"
android:layout_width="100px"
android:layout_height="70px"
android:id="#+id/submit"
android:layout_marginTop="20px"
android:layout_toRightOf="#+id/search"/>
<TextView
android:id="#+id/catagory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Recent Movie Theaters"
android:layout_below="#id/search"
android:gravity="center"
android:layout_centerInParent="true"
android:textStyle="bold"/>
</RelativeLayout>
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:gravity="left|center"
android:layout_width="wrap_content"
android:paddingBottom="5px"
android:paddingTop="5px"
android:paddingLeft="5px">
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/icon"
android:textColor="#FFFF00"
android:text="hi"></TextView>
<TextView
android:text="#+id/TextView02"
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textStyle="bold"
android:textSize="25px"
android:textColor="#0099CC"
android:layout_toRightOf="#+id/TextView01"></TextView>
<TextView
android:text="#+id/TextView03"
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#0099CC"
android:layout_toRightOf="#+id/TextView01"
android:layout_below="#+id/TextView02"></TextView>
<TextView
android:text="#+id/TextView04"
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#0099CC"
android:layout_toRightOf="#+id/TextView01"
android:layout_below="#+id/TextView03"></TextView>
<Button
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:id="#+id/button"
android:layout_width="wrap_content"
android:text="Bye"
android:layout_alignParentRight="true"
android:layout_marginRight="10px">
</Button>
</RelativeLayout>
Have you tried,
removing l1.setItemsCanFocus(true); and simply using:
...
else {
showSplashScreen();
setContentView(R.layout.main);
final ListView l1 = (ListView) findViewById(android.R.id.list);
l1.setAdapter(new EfficientAdapter(this));
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Object listItem = l1.getItemAtPosition(position);
Log.v("MyApp", "get onItem Click position= " + position);
Toast.makeText(getApplicationContext(), listItem.toString(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Valid", Toast.LENGTH_SHORT).show();
}
});
}
and removing
convertView.setClickable(true);
convertView.setFocusableInTouchMode(true);
convertView.setFocusable(true);
and using it like:
public static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return country.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
holder.text3 = (TextView) convertView.findViewById(R.id.TextView03);
holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(curr[position]);
holder.text2.setText(country[position]);
holder.text3.setText(country[position]);
holder.text4.setText(country[position]);
return convertView;
}
static class ViewHolder {
TextView text4;
TextView text;
TextView text2;
TextView text3;
}
}
??
If you tried this,and it doesn't work,please show us your logcat for errors if you are getting any.
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;
}
}
i can not figure out the problem my listview suddenly disappeared. I searched solution and tried every possible change in xml file but i still get the empty screen.
xml for listview
<?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="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
listview items
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/dcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="drcode"
android:textColor="#000000" />
<TextView
android:id="#+id/dname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:text="docname"
android:textColor="#000000" />
<TextView
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:text="adr"
android:textColor="#000000" />
<TextView
android:id="#+id/cd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/adr"
android:text="class_desc"
android:textColor="#000000" />
<TextView
android:id="#+id/tvshift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:text="shift"
android:textColor="#000000" />
<TextView
android:id="#+id/tcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/docname"
android:text="terrcode"
android:textColor="#000000" />
code of listview adapter
public class planningListViewAdapter extends BaseAdapter {
Context context;
public ArrayList<Planning> planArraylist;
private static LayoutInflater inflater = null;
public Activity attdSetupActivity;
public planningListViewAdapter(Context context,ArrayList<Planning> planArraylist) {
this.context = context;
this.planArraylist = planArraylist;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return planArraylist.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 = inflater.inflate(R.layout.single_lv_item, null);
}
Planning p = planArraylist.get(position);
TextView tvdrCode = (TextView) convertView.findViewById(R.id.dcode);
TextView tvDrName = (TextView) convertView.findViewById(R.id.dname);
TextView tvTerr = (TextView) convertView.findViewById(R.id.tcode);
TextView tvAdr = (TextView) convertView.findViewById(R.id.a);
TextView tvClassDesc = (TextView) convertView.findViewById(R.id.cd);
//TextView tvSpeciality= (TextView) convertView.findViewById(R.id.speciaility);
TextView tvShift = (TextView) convertView.findViewById(R.id.tvshift);
tvdrCode.setText(p.getDrCode());
tvDrName.setText(p.getDocName());
tvTerr.setText(p.getTerrCode());
tvAdr.setText(p.getAdr());
tvClassDesc.setText(p.getClassDesc());
//tvSpeciality.setText(p.getSpeciality());
tvShift.setText(p.getShift());
return convertView;
}
}
Activity code
public class PlanningList_activity extends Activity{
private Db_sqlite dbHelper;
private planningListViewAdapter dataAdapter;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.planning_listview);
/* TextView columnHeader1 = (TextView) findViewById(R.id.column_header1);
TextView columnHeader2 = (TextView) findViewById(R.id.column_header2);
TextView columnHeader3 = (TextView) findViewById(R.id.column_header3);
TextView columnHeader4 = (TextView) findViewById(R.id.column_header4);
TextView columnHeader5 = (TextView) findViewById(R.id.column_header5);
TextView columnHeader6 = (TextView) findViewById(R.id.column_header6);
columnHeader1.setText("Plan No");
columnHeader2.setText("Plan Date");
columnHeader2.setText("Mon");
columnHeader2.setText("Dr Code");
columnHeader2.setText("Morn_Even");
columnHeader2.setText("FF_Code"); */
Log.e("on create", "yes");
listView = (ListView) findViewById(R.id.listView1);
dbHelper = new Db_sqlite(PlanningList_activity.this);
new AsyncLoadplanList().execute();
}
public class AsyncLoadplanList extends
AsyncTask<Void, Void, ArrayList<Planning>> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(PlanningList_activity.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
Log.e("pre execute", "yes");
}
#Override
public ArrayList<Planning> doInBackground(Void... params) {
try {
Log.e("In background", "yee");
return loadData();
} catch (Exception e) {
Log.e("get lost", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(ArrayList<Planning> result) {
super.onPostExecute(result);
dbHelper.close();
Log.e("data saving", "yoo");
dataAdapter = new planningListViewAdapter(getApplicationContext(),result);
listView.setAdapter(dataAdapter);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(getApplicationContext(), "data loaded",Toast.LENGTH_LONG).show();
}
private ArrayList<Planning> loadData() {
// Load data here from database
return dbHelper.getPlanningList();
}
}
Database method
public ArrayList<Planning> getPlanningList() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<Planning> planningList = new ArrayList<Planning>();
Cursor mCursor = db.query(DOC_PLANNING_TABLE_NAME, null,null,null,null,null,null);
if (mCursor != null && mCursor.moveToFirst()) {
do {
Planning planning = new Planning();
String docCode = mCursor.getString(mCursor.getColumnIndex(DCP_COLUMN_dr_code));
String planno = mCursor.getString(mCursor.getColumnIndex(DCP_COLUMN_PLAN_NO));
String terrCode = mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_terr_code));
String adr = mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_PLAN_DATE));
String classdesc= mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_MON));
String speciality= mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_REF_NO));
String shift= mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_MORN_EVEN));
planning.setDrCode(docCode);
planning.setDocName(planno);
planning.setTerrCode(terrCode);
planning.setAdr(adr);
planning.setClassDesc(classdesc);
planning.setShift(shift);
planning.setSpeciality(speciality);
} while (mCursor.moveToNext());
mCursor.close();
}
return planningList;
}
I have wasted a lot of time finding the problem but i cannnot understand why it is not showing on the screen. please guide me.
Correct in your code as below:
#Override
public Object getItem(int arg0) {
return planArraylist.get(planArraylist.get(arg0-1)); }
#Override
public long getItemId(int arg0) {
return arg0; }
I think you write you getView method differently and use a holder class, as shown below:
#Override
public Object getItem(int arg0) {
return planArraylist.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.single_lv_item, parent, false);
holder = new ViewHolder();
holder.tvdrCode = (TextView) convertView.findViewById(R.id.dcode);
holder.tvDrName = (TextView) convertView.findViewById(R.id.dname);
holder.tvTerr = (TextView) convertView.findViewById(R.id.tcode);
holder.tvAdr = (TextView) convertView.findViewById(R.id.a);
holder.tvClassDesc = (TextView) convertView.findViewById(R.id.cd);
holder.tvShift = (TextView) convertView.findViewById(R.id.tvshift);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Planning p = planArraylist.get(position);
holder.tvdrCode.setText(p.getDrCode());
holder.tvDrName.setText(p.getDocName());
holder.tvTerr.setText(p.getTerrCode());
holder.tvAdr.setText(p.getAdr());
holder.tvClassDesc.setText(p.getClassDesc());
holder.tvShift.setText(p.getShift());
return convertView;
}
static class ViewHolder {
TextView tvdrCode;
TextView tvDrName;
TextView tvTerr;
TextView tvAdr;
TextView tvClassDesc;
TextView tvShift;
}
Also, your list view xml file uses a Linear layout but you have properties that can only be found in a Relative layout such as android:layout_alignParentTop="true". Could you fix that as well?
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 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
I got a custom ListView, and I put three TextView in the List using a custom List Adapter! How I can to add a CheckBox with each List Item and make each CheckBox checked on list item click?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#7a4b9d"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/calorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sdf"
android:textColor="#ffffff" />
<TextView
android:id="#+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sdf"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/chkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
MainActivity
public class Main_Activity extends Fragment implements OnClickListener {
private ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.main_activity, null);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv = (ListView) root.findViewById(R.id.list_two);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setTextFilterEnabled(true);
lv.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
boolean result = searchResults.get(position).isSolved();
if (result) {
searchResults.get(position).setSolved(false);
} else {
searchResults.get(position).setSolved(true);
}
Toast.makeText(getActivity(),"You have chosen: " + " " +
fullObject.getPhone(), Toast.LENGTH_SHORT).show();
}
});
return root;
}
private ArrayList<SearchResults> GetSearchResults() {
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr = new SearchResults();
sr.setName("Apple");
sr.setCalorie("35");
sr.setPrice("5");
results.add(sr);
return results;
}
}
Custom Base Adapter
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCalorie = (TextView) convertView
.findViewById(R.id.calorie);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.chkBox1 = (CheckBox)convertView.findViewById(R.id.chkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCalorie.setText(searchArrayList.get(position)
.getCalorie());
holder.txtPrice.setText(searchArrayList.get(position).getPrice());
holder.chkBox1.setChecked(searchArrayList.get(position).isSolved());
return convertView;
}
static class ViewHolder {
CheckBox chkBox1;
TextView txtName;
TextView txtCalorie;
TextView txtPrice;
}
Search Results
public boolean isSolved() {
// TODO Auto-generated method stub
return b;
}
public void setSolved(boolean b) {
// TODO Auto-generated method stub
this.b = b;
}
You will have to define your CheckBox view in custom_row_view.xml file first,
<CheckBox android:id="#+id/chkBox1"
android:layout_width=...
android:layout_height=... />
Then, you wll have to call this reference in your MyCustomBaseAdapter class or in the holder in your case, like
CheckBox chkBox1;
then,
holder.chkBox1 = = (CheckBox)convertView.findViewById(R.id.chkBox1);
You can define a boolean value in your SearchResults class which can take care of chkBox check and use
holder.chkBox1.setChecked(searchArrayList.get(position).isSolved());
Something on these lines and you will be good to go :)
EDIT: Remember to change the value of boolean in searchResult instance on itemClick.
boolean result = searchResults.get(position).isSolved();
if (result) {
searchResults.get(position).setSolved(false);
} else {
searchResults.get(position).setSolved(true);
}
lv.getAdapter().notifyDataSetChanged();