Adding View Inside Listview - android

Hello Guys ,
I had edited my question with the screenshot, as you can see on the charge tab there is listview ,there is several block one below the other ,in the screenshot you can see 1 , with the truck number and charge 1 and charge 2 ,and below that there is another block items with same view as block 1.
What i need to do ,charge(i.e below LR No.) is coming from the webservice , there can be any number of charges , if there is 5 charges applied to truck number then it should be added below detention layout (i.e charge 2) .As (similiar to Charge 2 is added below charge 1)
BaseAdapter Class:
public class ApprovalAdapter extends BaseAdapter {
List<ApprovalPogo> list;
Context context;
LayoutInflater inflater;
public ApprovalAdapter( Context context,List<ApprovalPogo> list) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class Viewholder{
TextView truck_name;
TextView lr_no;
Button btn_approve;
Button btn_reject;
Button btn_approve_status_change;
Button btn_approve2;
Button btn_reject2;
Button btn_chnge_status_two;
TextView extra_height;
TextView detention;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Viewholder viewholder;
if(convertView==null){
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.single_row_approvals, parent, false);
viewholder=new Viewholder();
viewholder.truck_name = (TextView) convertView.findViewById(R.id.appr_txt_truck);
viewholder.lr_no = (TextView) convertView.findViewById(R.id.appr_lrNo);
viewholder.extra_height = (TextView) convertView.findViewById(R.id.extra_height);
viewholder.detention = (TextView) convertView.findViewById(R.id.txt_detention);
viewholder.btn_approve=(Button) convertView.findViewById(R.id.approve_btn);
viewholder.btn_reject=(Button) convertView.findViewById(R.id.reject_btn);
viewholder.btn_approve_status_change = (Button) convertView.findViewById(R.id.status_change_btn);
viewholder.btn_approve2=(Button) convertView.findViewById(R.id.status_charge_two_approve_btn);
viewholder.btn_reject2=(Button) convertView.findViewById(R.id.status_charge_two_reject_btn);
viewholder.btn_chnge_status_two=(Button) convertView.findViewById(R.id.status_change_charge_two_btn);
viewholder.btn_approve.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time=getTime();
viewholder.btn_approve.setVisibility(View.GONE);
viewholder.btn_reject.setVisibility(View.GONE);
viewholder.btn_approve_status_change.setVisibility(View.VISIBLE);
viewholder.btn_approve_status_change.setText("Accepted on "+time);
viewholder.btn_approve_status_change.setBackgroundResource(R.drawable.approval_change_status_backg);
}
});
viewholder.btn_reject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time=getTime();
viewholder.btn_reject.setVisibility(View.GONE);
viewholder.btn_approve.setVisibility(View.GONE);
viewholder.btn_approve_status_change.setVisibility(View.VISIBLE);
viewholder.btn_approve_status_change.setText("Rejected on "+time);
viewholder.btn_approve_status_change.setBackgroundResource(R.drawable.approval_reject_chng_status);
}
});
viewholder.btn_approve2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time=getTime();
viewholder.btn_approve2.setVisibility(View.GONE);
viewholder.btn_reject2.setVisibility(View.GONE);
viewholder.btn_chnge_status_two.setVisibility(View.VISIBLE);
viewholder.btn_chnge_status_two.setText("Accepted on "+time);
viewholder.btn_chnge_status_two.setBackgroundResource(R.drawable.approval_change_status_backg);
}
});
viewholder.btn_reject2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time=getTime();
viewholder.btn_reject2.setVisibility(View.GONE);
viewholder.btn_approve2.setVisibility(View.GONE);
viewholder.btn_chnge_status_two.setVisibility(View.VISIBLE);
viewholder.btn_chnge_status_two.setText("Rejected on "+time);
viewholder.btn_chnge_status_two.setBackgroundResource(R.drawable.approval_reject_chng_status);
}
});
convertView.setTag(viewholder);
}else{
viewholder=(Viewholder) convertView.getTag();
}
viewholder.truck_name.setText(list.get(position).getTruckName()) ;
viewholder.lr_no.setText(list.get(position).getLrNmbr()) ;
// viewholder.btn_approve_status_change.setText(list.get(position));
//
// for(int i=0;i<list.get(position).getApprovalSubListPogos().size();i++){
// viewholder.btn_approve_status_change.setText(list.get(position).getApprovalSubListPogos().get(i).getStatusChange()) ;
// viewholder.extra_height.setText(list.get(position).getApprovalSubListPogos().get(i).getHeight()) ;
// viewholder.detention.setText(list.get(position).getApprovalSubListPogos().get(i).getHeight()) ;
//
//
// }
Config.colorFont(context, null, viewholder.truck_name, null);
Config.colorFont(context, null, viewholder.lr_no, null);
Config.colorFont(context, null, viewholder.extra_height, null);
Config.colorFont(context, null, viewholder.detention, null);
Config.colorFont(context, null, null ,viewholder.btn_approve);
Config.colorFont(context, null, null, viewholder.btn_reject);
Config.colorFont(context, null, null, viewholder.btn_approve_status_change);
Config.colorFont(context, null, null, viewholder.btn_approve2);
Config.colorFont(context, null,null, viewholder.btn_reject2);
Config.colorFont(context, null,null, viewholder.btn_chnge_status_two);
return convertView;
}
#Override
public boolean areAllItemsEnabled()
{
return false;
}
#Override
public boolean isEnabled(int position)
{
return false;
}
public String getTime(){
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"+5.5));
Date currentLocalTime = cal.getTime();
System.out.println(currentLocalTime);
DateFormat date = new SimpleDateFormat("HH:mm a");
String localTime = date.format(Calendar.getInstance().getTime());
// date.setTimeZone(TimeZone.getTimeZone("GMT"));
// String localTime = date.format(currentLocalTime);
return localTime;
}
public void findDuplicate(){
for(int i=0;i<list.size();i++){
for(int j=i+1;j<list.size();j++){
if(list.get(j).getTruckName().equalsIgnoreCase(list.get(i).getTruckName())){
}
}
}
}
}
single_row_xml:
<?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:background="#drawable/truck_rounded_view"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/approval_header"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/appr_txt_truck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Truck MH01BQ4111"
android:layout_marginTop="#dimen/approval_margin"
android:layout_marginLeft="#dimen/approval_margin"
android:textSize="#dimen/approval_header_size"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textColor="#color/darkgray"
android:textStyle="bold" />
<TextView
android:id="#+id/appr_lrNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LR 788691"
android:layout_marginTop="#dimen/approval_margin"
android:layout_marginLeft="#dimen/approval_margin"
android:textSize="#dimen/approval_header_size"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginBottom="#dimen/approval_rl_top"
android:textColor="#color/darkgray"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<!--<View
android:layout_marginTop="#dimen/approval_margin"
android:layout_marginLeft="#dimen/approval_margin"
android:layout_marginRight="#dimen/approval_margin"
android:layout_width="match_parent"
android:layout_height="#dimen/approval_divider_height"
android:background="#color/white" />-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_marginTop="#dimen/approval_rl_top"
android:layout_marginLeft="#dimen/approval_margin"
android:layout_marginRight="#dimen/approval_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/appr_charge_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Charge 1"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textSize="#dimen/txt_approval_txt"
android:textColor="#color/white"
android:textStyle="bold" />
<Button
android:id="#+id/approve_btn"
android:layout_width="#dimen/approval_btn_width"
android:layout_height="#dimen/approval_btn_height"
android:background="#drawable/aprroval_aprrove_backg"
android:textColor="#color/white"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="#dimen/approval_padding_left"
android:clickable="true"
android:layout_alignParentRight="true"
android:textSize="#dimen/approval_text_size"
android:textStyle="bold"
android:text="APPROVE" />
<Button
android:visibility="gone"
android:id="#+id/status_change_btn"
android:layout_width="#dimen/approval_status_chnege_width"
android:layout_alignParentRight="true"
android:maxLines="2"
android:layout_height="#dimen/approval_btn_height"
android:background="#drawable/approval_change_status_backg"
android:gravity="center"
android:padding="#dimen/approval_margin"
android:textSize="#dimen/approval_text_size"
android:textStyle="bold"
android:textColor="#color/white"
android:text="Accepted on 3:00 PM" />
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="#dimen/approval_rl_top"
android:layout_marginLeft="#dimen/approval_margin"
android:layout_marginRight="#dimen/approval_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/extra_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Extra Height - Rs 200"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textSize="#dimen/txt_approval_txt"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/white"
android:textStyle="bold" />
<Button
android:id="#+id/reject_btn"
android:layout_width="#dimen/approval_btn_width"
android:layout_height="#dimen/approval_btn_height"
android:text="Reject"
android:textSize="#dimen/approval_text_size"
android:paddingLeft="#dimen/approval_padding_left"
android:gravity="center"
android:textColor="#color/white"
android:layout_alignParentRight="true"
android:background="#drawable/approval_reject_backg"
android:textStyle="bold"/>
</RelativeLayout>
<View
android:layout_marginTop="#dimen/approval_rl_top"
android:layout_marginLeft="#dimen/approval_margin"
android:layout_marginRight="#dimen/approval_margin"
android:layout_width="match_parent"
android:layout_height="#dimen/approval_divider_height"
android:background="#color/white" />
<RelativeLayout
android:layout_marginTop="#dimen/approval_rl_top"
android:layout_marginLeft="#dimen/approval_margin"
android:layout_marginRight="#dimen/approval_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/charge_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Charge 2 "
android:layout_gravity="center"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textSize="#dimen/txt_approval_txt"
android:textColor="#color/white"
android:textStyle="bold" />
<Button
android:id="#+id/status_charge_two_approve_btn"
android:layout_width="#dimen/approval_btn_width"
android:layout_height="#dimen/approval_btn_height"
android:background="#drawable/aprroval_aprrove_backg"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#color/white"
android:layout_alignParentRight="true"
android:paddingLeft="#dimen/approval_padding_left"
android:textSize="#dimen/approval_text_size"
android:textStyle="bold"
android:text="APPROVE" />
<Button
android:visibility="gone"
android:id="#+id/status_change_charge_two_btn"
android:layout_width="#dimen/approval_status_chnege_width"
android:layout_alignParentRight="true"
android:maxLines="2"
android:layout_height="#dimen/approval_btn_height"
android:background="#drawable/approval_change_status_backg"
android:textStyle="bold"
android:textColor="#color/white"
android:gravity="center"
android:paddingLeft="#dimen/approval_padding_left"
android:textSize="#dimen/approval_text_size"
android:text="Rejected on 3:00 PM" />
</RelativeLayout>
<RelativeLayout
android:layout_marginTop="#dimen/approval_rl_top"
android:layout_marginLeft="#dimen/approval_margin"
android:layout_marginRight="#dimen/approval_margin"
android:layout_marginBottom="#dimen/approval_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt_detention"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detention - Rs 200"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:textSize="#dimen/txt_approval_txt"
android:textColor="#color/white"
android:textStyle="bold" />
<Button
android:id="#+id/status_charge_two_reject_btn"
android:layout_width="#dimen/approval_btn_width"
android:layout_height="#dimen/approval_btn_height"
android:gravity="center"
android:layout_alignParentRight="true"
android:text="REJECT"
android:textColor="#color/white"
android:background="#drawable/approval_reject_backg"
android:textStyle="bold"
android:paddingLeft="#dimen/approval_padding_left"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

please use Expandable ListView to better use.
for more information and example about Expandable listview

You are not supposed to put scrollable things into scrollable things, and you usually shouldn't need to in the first place. This won't work most of the time.

Related

how to set the specific row item option(visible, invisible) in listview in android

frist my english skill weak.
description>
this is list view.
ㅡㅡㅡㅡㅡㅡㅡㅡ
ㅣㅡㅡㅡㅡㅡㅣㅢ <-- this is button , i init set invisible
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡ ////// <-- i want make visible button
ㅣㅡㅡㅡㅡㅡ ////// <-- specific position
I make the custom ListView
ListView row contains texts, Button.
The Button is set invisible option in xml file.
then, I want set the visible specific row button.
I tried that and failed
after make ArrayList for ListView, marking matching position like this
for(i=0; i<arraylist.size(); i++){
int t41=Integer.parseInt(arraylist.get(i).getm());
if(month == t41){
confirm_replay[i]=true;
temp55=i;
}
}
I can set the textValue. through adapter.getItem(int position).
but i don't know, how to control specific button.
also try add button into adapter. failed..
also search question in google but my eng skill bad. failed
add my code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:paddingTop="10dp"
android:text="match day(weekend)"
android:textSize="28dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:background="#2d000000"
android:layout_width="match_parent"
android:layout_height="3dp">
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
</b>
adapter
class MlistViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<MatchInfomation> matchinformationlist = null;
private ArrayList<MatchInfomation> arraylist;
public MlistViewAdapter(Context context,
List<MatchInfomation> matchinformationlist) {
mContext = context;
this.matchinformationlist = matchinformationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<MatchInfomation>();
this.arraylist.addAll(matchinformationlist);
}
public class ViewHolder {
TextView m;
TextView d;
TextView yoil;
TextView vsTeam;
TextView time;
TextView league;
}
#Override
public int getCount() {
return matchinformationlist.size();
}
#Override
public MatchInfomation getItem(int position) {
return matchinformationlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.activity_match_list, null);
button_youtube.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra("query", "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
});
// Locate the TextViews in listview_item.xml
holder.m = (TextView) view.findViewById(R.id.m);
holder.d = (TextView) view.findViewById(R.id.d);
holder.yoil = (TextView) view.findViewById(R.id.yoil);
holder.vsTeam = (TextView) view.findViewById(R.id.vsTeam);
holder.time = (TextView) view.findViewById(R.id.time);
holder.league = (TextView) view.findViewById(R.id.league);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.m.setText(matchinformationlist.get(position).getm());
holder.d.setText(matchinformationlist.get(position).getd());
holder.yoil.setText(matchinformationlist.get(position).getyoil());
holder.vsTeam.setText(matchinformationlist.get(position).getvsTeam());
holder.time.setText(matchinformationlist.get(position).gettime());
holder.league.setText(matchinformationlist.get(position).getleague());
return view;
}
}
If you want to adjust a specific row, you can use the position parameter in your getView() method in adapter. For instance;
if(position==55){
holder.m.setVisibility(View.GONE);
}
I'm not sure i understand your question.
If i'm right you juste want your button to be invisble for specific row. To do so add the specific line at the end of your getView method
yourButton.setVisibility(View.INVISIBLE)
If you want to hide the entire row, the best way will probably be to change your adapter to diplay only row with content.
before setting the values to the view check the condition whatever you want like:-
if condition is true then set your view visible
else set your view invisible
if(true){
button.setVisibility(View.VISIBLE);
}else{
button.setVisibility(View.GONE);
}

OnClickListner not called on the whole View

On an Android application I am developing i got this situation.
In a Fragment there is a ListView that contains a list of customers (stored in instances of the Customer_InputOld class) using a customized ArrayAdapter (named ArrayAdapter_ClientiInput). The single row contains a ViewFlipper with two TextViews and seven nearly identical TextViews.
I put a ClickListner and a TouchListner in the Adapter, so on the click (and the touch) of a line the application make something. This works, except for the fact that these events are not triggered on the whole row.
Only the rough 80% of the row is "clickable", while the last textView does not respond to the listner.
Sincerely I don't know what code to post cause I have no idea where the problem could be: I tried to toast the View.getWidth() on the Click of the View itself, but actually the width is 1280 px, the entire device's width.
The single row is inflated with this customer_row.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".MyActivity">
<ViewFlipper
android:id="#+id/capitalViewFlipper"
android:layout_width="75dp"
android:layout_height="75dp">
<TextView
android:id="#+id/customerLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABCDEF"
android:gravity="center"
android:text="D"
android:textColor="#fff"
android:textSize="28pt" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBBB"
android:gravity="center"
android:src="#drawable/ic_selected_done" />
</ViewFlipper>
<TextView
android:id="#+id/ragioneSocialeViewList"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/capitalViewFlipper"
android:gravity="center_vertical"
android:padding="10dip"
android:singleLine="true"
android:text="Dummy Customer"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="#+id/canaleViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignLeft="#id/ragioneSocialeViewList"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:gravity="center"
android:padding="10dip"
android:text="Dummy type"
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="#+id/sollecitiViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/ragioneSocialeViewList"
android:gravity="center"
android:padding="10dip"
android:text="2"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/tipoViewList"
android:layout_width="125dp"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_toRightOf="#+id/sollecitiViewList"
android:gravity="center"
android:padding="10dip"
android:text="F/S"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/emailViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/telefonoViewList"
android:layout_alignRight="#+id/telefonoViewList"
android:layout_alignTop="#+id/capitalViewFlipper"
android:gravity="center"
android:padding="10dip"
android:text="dummymail#gmail.com"
android:textAlignment="center"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/telefonoViewList"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_marginBottom="-7dp"
android:layout_toRightOf="#+id/tipoViewList"
android:gravity="center"
android:padding="10dip"
android:text="tel. 035424344"
android:textColor="#000"
android:textSize="22dp" />
<TextView
android:id="#+id/ultimoOrdineViewList"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_alignBottom="#+id/capitalViewFlipper"
android:layout_alignTop="#+id/capitalViewFlipper"
android:layout_gravity="center"
android:layout_toRightOf="#+id/emailViewList"
android:capitalize="words"
android:gravity="center_vertical|right"
android:padding="10dip"
android:singleLine="true"
android:text="21/12/2012"
android:textColor="#000"
android:textSize="22dp" />
</RelativeLayout>
While the Adapter Code is this:
public class ArrayAdapter_ClientiInput extends ArrayAdapter<CustomerInputOld> {
public EventLog.Event ImageTouched;
//public ListView customerListView;
private ArrayList<CustomerInputOld> list;
private Context context;
//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public ArrayAdapter_ClientiInput(Context context, int textViewResourceId, ArrayList<CustomerInputOld> rowDataList) {
//populate the local list with data.
super(context, textViewResourceId, rowDataList);
this.list = new ArrayList<CustomerInputOld>();
this.list.addAll(rowDataList);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
//creating the ViewHolder we defined earlier.
ViewHolder_CustomerRow holder = new ViewHolder_CustomerRow();
//creating LayoutInflater for inflating the row layout.
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflater.inflate(R.layout.customers_row, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.ragioneSocialeViewList);
holder.capital = (TextView) convertView.findViewById(R.id.customerLogo);
holder.flipper = (ViewFlipper) convertView.findViewById(R.id.capitalViewFlipper);
//holder.line = (RelativeLayout) convertView.findViewById(R.id.customer_whole_row);
holder.canale = (TextView) convertView.findViewById(R.id.canaleViewList);
holder.solleciti = (TextView) convertView.findViewById(R.id.sollecitiViewList);
holder.email = (TextView) convertView.findViewById(R.id.emailViewList);
holder.tipo = (TextView) convertView.findViewById(R.id.tipoViewList);
holder.numero = (TextView) convertView.findViewById(R.id.telefonoViewList);
holder.ultimoOrdine = (TextView) convertView.findViewById(R.id.ultimoOrdineViewList);
//define an onClickListener for the CheckBox.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getContext(),Integer.toString(v.getWidth()),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), CustomerDetail.class);
intent.putExtra("name", list.get(position).getCompany());
getContext().startActivity(intent);
}
});
convertView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == android.view.MotionEvent.ACTION_DOWN) {
v.setBackgroundColor(0xFAAA);
} else if ( e.getAction() == android.view.MotionEvent.ACTION_UP ||
e.getAction() == android.view.MotionEvent.ACTION_CANCEL) {
isSelected(position, v);
}
//Ritorno false per permettere anche al OnClickListener di agire..
return false;
}
});
holder.flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CustomerListFragment.onRowIconClicked(position, (RelativeLayout) v.getParent(), (ViewFlipper) v, context);
}
});
//setting data into the the ViewHolder.
holder.title.setText(list.get(position).getCompany());
holder.capital.setText(list.get(position).getCompany().substring(0, 1).toUpperCase());
holder.capital.setBackgroundColor(list.get(position).getIconColor());
holder.canale.setText(list.get(position).getChannel());
holder.solleciti.setText(list.get(position).getReminders());
if (Integer.parseInt(list.get(position).getReminders()) >= 3)
holder.solleciti.setTextColor(0xffff0000);
holder.email.setText(list.get(position).getEmail());
holder.numero.setText("tel. " + list.get(position).number);
holder.ultimoOrdine.setText(new SimpleDateFormat("c d MMM ''yy").format(list.get(position).getLastOrderDate()));
if (list.get(position).getInvoiceCustomer() == null) {
if (list.get(position).getDeliveryCustomer() == null) {
holder.tipo.setText("F/S");
} else {
holder.tipo.setText("F");
}
} else {
holder.tipo.setText("S");
}
//modifiche nel caso in cui sia selezionato
if ( isSelected(position,convertView) ) {
holder.flipper.showNext();
}
//return the row view.
return convertView;
}
private boolean isSelected(int position, View v){
if (position == CustomerListFragment.customerSelected) {
v.setBackgroundColor(0xFFE6C86F);
return true;
} else {
v.setBackgroundColor(0xFFFFFFFF);
return false;
}
}
}
Solution by OP.
In the last TextView I put android:capitalize="words", and that is an EditText's attribute, not Textview's! So when I clicked on the last "20%" of screen - coincidentally, that TextBox- tha app thinks that the click was on the TextView and not the line!

Android ArrayAdapter, BaseAdapter not calling getView()

I am extending ListFragment to display data retrieved from a web server in an AsyncTask in another class. If I set a custom height (i.e. 400dp) on the #android:id/list widget in my xml, getView() does not get called at all. If I set it to wrap_content, getView() gets called for the list items but the list items don't show up! It seems that the listview does not expand to fit the item heights, even though the list item's xml specifies a layout height. When a fixed height is defined I can see the colored background of the list, but no data. getCount() never returns 0.
Part of the adapter class:
public class gAdapter extends ArrayAdapter<GroupObject> {
ArrayList<GroupObject> groups;
public gAdapter(Context context, int textViewResId, ArrayList<GroupObject> groups) {
super(context, R.layout.groups_listview_row, groups);
mInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
con = context;
this.groups = groups;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
View row = convertView;
if(convertView == null)
row = mInflater.inflate(R.layout.groups_listview_row, parent, false);
TextView name = (TextView) row.findViewById(R.id.groups_listview_name);
//Set name
try {
name.setText( groups.get(position).groupName);
Log.d("test", "Got name " + groups.get(position).groupName);
} catch(Exception e) {
e.printStackTrace();
name.setText("Could not load name");
}
return row;
}
#Override
public int getCount() {
Log.d("test", "Returning count " + (this.groups.size() != 0 ? this.groups.size() : 0));
return this.groups.size() != 0 ? this.groups.size() : 0;
}
#Override
public GroupObject getItem(int position) {
Log.d("test", "get item " + position);
return this.groups.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
groups_listview_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/groups_listview_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/avatar" />
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/groups_listview_imageview"
android:layout_alignTop="#+id/groups_listview_imageview"
android:layout_toRightOf="#+id/groups_listview_imageview" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/groups_listview_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0.8"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groups_listview_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="5dp"
android:gravity="right|bottom"
android:singleLine="true"
android:text="zCode: 000000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/LinearLayout1"
android:layout_gravity="bottom"
android:gravity="bottom" >
<TextView
android:id="#+id/groups_listview_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_weight="0.8"
android:gravity="left|bottom"
android:text="Location" />
<TextView
android:id="#+id/groups_listview_numMembers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginRight="10dp"
android:layout_weight="0.2"
android:gravity="right|bottom"
android:text="0 Members" />
</LinearLayout>
</RelativeLayout>

custom listview edittext is not editable on button click when i use descedantsfocusability="blockdescedant" [duplicate]

I have a customized ListView. Each row has an EditText, Buttons & TextView. In order to make the ListView items clickable I have kept android:descendantFocusability="blocksDescendants" for row layout. If I don't keep the descendantFocusability I am not able to implement an action for onItemClick. If I keep descendantFocusability the EditTextwhich is present in my row is not gaining focus. I want the EditText focusable and also I should be able to click on each row to navigate to another Activity. Can anyone please help me in this. Thanks all.
Edit : In the CustomAdapter for EditText, I tried keeping onTouchListenerand also onClickListenerwhere I requestFocus but that does not seem to work.
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recentrowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/margin_left_5"
android:clickable="true"
tools:ignore="UseCompoundDrawables,HardcodedText,ContentDescription,UselessParent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_bg"
android:padding="#dimen/margin_left_5" >
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/addSubscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/flikart_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FLIKART"
android:textColor="#color/gray"
android:textSize="#dimen/medium_text_size" />
<ImageView
android:id="#+id/addToFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
<LinearLayout
android:id="#+id/rl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl1"
android:layout_marginLeft="#dimen/margin_30"
android:layout_marginRight="#dimen/margin_30"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/text_desciption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:lines="2"
android:text="20% off on Smart Phones and basic Handsets has upto 50% OFF only"
android:textColor="#color/blue" />
<TextView
android:id="#+id/text_desciption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:lines="1"
android:text="somethign something,......."
android:textColor="#color/gray" />
<TextView
android:id="#+id/couponTypeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_left_10"
android:background="#drawable/button_bg"
android:gravity="center"
android:padding="#dimen/margin_left_10"
android:text="STEAL THE DEAL"
android:textColor="#color/white"
android:textSize="#dimen/little_small_text_size" />
</LinearLayout>
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl2"
android:layout_marginTop="#dimen/margin_left_10" >
<TextView
android:id="#+id/text_offer_expiry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/margin_left_5"
android:text="Ends 10 days"
android:textColor="#color/red"
android:textSize="16sp" />
<RelativeLayout
android:id="#+id/rightLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" >
<ImageView
android:id="#+id/comment_image_total"
android:layout_width="#dimen/dimenstion_25"
android:layout_height="#dimen/dimenstion_20"
android:background="#drawable/comments" />
<TextView
android:id="#+id/text_comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/comment_image_total"
android:text="100 Comments"
android:textColor="#color/gray"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#id/rightLayout"
android:layout_toRightOf="#id/text_offer_expiry"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/like_image_total"
android:layout_width="#dimen/dimenstion_25"
android:layout_height="#dimen/dimenstion_25"
android:background="#drawable/like" />
<TextView
android:id="#+id/text_total_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/like_image_total"
android:text="999 Likes"
android:textColor="#color/gray"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<View
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:layout_below="#id/rl3"
android:layout_marginTop="#dimen/margin_5"
android:background="#color/purple_clor" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/title_bar_height"
android:layout_below="#id/view"
android:layout_centerVertical="true"
android:layout_marginTop="#dimen/margin_5" >
<RelativeLayout
android:id="#+id/likesLayout"
android:layout_width="#dimen/dimenstion_40"
android:layout_height="#dimen/dimenstion_40"
android:layout_centerVertical="true"
android:background="#drawable/unratedbkg" >
<Button
android:id="#+id/likesBtn"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/unrated" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/footermain"
android:layout_width="fill_parent"
android:layout_height="#dimen/dimenstion_40"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/likesLayout"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/animLayout"
android:layout_width="100dp"
android:layout_height="#dimen/dimenstion_40"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/likesLayout"
android:background="#drawable/ratingbkg"
android:visibility="gone" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="#dimen/margin_5" >
<ImageView
android:id="#+id/like_image"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/like" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/margin_5" >
<ImageView
android:id="#+id/dislike_image"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/dislike" />
</RelativeLayout>
</RelativeLayout>
<EditText
android:id="#+id/add_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/animLayout"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Add Comment"
android:inputType="text" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
please Don't use setOnItemClickListener for item click .. i think that you should be use item view click inside adapter method
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "click item",Toast.LENGTH_LONG).show();
}
});
Remove this from main list item layout
android:descendantFocusability="blocksDescendants"
Thanks and enjoy this code !
Try to add this line to your activity in the manifest.xml
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
you do like this:
first set in your listview's android:focusable="false";
then you want row to be clicked:
for this in your customAdapter you should do like this
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Hello world",2000).show();
}
});
It will work.
By default, your items should now have the click option, when you made android:focusable="false" in listview.
No need to use descendantFocusability in listview.
main.xml
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true" >
</ListView>
Create ArrayList add data into the arraylist.
lv=(ListView)findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
for(int i=0;i<30 data-blogger-escaped-br="" data-blogger-escaped-i=""> list.add(i);
}
1)create adapter for the listview and set the position as tag for the edittext.
2)Normally,when scrolling the item position will change.So,you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.
holder.caption = (EditText) convertView
.findViewById(R.id.editText12);
holder.caption.setTag(position);
holder.caption.setText(list.get(position).toString());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);
Finally,add the text watcher to the edittext and store the changes into correct position in the list.
holder.caption.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
final int position2 = holder.caption.getId();
final EditText Caption = (EditText) holder.caption;
if(Caption.getText().toString().length()>0){
list.set(position2,Integer.parseInt(Caption.getText().toString()));
}else{
Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
}
});
Refer this link..http://velmuruganandroidcoding.blogspot.in/2014/08/edittext-in-listview-android-example.html
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/MyList" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:descendantFocusability="beforeDescendants">
</ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="#+id/ItemCaption"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_marginLeft="2dip" android:singleLine="true">
</EditText>
</LinearLayout>
AndroidCustomListViewActivity
public class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView) findViewById(R.id.MyList);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
public MyAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
ListItem listItem = new ListItem();
listItem.caption = "Caption" + i;
myItems.add(listItem);
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
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) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.caption = (EditText) convertView
.findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
holder.caption.setText(myItems.get(position).caption);
holder.caption.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
myItems.get(position).caption = Caption.getText().toString();
}
}
});
return convertView;
}
}
class ViewHolder {
EditText caption;
}
class ListItem {
String caption;
}
}

Binding data to 5 different textbox in a linear layout

I have obtained the xml response after parsing the web response and here it is
<NewDataSet>
<Table>
<Restaurant_ID>1705</Restaurant_ID>
<restaurant_name>1947</restaurant_name>
<address>296, Ram Towers, 100 Feet Ring Road, Banashankari, Bangalore</address>
<costoftwo>Approx Rs. 600 for two (without alcohol)</costoftwo>
<rating>3.3</rating>
<timings>11 AM to 11 PM</timings>
<Phone>08026791213</Phone>
<Location_ID>34</Location_ID>
<Location_name>Banashankari</Location_name>
<city_ID>1</city_ID>
<city_name>Bangalore</city_name>
<Cuisine_ID>8</Cuisine_ID>
<Cuisine_name>North Indian</Cuisine_name>
</Table>
and i have a XML layout to which i need to bind the above response. my Xml layout is
<?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="match_parent"
android:gravity="right"
android:orientation="vertical"
android:weightSum="1.0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/headerbgcolor"
android:weightSum="1.0"
>
<Button
android:id="#+id/restaurant_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:onClick="finishActivity"
android:text="#string/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center"
android:text="#string/restauran_details" />
<Button
android:id="#+id/restaurant_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="#string/home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/Refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/refresh" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/Restaurant_name"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="#string/restaurant_name"
/>
<TextView
android:id="#+id/Restaurant_add"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="#string/restaurant_address"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="#+id/Restaurant_rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_rating"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_timings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_timings"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_cstfortwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/cost_for_two"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_cuisine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_cuisine"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_location"
android:layout_weight="0.1" />
<TextView
android:id="#+id/Restaurant_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/restaurant_city"
android:layout_weight="0.1" />
</LinearLayout>
<TextView
android:id="#+id/Restaurant_phone"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="#string/restaurant_phone"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/footerimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#808000"
android:src="#drawable/btn_bg_cuisine" />
</RelativeLayout>
</LinearLayout>
And i dont know whether it's good or not, but i am using customadapter to bind. Here is my customadapter.
public class CustomAdapterforRestaurant extends BaseAdapter {
private List<Item> mlist = null;
private Context mcontext;
private LayoutInflater l_Inflater;
public CustomAdapterforRestaurant(Context context, List<Item> lstresponse) {
mlist = lstresponse;
mcontext = context;
l_Inflater = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolderforcities holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.restaurantdetails, null);
holder = new ViewHolderforcities(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolderforcities) convertView.getTag();
}
holder.restaurant_location.setText(mlist.get(position).getLocation_name());
holder.restaurant_name.setText(mlist.get(position).getRestaurant_name());
holder.restaurant_add.setText(mlist.get(position).getAddress());
holder.restaurant_rating.setText(mlist.get(position).getRating());
holder.restaurant_timings.setText(mlist.get(position).getTimings());
holder.restaurant_cstfortwo.setText(mlist.get(position).getCostoftwo());
holder.restaurant_cuisine.setText(mlist.get(position).getCuisine_name());
holder.restaurant_city.setText(mlist.get(position).getCity_name());
holder.restaurant_phone.setText(mlist.get(position).getPhone());
return convertView;
}
class ViewHolderforcities {
TextView restaurant_name,restaurant_add,restaurant_rating,restaurant_timings,
restaurant_cstfortwo,restaurant_cuisine,restaurant_location,restaurant_city,
restaurant_phone;
public ViewHolderforcities(View base)
{
restaurant_name = (TextView) base.findViewById(R.id.Restaurant_name);
restaurant_add = (TextView) base.findViewById(R.id.Restaurant_add);
restaurant_rating = (TextView) base.findViewById(R.id.Restaurant_rating);
restaurant_timings = (TextView) base.findViewById(R.id.Restaurant_timings);
restaurant_cstfortwo = (TextView) base.findViewById(R.id.Restaurant_cstfortwo);
restaurant_cuisine = (TextView) base.findViewById(R.id.Restaurant_cuisine);
restaurant_location = (TextView) base.findViewById(R.id.Restaurant_location);
restaurant_city = (TextView) base.findViewById(R.id.Restaurant_city);
restaurant_phone = (TextView) base.findViewById(R.id.Restaurant_phone);
}
}
Can some 1 please explain me how will i bind the data to it.
Thanks
Ramu.
You need to parse this XML using one of the available XML parsers (The core android reference is http://developer.android.com/training/basics/network-ops/xml.html). Once you have got the information from the xml, You fill in the textViews programaticly using the textView.setText("text") method.

Categories

Resources