Binding data to 5 different textbox in a linear layout - android

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.

Related

why CardView causing slow down the scrolling process?

I was using listView without with the custom layout which is having
Linear layout as parent layout. Scrolling was fine, Then I need to
implement CardView for elevation and round corner, After implementation
list Scrolling become slow.Moreover it is also slowing down the
RecyclerView Process.
here is my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/WhiteSmoke"
android:layout_margin="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="12dp"
card_view:cardElevation="6dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/listviewback"
android:orientation="vertical"
android:padding="5dip">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/custom_listviewIV"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:src="#drawable/avatar2"
/>
<TextView
android:id="#+id/custom_listviewMainTv"
android:layout_width="180dip"
android:layout_height="45dip"
android:layout_marginLeft="2dip"
android:layout_toLeftOf="#+id/custom_listviewLinearLay"
android:layout_toRightOf="#+id/custom_listviewIV"
android:maxLines="2"
android:paddingTop="2dip"
android:text="Goverment "
android:textColor="#color/GreyDark"
android:textSize="15sp"
android:textStyle="bold"/>
<LinearLayout
android:id="#+id/custom_listviewLinearLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="2dip"
android:orientation="vertical">
<TextView
android:id="#+id/custom_listviewRating"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:layout_marginBottom="2dp"
android:background="#drawable/tv_round"
android:backgroundTint="#color/primary_dark"
android:gravity="center"
android:text="4.2/5"
android:textColor="#color/white"
android:textSize="13sp" />
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="5dip"
android:background="#color/GreyLight" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:orientation="vertical"
android:paddingLeft="10dip"
android:paddingTop="1dip"
android:paddingRight="10dip"
>
<TextView
android:id="#+id/custom_listviewFirstTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:text="Fsc Pre Engineering1"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewSecondTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fsc Pre Engineering2"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewThirdTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fsc Pre Engineering3"
android:textColor="#color/greyligher"
android:textSize="13sp"
android:visibility="gone" />
<TextView
android:id="#+id/custom_listviewFourthTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="more+"
android:textColor="#color/black"
android:textSize="13sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
here is my adapter code
public class CustomAdaptor extends BaseAdapter {
private ArrayList<Data> myDataList;
private Context context;
LinearLayout linearLayout;
public CustomAdaptor(ArrayList<Data> mylist, Context context) {
this.myDataList = mylist;
this.context = context;
}
public int getCount() {
return myDataList.size();
}
public Object getItem(int position) {
return myDataList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.custom_listview, null);
linearLayout= view.findViewById(R.id.layout);
TextView collegeName = view.findViewById(R.id.custom_listviewMainTv);
TextView rating = view.findViewById(R.id.custom_listviewRating);
ImageView country = view.findViewById(R.id.custom_listviewIV);
LinearLayout ll= view.findViewById(R.id.custom_listviewLinearLay);
TextView firstTv= view.findViewById(R.id.custom_listviewFirstTv);
TextView secondTv= view.findViewById(R.id.custom_listviewSecondTv);
TextView thirdTv= view.findViewById(R.id.custom_listviewThirdTv);
TextView fourthTv= view.findViewById(R.id.custom_listviewFourthTv);
collegeName.setText(myDataList.get(position).getName());
rating.setText("" + myDataList.get(position).getRating());
Double n=0.0;
if (n==myDataList.get(position).getRating()) {
ll.setVisibility(View.GONE);
}
if(!myDataList.get(position).getCourseList().isEmpty()) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName().equals("")) {
firstTv.setVisibility(View.VISIBLE);
firstTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(0).getCourseName());
}
if (myDataList.get(position).getCourseList().get(0).lst.size()>1) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName().equals("")) {
secondTv.setVisibility(View.VISIBLE);
secondTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(1).getCourseName());
}
}
if (myDataList.get(position).getCourseList().get(0).lst.size()>2) {
if (!myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName().equals("")) {
thirdTv.setVisibility(View.VISIBLE);
thirdTv.setText(myDataList.get(position).getCourseList().get(0).lst.get(2).getCourseName());
}
}
if (myDataList.get(position).getCourseList().size()>1) {
fourthTv.setVisibility(View.VISIBLE);
fourthTv.setText("+More");
}else if(myDataList.get(position).getCourseList().get(0).lst.size()>3) {
fourthTv.setVisibility(View.VISIBLE);
fourthTv.setText("+More");
}
String img = "" + myDataList.get(position).getImage();
img = img.replaceAll(" ", "%20");
Glide.with(context).load(img).into(country);
view.setTag(myDataList.get(position).getId());
}
else {
linearLayout.setVisibility(View.GONE);
}
return view;
}
}
Cardview is probably just the "breaking point" for how much graphic content that cna be used in an unoptimized way without lagging. As mentioned in some comments you should be using a ViewHolder, here is a simple example of using a viewholder:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.nameTextView.setText(person.getName());
holder.surnameTextView.setText(person.getSurname());
//holder.personImageView.setImageBitmap(person.getImage());
return convertView;
}
you can read more about it here
Additionally you should be using RecyclerView and not ListView, to optimize performance.
Try to minimize your view hierarchy and reduce complex operations from binding.

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

Get "No results" Message Custom Adapter ListView Android

I am having a problem to set a "No results" message when getCount() returns 0.
I have a custom layout for the listview, and I simply want to get a "No Results" textview when there is no data on the listview.
Any idea how to achieve this?
I saw something with a empty id, but that needs to be on a listview, since this is a custom layout, it doesn't have that "tag".
Custom Adapter Code:
public class ListScheduleAdapter extends BaseAdapter {
Context context;
protected List<Schedule> listSchedules;
LayoutInflater inflater;
public ListScheduleAdapter(Context context, List<Schedule> listSchedules) {
this.listSchedules = listSchedules;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return listSchedules.size();
}
public Schedule getItem(int position) {
return listSchedules.get(position);
}
public long getItemId(int position) {
return listSchedules.get(position).getCod_Horario();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.layout_list_item,
parent, false);
holder.txtTeacher = (TextView) convertView
.findViewById(R.id.txt_teacher);
holder.txtDayofWeek = (TextView) convertView
.findViewById(R.id.txt_dayofweek);
holder.txtSubject = (TextView) convertView
.findViewById(R.id.txt_subject);
holder.txtTime = (TextView) convertView
.findViewById(R.id.txt_time);
holder.txtRoom = (TextView) convertView
.findViewById(R.id.txt_room);
holder.txtClass = (TextView) convertView
.findViewById(R.id.txt_class);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Schedule sche = listSchedules.get(position);
String fullTeacher = sche.getProfessor();
String[] names = fullTeacher.split(" ");
holder.txtTeacher.setText(names[0] + " " + names[names.length-1]);
holder.txtDayofWeek.setText(sche.getDia_Semana());
holder.txtSubject.setText(sche.getDisciplina());
holder.txtTime.setText(sche.getT_Entrada() + "h-" + sche.getT_Saida()+"h");
holder.txtRoom.setText(sche.getSala());
holder.txtClass.setText(sche.getTurma());
return convertView;
}
private class ViewHolder {
TextView txtTeacher;
TextView txtDayofWeek;
TextView txtSubject;
TextView txtTime;
TextView txtRoom;
TextView txtClass;
}
}
Fragment Code (method called on a async OnPostExecute method):
private void populateListView() {
ListScheduleAdapter adapter = new ListScheduleAdapter(getActivity(), ScheduleList);
listviewHorarios.setAdapter(adapter);
}
Layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<LinearLayout
android:id="#+id/layout_row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_teacher"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_dayofweek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row1"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_subject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row2"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_room"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_class"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
</RelativeLayout>
add
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
to the same layout where you declared your ListView (wrap both in a FrameLayout)
on the ListView's instance call
listView.setEmptyView(findViewById(R.id.empty));
when the adapter is empty, you will see the TextView apper
add TextView to your listview layout and setVisibility of it to GONE and check the adapter:
if(adapter==null){
listView.setVisiblity(View.GONE);
tvEmpty.setVisiblity(View.VISIBLE);
}
hope this will help.

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

ListView Rowlayout in a own coded Chat

What I'm trying to do
I'm trying to create a chat which has to diffrent row's. For every row I made a own layout file, but the problem is that the layoutfile of one row dosn't fit the screen.
Question
What do I need to change in the row layout that it fits like it should. You'll find the code and also a printscreen of what I'm trying.
Code
ListAdapter
public class ChatListAdapter extends BaseAdapter {
private ArrayList<String> ContentList;
private ArrayList<Integer> ChatUserList;
private static LayoutInflater inflater = null;
public ChatListAdapter(Activity activity, ArrayList<String> _ContentList,
ArrayList<Integer> _ChatUserList) {
ContentList = _ContentList;
ChatUserList = _ChatUserList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return ContentList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
TextView tv_text;
TextView tv_date;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
if (ChatUserList.get(position) == 1) {
// I'm RECIVER -> Message is left align
vi = inflater.inflate(R.layout.rowlayout_leftalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_leftalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_leftalign_date);
} else {
// I'm SENDER -> Message is right align
vi = inflater.inflate(R.layout.rowlayout_rightalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_rightalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_rightalign_date);
}
tv_date.setText(sdf.format(new Date()));
tv_text.setText(ContentList.get(position));
tv_text.setMaxWidth(((Chat.display.getWidth() / 4) * 3));
return vi;
}
RowLayout XML Left (Blue bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_rightalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_rightalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_rightalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toLeftOf="#id/tv_chat_rightalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
RowLayout XML right (red bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_leftalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_leftalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_leftalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toRightOf="#id/tv_chat_leftalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
for both XML add android:layout_width="fill_parent" and android:layout_height="wrap_content" like followings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
and so on...

Categories

Resources