listview does not show anything - android

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?

Related

My list view is lagging can anyone help me to fix this?

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;
}
}

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;
}
}

How to get id of particular item of listview in android?

I am using Listview and set the multiple item and each item have Like, share and comment option.
Actually I have fetch the Item Id and also setText of TextView, Its Successfully. But Not able to get Item Id on Like Button Click event for particular item.
How to fetch Item Id for every items in Listview on Button Click Event?
Demo for MyAdapter Class:
public class Adapter1 extends BaseAdapter implements OnClickListener {
public ArrayList<HashMap<String, String>> arr = null;
Context context = null;
LayoutInflater layoutInflater = null;
HashMap<String, String> getData = new HashMap<String, String>();
String urlLike = null;
TextView strId = null;
public NewsFeedAdapter(Context context,
ArrayList<HashMap<String, String>> arr) {
this.context = context;
this.arr= arr;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return arr.size();
}
#Override
public Object getItem(int position) {
return arr.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.my_list_item, null);
/** initialize ViewHolder */
viewHolder = new ViewHolder();
/** Initialize Widgets */
/** Imageview */
viewHolder.imgLike = (ImageView) convertView
.findViewById(R.id.imgLike);
viewHolder.imgLike.setOnClickListener(this);
viewHolder.imgComments = (ImageView) convertView
.findViewById(R.id.imgComments);
viewHolder.imgComments.setOnClickListener(this);
viewHolder.imgShare = (ImageView) convertView
.findViewById(R.id.imgShare);
viewHolder.imgShare.setOnClickListener(this);
/** TextView */
viewHolder.txtId = (TextView) convertView
.findViewById(R.id.txtId);
viewHolder.txtDescription = (TextView) convertView
.findViewById(R.id.txtDescription);
getData = arr.get(position);
viewHolder.txtId.setText(getData
.get(Fragment1.TAG_ID));
viewHolder.txtDescription.setText(getData
.get(Fragment1.TAG_MESSAGE));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return convertView;
}
/** ViewHolder Class */
public class ViewHolder {
ImageView imgLike = null, imgComments = null,
imgShare = null;
TextView txtId = null,txtDescription = null;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgLike:
strId = ((TextView) view
.findViewById(R.id.txtId));
System.out.println("On Click====="
+ strId.getText().toString());
urlLike = "http://www.example.com?id="
+ strId.getText().toString() + "&user_id="
+ myDetail.getUserId();
new sendLikeData().execute();
break;
case R.id.imgComments:
break;
case R.id.imgShare:
break;
default:
break;
}
}
public class sendLikeData extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(urlLike, ServiceHandler.GET);
Log.d("Response : Like", ">" + jsonStr);
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
};
}
}
OutPut is, NullPointer Exception in System.out.println("On Click====="
+ strId.getText().toString());.
xml file is,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/masterLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/five_dp" >
<TextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/ten_dp" />
<ImageView
android:id="#+id/imgImage"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#+id/txtDescription"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/ten_dp"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/ten_dp"
android:background="#null"
android:contentDescription="#string/content_description"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imgImage"
android:background="#drawable/strip"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="#string/content_description"
android:src="#drawable/like" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imgLike"
android:layout_toRightOf="#+id/imgLike"
android:background="#drawable/getbg"
android:gravity="center"
android:padding="#dimen/two_dp"
android:textColor="#color/white"
android:textSize="#dimen/ten_sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="#string/content_description"
android:src="#drawable/comments" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgComments"
android:background="#drawable/getbg"
android:gravity="center"
android:padding="#dimen/two_dp"
android:text="+1"
android:textColor="#android:color/white"
android:textSize="#dimen/ten_sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="#string/content_description"
android:src="#drawable/share" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="#+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="#dimen/ten_dp"
android:text="id"
android:textSize="20dp" />
</RelativeLayout>
And Logcat is,
11-24 18:54:20.354: E/AndroidRuntime(7120): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
11-24 18:54:20.354: E/AndroidRuntime(7120): Adapter.onClick(Fragment.java:342)
11-24 18:54:20.354: E/AndroidRuntime(7120): at android.view.View.performClick(View.java:4562)
11-24 18:54:20.354: E/AndroidRuntime(7120): at android.view.View$PerformClick.run(View.java:18918)
11-24 18:54:20.354: E/AndroidRuntime(7120): at android.os.Handler.handleCallback(Handler.java:808)
11-24 18:54:20.354: E/AndroidRuntime(7120): at android.os.Handler.dispatchMessage(Handler.java:103)
11-24 18:54:20.354: E/AndroidRuntime(7120): at android.os.Looper.loop(Looper.java:193)
11-24 18:54:20.354: E/AndroidRuntime(7120): at android.app.ActivityThread.main(ActivityThread.java:5388)
11-24 18:54:20.354: E/AndroidRuntime(7120): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 18:54:20.354: E/AndroidRuntime(7120): at java.lang.reflect.Method.invoke(Method.java:515)
11-24 18:54:20.354: E/AndroidRuntime(7120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-24 18:54:20.354: E/AndroidRuntime(7120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
11-24 18:54:20.354: E/AndroidRuntime(7120): at dalvik.system.NativeStart.main(Native Method)
Try with below code:
public class Adapter1 extends BaseAdapter implements OnClickListener {
public ArrayList<HashMap<String, String>> arr = null;
Context context = null;
LayoutInflater layoutInflater = null;
HashMap<String, String> getData = new HashMap<String, String>();
String urlLike = null;
TextView strId = null;
public NewsFeedAdapter(Context context,
ArrayList<HashMap<String, String>> arr) {
this.context = context;
this.arr= arr;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return arr.size();
}
#Override
public Object getItem(int position) {
return arr.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.my_list_item, null);
/** initialize ViewHolder */
viewHolder = new ViewHolder();
/** Initialize Widgets */
/** Imageview */
viewHolder.imgLike = (ImageView) convertView
.findViewById(R.id.imgLike);
viewHolder.imgComments = (ImageView) convertView
.findViewById(R.id.imgComments);
viewHolder.imgComments.setOnClickListener(this);
viewHolder.imgShare = (ImageView) convertView
.findViewById(R.id.imgShare);
viewHolder.imgShare.setOnClickListener(this);
/** TextView */
viewHolder.txtId = (TextView) convertView
.findViewById(R.id.txtId);
viewHolder.txtDescription = (TextView) convertView
.findViewById(R.id.txtDescription);
getData = arr.get(position);
viewHolder.txtId.setText(getData
.get(Fragment1.TAG_ID));
viewHolder.txtDescription.setText(getData
.get(Fragment1.TAG_MESSAGE));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
viewHolder.imgLike.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
strId=arr.get(position).getId();//or whatever you want to do do here.
}
});
return convertView;
}
/** ViewHolder Class */
public class ViewHolder {
ImageView imgLike = null, imgComments = null,
imgShare = null;
TextView txtId = null,txtDescription = null;
}
}
Try this:
strId = (TextView) view.getParent().findViewById(R.id.txtId);
This will fix your crash, but you need to rethink about your design.
What u have to do is set Diff onclick Listeners on diff views
viewHolder.txtDescription.setTag(R.id.txtDescription,position);
viewHolder.txtDescription.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int position_clicked=(Integer)v.getTag();
}
});
repeat it for every view your want to set listener
You need to initialize strId in getView()
here in the following code, its not able to initialize it based on "view" object, hence you get nullpointer.
case R.id.imgLike:
strId = ((TextView) view.findViewById(R.id.txtId));
mark as solved if you find the answer useful.
You can also use onClickListener inside your adapter class.
Look this sample code
viewHolder.imgLike = (ImageView) convertView.findViewById(R.id.imgLike);
viewHolder.imgLike.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String id = getData.get(Fragment1.TAG_ID);
}
});
try like this,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.my_list_item, null);
/** initialize ViewHolder */
viewHolder = new ViewHolder();
/** Initialize Widgets */
/** Imageview */
viewHolder.imgLike = (ImageView) convertView
.findViewById(R.id.imgLike);
viewHolder.imgLike.setOnClickListener(this);
viewHolder.imgLike.setTag(position);
viewHolder.imgComments = (ImageView) convertView
.findViewById(R.id.imgComments);
viewHolder.imgComments.setOnClickListener(this);
viewHolder.imgShare = (ImageView) convertView
.findViewById(R.id.imgShare);
viewHolder.imgShare.setOnClickListener(this);
/** TextView */
viewHolder.txtId = (TextView) convertView
.findViewById(R.id.txtId);
viewHolder.txtDescription = (TextView) convertView
.findViewById(R.id.txtDescription);
getData = arr.get(position);
viewHolder.txtId.setText(getData
.get(Fragment1.TAG_ID));
viewHolder.txtDescription.setText(getData
.get(Fragment1.TAG_MESSAGE));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return convertView;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgLike:
View view = listview.getAdapter().getView(((ImageView)v).getTag(), null, listview);
txtId = (TextView) view.findViewById(R.id.txtId);
System.out.println("On Click====="
+ strId.getText().toString());
urlLike = "http://www.example.com?id="
+ strId.getText().toString() + "&user_id="
+ myDetail.getUserId();
new sendLikeData().execute();
break;
case R.id.imgComments:
break;
case R.id.imgShare:
break;
default:
break;
}
}

Adding radio button to listview

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

onClick does not work with custom listview

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.

Categories

Resources