I am displaying a listview using an arrayadapter. for each listitem i inflate the same xml file.i am able to display the listview fine.but now i have another requirement. i want to add some more view programatically to the same xml file that i am inflating for each list item. but when i try to do that i am not getting any error. but somehow the view i added programatically is not showing up...can anyone please help me. below is the code for the xml layout and for the arrayadapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/listitemcolor"
android:padding="10dip">
<LinearLayout
android:layout_weight="1.0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:textColor="#android:color/black"
android:id="#+id/locationitem_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textSize="16dp"
android:textStyle="bold" />
<LinearLayout
android:layout_marginTop="5dip"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:drawableLeft="#drawable/like"
android:textColor="#008000"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:textColor="#008000"
android:layout_marginLeft="5dip"
android:id="#+id/number_of_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:drawableLeft="#drawable/dislike"
android:textColor="#FF0000"
android:layout_marginLeft="10dip"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dislikes"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:textColor="#FF0000"
android:layout_marginLeft="5dip"
android:id="#+id/number_of_dislikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
This is the code for arrayadapter:
public class LocationItemAdapter extends ArrayAdapter<LocationItem> {
private Context context;
private ArrayList<LocationItem> items;
public LocationItemAdapter(Context context,
List<LocationItem> objects) {
super(context, R.layout.locationitem_listview_element, objects);
this.context = context;
items = (ArrayList<LocationItem>) objects;
}
/* (non-Javadoc)
* #see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowview = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowview = inflater.inflate(R.layout.locationitem_listview_element, null);
}
else
{
rowview = convertView;
}
((TextView)rowview.findViewById(R.id.locationitem_name)).setText(items.get(position).getName());
((TextView)rowview.findViewById(R.id.number_of_likes)).setText(String.valueOf(items.get(position).getLikes()));
((TextView)rowview.findViewById(R.id.number_of_dislikes)).setText(String.valueOf(items.get(position).getDislikes()));
LayoutInflater inflater1 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView tv = new TextView(context);
//TextView tv = (TextView)inflater1.inflate(R.layout.sample, null);
tv.setText("My name is blah");
tv.setTextColor(R.color.black);
tv.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
//tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LinearLayout ll = (LinearLayout)rowview.findViewById(R.id.placeholder);
//((LinearLayout)rowview.findViewById(R.id.placeholder)).addView(tv);
ll.addView(tv);
rowview.setTag(items.get(position));
return rowview;
}
}
That looks correct to me. Perhaps the embed is successful but ll takes up no space on the screen? Can you verify that placeholder takes up some amount of visible space on the screen by setting it's background color to hot pink or something?
Related
I have a Listview with a custom adapter , which displays person's data like
Barak Obama 2008
But I want to put a header with Name Surname Date but properly alligned
A draft example:
ListView Items with titles
How Can I achieve this?
thank you
Custom ListView, All what you need, A xml layout for row with adapter and listview
check out this link explain Custom listview
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
You can use TableLayout to show your Data as your requirement. I've replicated your demand. Here are the codes.
1) MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lstview=(ListView)findViewById(R.id.listView);
// Inflate header view
ViewGroup headerView = (ViewGroup)getLayoutInflater().inflate(R.layout.header_layout, lstview,false);
// Add header view to the ListView
lstview.addHeaderView(headerView);
// Get the string array defined in strings.xml file
String[] date = {"29-03-17", "29-03-17"};
String[] amount = {"5", "2"};
String[] dept = {"14", "4"};
// Create an adapter to bind data to the ListView
CustomListViewAdapter adapter=new CustomListViewAdapter(this,R.layout.row_listview, R.id.tvDate, date, amount, dept);
// Bind data to the ListView
lstview.setAdapter(adapter);
}
2) CustomListViewAdapter.java
public class CustomListViewAdapter extends ArrayAdapter<String> {
int groupid;
String[] date;
String[] amount;
String[] dept;
ArrayList<String> desc;
Context context;
public CustomListViewAdapter(Context context, int vg, int id, String[] date, String[] amount, String[] dept){
super(context,vg, id, date);
this.context=context;
groupid=vg;
this.date=date;
this.amount=amount;
this.dept=dept;
}
// Hold views of the ListView to improve its scrolling performance
static class ViewHolder {
public TextView tvDate;
public TextView tvAmount;
public TextView tvDept;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// Inflate the rowlayout.xml file if convertView is null
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView= inflater.inflate(groupid, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvDate= (TextView) rowView.findViewById(R.id.tvDate);
viewHolder.tvAmount= (TextView) rowView.findViewById(R.id.tvAmount);
viewHolder.tvDept= (TextView) rowView.findViewById(R.id.tvDept);
rowView.setTag(viewHolder);
}
// Set text to each TextView of ListView item
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.tvDate.setText(date[position]);
holder.tvAmount.setText(amount[position]);
holder.tvDept.setText(dept[position]);
return rowView;
}
}
3) HeaderLayout (header_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvDate"
android:layout_column="0"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Date"
android:layout_gravity="center">
</TextView>
<TextView
android:id="#+id/tvAmount"
android:layout_column="1"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Amount"
android:layout_gravity="center">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDept"
android:layout_column="2"
android:layout_span="1"
android:text="Dept"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDelete"
android:layout_column="3"
android:layout_span="1"
android:text="Delete"
android:layout_gravity="center" />
</TableRow>
</TableLayout>
4) RowLayout for each ListView row (row_listview.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDate"
android:layout_column="0"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Date"
android:layout_gravity="center">
</TextView>
<TextView
android:id="#+id/tvAmount"
android:layout_column="1"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Amount"
android:layout_gravity="center">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDept"
android:layout_column="2"
android:layout_span="1"
android:text="Dept"
android:layout_gravity="center" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="3"
android:layout_span="1"
android:src="#android:drawable/ic_delete" />
</TableRow>
</TableLayout>
5) Output:
Hope this helps.
I'm building an app for a news website, I have created a ListView and a layout for each item, which has quite a bit of white space, but on the list view all the white space is removed.
Here's what I mean: http://imgur.com/a/mLuuE
This is an item layout:
<?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="175dp"
android:gravity="center"
android:layout_weight="0"
android:id="#+id/article_layout"
android:background="#drawable/gradientdemo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
</LinearLayout>
This is my custom adapter:
public class ArticleAdapter extends ArrayAdapter<Article> {
public ArticleAdapter(Context context, int textViewResourceId){
super(context, textViewResourceId);
}
public ArticleAdapter(Context context, int resource, List<Article> items ){
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.article_item, null);
}
Article article = (Article) getItem(position);
TextView title = (TextView) v.findViewById(R.id.article_title);
TextView info = (TextView) v.findViewById(R.id.article_info);
if( title != null ) {
title.setText( article.getTitle() );
}
if( info != null ) {
info.setText( article.getAuthor() );
}
return v;
}
}
Hope it's readable, I'm kind of new to android development.
Your cell's LineaerLayout:
android:layout_height="0dp"
android:layout_weight="1"
change to:
android:layout_height="wrap_content"
you don't need layout_weight
Delete the android:layout_weight="1" and add a correct height, e.g. android:layout_height="60dp":
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
in the linearLayout use wrapcontent, erase layout_weigth and add weigthsum=1
Then in textViews change height to 0dp and add to each one layout_weight=0.5
I think this will work
I fixed this by changing this line in the adapter
v = vi.inflate(R.layout.article_item, null);
to this one:
v = vi.inflate(R.layout.article_item, parent, false);
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.
adapterListView = new SpecialAdapter(getBaseContext(),list,R.layout.listview_layout,from,to);
headerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.jpos_ror_header, null, false);
footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.jpos_ror_footer, null, false);
TextView tin=(TextView)headerView.findViewById(R.id.textViewTinValue);
TextView textViewNameOfPayor=(TextView)headerView.findViewById(R.id.textViewNameOfPayor);
TextView textViewLocation=(TextView)headerView.findViewById(R.id.textViewLocation);
Here is my code in getting object id's from my header and footer. how can i get the view of my list view data adapter without onClick event. i want to do some layout manipulation in viewing of my layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- here is my data adapter to be display per item in list view -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativeModeOfPaymentCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<LinearLayout
android:id="#+id/linearForCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:paddingTop="5dp" >
<TextView
android:id="#+id/textViewModeOfPaymentCheckValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mode of Payment Value check"
android:layout_marginLeft="15dp"
android:textStyle="bold"
android:textColor="#ffffff" />
</LinearLayout>
<!-- asdasda -->
<LinearLayout
android:id="#+id/linearForAmountModeOfPayment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearCheckDate"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
android:id="#+id/relativeBlock100"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#000000" >
<TextView
android:id="#+id/textViewModAmountCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AMOUNT"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeBlock101"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000" >
<TextView
android:id="#+id/textViewModeOfPayCheckValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
<!-- name of check issued -->
<LinearLayout
android:id="#+id/linearForNameCheckIssued"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearForCheck"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="2" >
<RelativeLayout
android:id="#+id/relativeBlock101"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#000000" >
<TextView
android:id="#+id/textViewBankNameCheckIssued"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RIZAL COMMERCIAL BANKING CORP."
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
<!-- CHECK NUMBER -->
<LinearLayout
android:id="#+id/linearCheckNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearForNameCheckIssued"
android:visibility="gone"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewCheckNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="CHECK # : "
android:textColor="#ffffff" />
<TextView
android:id="#+id/textViewCheckNumValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</LinearLayout>
<!-- CHECK DATE -->
<LinearLayout
android:id="#+id/linearCheckDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
<!-- android:visibility="gone" -->
android:layout_below="#+id/linearCheckNumber"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewCheckDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="CHECK DATE : "
android:textColor="#ffffff" />
<TextView
android:id="#+id/textViewCheckDateValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLineForCheck"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearForAmountModeOfPayment"
android:orientation="horizontal" >
<View
android:id="#+id/View201"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
in this adapter i want to manipulate the view of linearlayouts by having LinearLAyout.setVisibility(LinearLayout.GONE or VISIBLE);
Here i think i have some lead from my problem from the answer of himanshu. here is my listview special adapter im having problem how can get the view and viewGroup implementation to my special adapter here is my SpecialAdapter..
public class SpecialAdapter extends SimpleAdapter {
public static TextView tv;
private int[] colors = new int[] { 0xffcccccc , 0xffffffff };
private int[] colors2 = new int[] { 0xffffffff , 0xff000000 };
// Context context;
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
//TextView textView = (TextView)view.findViewById(R.id.textViewModAmountCheck);
//textView.setText("AlvinTest");
return view;
}
Could anyoune tell me i am doing right when im implementing mg getView2.
public View getView2(Context context, int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);//get your layout inflator;
//LayoutInflater inf = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_layout, null);
}
LinearLayout yyy = (LinearLayout) convertView.findViewById(R.id.linearCheckDate);
yyy.setVisibility(View.GONE);
return convertView;
}
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
int getIndex = cursor.getColumnIndex("Name");
String empname = cursor.getString(getIndex);
tv = (TextView) view;
tv.setTextColor(Color.WHITE);
tv.setText(empname);
if(empname.equals("Any String"))
{
tv.setTextColor(Color.rgb(58, 58, 224));
return true;
}
return false;
}
}
Here's my code when im calling the getView2 from my listAdapter
LayoutInflater inf = LayoutInflater.from(this);
View v = inf.inflate(R.layout.listview_layout, null);
//add magic here bu i don't know how can i get the view of these View and ViewGroup
View itemConvertView = inf.inflate(R.layout.listview_layout, null);
ViewGroup listParent =null; //this.getParent();//(ViewGroup)SelectorView.this.getParent();// null;
adapterListView.getView2(this,0, itemConvertView, listParent);
try this:
Hide your Layout:
YourLayout.setVisibility(View.GONE);
Visible your Layout :
YourLayout.setVisibility(View.VISIBLE);
If i understood your problem correctly, you want to get the first element of the ListView pro grammatically and change its state (Visibility).
You can get the element in ListView by calling getChildAt(int index) method
e.g
LinearLayout yourFirstView = (LinearLayout) listview.getChildAt(0);
Accessing child of the element that reside in first row of the listview.
ViewType view = (ViewType)yourFirstView.findViewById(R.id.elementId);
setting the visiblity
yourFirstView.setVisiblity(View.Gone); // or View.VISIBLE
What I understand from your question is that there is some view in your ListView item which you want to show only if needed. You can do this in the getView method of your adapter where you inflate the custom layout of list item. Something like below
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = get your layout inflator;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.XXXX, null);
}
LinearLayout yyy = (LinearLayout) convertView.findViewById(R.id.member_name_text_view);
yyy.setVisibility(View.Gone)
return convertView;
}
I have a query results from a database and corresponding variables are filled.
I think that the problem is the layout, I am using incorrect layouts in my XML file. Maybe it is the ArrayAdapter, I don't know. I've spent a lot of hours trying to solve this problem and haven't been able to. Thanks for your help!
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:id="#+id/precio_offers_charger"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="20px"
android:textColor="#FFFFFF"
android:background="#FF0000" />
<TextView android:id="#+id/cabecera_offers_charger"
android:paddingLeft="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18px"
android:textColor="#FFFFFF"
android:background="#010201" />
</TableRow>
<TableRow>
<ImageView android:id="#+id/imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/descripcion_offers_charger"
android:paddingLeft="10px"
android:paddingTop="10px"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textStyle="normal"
android:textSize="14px" />
</TableRow>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</TableLayout>
This is a part of my class:
public class OffersChargerActivity extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offers_charger);
...
public class AdaptadorTitulares extends ArrayAdapter<Oferta> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.offers_charger, ofertas);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = linflater.inflate(R.layout.offers_charger, null);
}
// poblamos la lista de elementos
TextView precio = (TextView)view.findViewById(R.id.precio_offers_charger);
precio.setText(ofertas.get(position).getPrecio().concat("€"));
ImageView imagen = (ImageView) view.findViewById(R.id.imagen);
imagen.setImageBitmap(new GetImages().downloadFile(ofertas.get(position).getImagen()));
TextView cabecera = (TextView)view.findViewById(R.id.cabecera_offers_charger);
cabecera.setText(ofertas.get(position).getCabecera());
TextView descripcion = (TextView)view.findViewById(R.id.descripcion_offers_charger);
descripcion.setText(ofertas.get(position).getDescripcion());
return view;
}
}
...
}
This worked for me:
<TextView
android:id="#+id/nameText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#android:color/black" />
Try adding in 'layout_weight' and change your 'layout_width' to "fill_parent".
Hope this helps!
don't wrap content, use fill_parent for width, and also set android:ellipsize="true"