I have a listview which contains custom rows. When I show the listview with it's custom rows, all the rows only have a height of about 2px. I tried it on an Android 4.0 device, and on an emulator which has Android 2.x. I added a screenshot of the problem.
Prepare, here comes quite a lot of code:
This is my code for my custom row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="105px"
android:background="#fff"
android:orientation="vertical"
android:paddingLeft="15dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8px"
android:gravity="top"
android:text="name"
android:textColor="#322a37"
android:textSize="11pt" />
</RelativeLayout>
The code in my activity:
public class ListActivity extends BaseActivity {
private ArrayList<User> m_items;
private ObjectAdapter m_adapter;
private EditText textSearch;
private ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
textSearch = (EditText) findViewById(R.id.textSearch);
listView = (ListView) findViewById(R.id.listView);
m_items = new ArrayList<User>();
//(fill m_items, goes well)
this.m_adapter = new ObjectAdapter(this, R.layout.row_text, m_items);
listView.setAdapter(this.m_adapter);
}
private class ObjectAdapter extends ArrayAdapter<User> {
private ArrayList<User> items;
#SuppressWarnings("unchecked")
public ObjectAdapter(Context context, int textViewResourceId,
ArrayList<User> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_text, null);
}
User u = items.get(position);
if (u != null) {
TextView top = (TextView) v.findViewById(R.id.toptext);
if (top != null) {
top.setText(u.getName());
}
}
return v;
}
}
}
Anyone any clue why this is happening?
The line:
android:layout_height="0dip"
does not strike me as being particularly clever.
Edit your custom row xml and correct the hight of the TextView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="105px"
android:background="#fff"
android:orientation="vertical"
android:paddingLeft="15dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8px"
android:gravity="top"
android:text="name"
android:textColor="#322a37"
android:textSize="11pt" />
Related
I have a listView with some items now I want to change the background color or item text color by click on a separate button which IS NOT on the list . How can I do that in my android app?
This is my main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayAdapter = new SubArrayAdapter(this, dataItemList);
ListView lv = findViewById(R.id.mylist);
lv.setAdapter(arrayAdapter);
ImageButton btn_check = findViewById(R.id.button_checkout);
btn_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// how can I access the list items within this click-handler method
});
List Item Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listitems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="#+id/yes_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingRight="5dp"
android:visibility="invisible"
/>
<ImageView
android:id="#+id/no_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingRight="5dp"
android:visibility="invisible"
/>
<TextView
android:id="#+id/paragraph_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/yes_image"
android:layout_toRightOf="#id/yes_image"
android:textColor="#color/paragraph_items_colro"
android:textSize="16sp"
android:textStyle="normal" />
</RelativeLayout>
Activity_main xml layout file:
android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textDirection="ltr"
tools:context=".MainActivity">
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#android:color/transparent"
android:divider="#color/colorAccent"
android:dividerHeight="1dp"
android:listSelector="#0f0"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
<LinearLayout
android:id="#+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/mylist">
<ImageButton
android:id="#+id/button_checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/checkout_9"
android:src="#drawable/checkout_9"
app:layout_constraintTop_toBottomOf="#id/mylist"
tools:layout_editor_absoluteX="106dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
And I have myown adapter class that in the getview() method I populated the list items
this inner class is located in mainactivity:
private class SubArrayAdapter extends ArrayAdapter<DataItem> {
private List<DataItem> items;
private Context context;
public SubArrayAdapter(#NonNull Context context, #NonNull List<DataItem>
items) {
super(context, R.layout.listitems, items);
this.items = items;
this.context = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull
ViewGroup parent) {
DataItem dataitem1 = items.get(position);
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listitems, null);
TextView tv = (TextView)
view.findViewById(R.id.paragraph_description);
tv.setText(dataitem1.getDescription());
tv.setTag(position + 100);
ImageView yes_iv = view.findViewById(R.id.yes_image);
int yes_res = context.getResources().getIdentifier("yes",
"drawable", getPackageName());
yes_iv.setImageResource(yes_res);
yes_iv.setTag(position + 200);
//yes_iv.setVisibility(View.VISIBLE);
ImageView no_iv = view.findViewById(R.id.no_image);
int no_res = context.getResources().getIdentifier("no", "drawable",
getPackageName());
no_iv.setImageResource(no_res);
no_iv.setTag(position + 300);
return view;
}
}
}
I've noticed You have set android:listSelector for ListView in Your activity_main.xml - which was giving You an ability to change item's background on single click.
So I decided to check out how it works in ListView class.
I've looked at source code of ListView and searched for phrase selector. There was the line:
} else if (mAction == STATE_REQUEST_FOCUS) {
final int childIndex = mPosition - mFirstPosition;
final View child = getChildAt(childIndex);
if (child != null) {
child.requestFocus();
}
mAction = -1;
}
final View child = getChildAt(childIndex);
Following these steps - I knew solution for Your question problem. Here it is:
ImageButton imageButton = findViewById(R.id.button_checkout);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int hardCodedPoositionToSelect = 1;
View childAt = listView.getChildAt(hardCodedPoositionToSelect);
if (childAt != null) {
//change background
childAt.setBackgroundColor(Color.RED);
//change textView color of current view
TextView paragraphDescription = childAt.findViewById(R.id.paragraph_description);
paragraphDescription.setTextColor(Color.MAGENTA);
}
}
});
You can see I am calling getChildAt() on listView to obtain current item's view basing on item's position from hardCodedPoositionToSelect.
I am new in android. I want to show the data that I get from Database into ListView.
For now I can show only a single data.
How to show multiple data into custom ListView?
Here's my MainActivity.class
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
Log.d("Reading: ", "Reading all contacts..");
List <AllItem> allItems = new ArrayList<AllItem>();
allItems = db.getAllAccommodations();
ArrayList <String> allItems2 = new ArrayList<String>();
for (AllItem cn : allItems) {
allItems2.add(cn.getItem_name());
allItems2.add(cn.getAreaNAme());
}
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,allItems2);
listview.setAdapter(adapter);
I have my own custom ListView like this
Acco.xml
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
AccoLayout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Is there anyone can help me with this?
You need to create CustomAdapter class for this.
Use your listview as it is.
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
And your custom Listview also.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Create your custom adapter class and pass data to this class.
adapter = new ListAdapter(this, allItems2);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Here is CustomAdapter class.
public class ListAdapter extends BaseAdapter {
public ListAdapter(Activity a, List <AllItem> allItems) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.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;
if (convertView == null)
vi = inflater.inflate(R.layout.AccoLayout, null);
TextView itemName = (TextView)vi.findviewById(R.id.item_name);
TextView areaName = (TextView)vi.findviewById(R.id.area_name);
// Set your data here
itenName.setText(data.get(position));//like this
return vi;
}
}
I assume that your allItems2 list has a pattern like itemName0, areaName0, itemName1, areaName1, itemName2.. etc. Then, you can write a custom adapter like this.
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final List<String> items;
public CustomAdapter (Activity context, List<String> items) {
super(context, R.layout.AccoLayout, items);
this.context = context;
this.items= items;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.AccoLayout, null, true);
TextView itemName = (TextView) rowView.findViewById(R.id.item_name);
TextView areaName= (TextView) rowView.findViewById(R.id.area_name);
itemName.setText(items.get(2*position));
areaName.setText(items.get(2*position + 1));
return rowView;
}
}
Then call it from your main activity;
CustopAdapter adapter = new CustomAdapter (MainActivity.this, allItems2);
listview.setAdapter(adapter);
Also, by Android's convention, try not to use uppercase letters in XML file names to prevent any complication. You may change to acco_layout.xml.
Consider this to be your Cursor: after you fetch from DB
Cursor mCur = db.getAllAccommodations();
in onCreate:
CurAdapter Cur = new CurAdapter(getActivity(), mCur,0);
final ListView lv = (ListView)findViewById(R.id.listAccommodation);
lv.setFastScrollEnabled(true);
lv.setAdapter(Cur);
Then Make a Subclass:
private class CurAdapter extends CursorAdapter{
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.item_name);
TextView tv1 = (TextView) view.findViewById(R.id.area_name);
String item = (cursor.getString(cursor.getColumnIndexOrThrow("ColumnName1")));
String area = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("ColumnName2")));
tv1.setText(item);
tv.setText(area);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.AccoLayout, null);
return view;
}
}
Where R.layout.AccoLayout is your "listview row" layout, for example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_name"
android:textSize="14sp" />
</RelativeLayout>
i've a problem, i want an EditText above my ListView that performs a search in that ListView. Is it possible to do?
This is the Activity code:
public static class BirreChiareListView extends Main implements AdapterView.OnItemClickListener {
ListView list;
String [] birrechiare;
int [] images={R.drawable.ichnusa, R.drawable.ichnusa_speciale, R.drawable.ichnusa_cruda};
private MyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(birrechiare_listview);
Resources res=getResources();
birrechiare=res.getStringArray(R.array.birre_chiare);
list = (ListView)findViewById(R.id.listViewBirreChiare);
MyAdapter adapter = new MyAdapter(this,birrechiare, images);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long l) {
String item = adapter.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "Hai selezionato: " + item, Toast.LENGTH_SHORT).show();
if (position == 0){
Intent gotoclassica = new Intent(BirreChiareListView.this,Ichnusa_Classica.class);
startActivity(gotoclassica);
}
if (position == 1){
Intent gotospeciale = new Intent(BirreChiareListView.this,Ichnusa_Speciale.class);
startActivity(gotospeciale);
}
if (position == 2){
Intent gotocruda = new Intent(BirreChiareListView.this,Ichnusa_Cruda.class);
startActivity(gotocruda);
}
}
}
static class MyAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
MyAdapter(Context c, String[] titles, int imgs []){
super (c, single_row, R.id.Titletxt, titles);
this.context = c;
this.images=imgs;
this.titleArray=titles;
}
#Override
public View getView (int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(single_row, parent,false);
ImageView myimage = (ImageView) row.findViewById(R.id.imageView);
TextView mytitle = (TextView) row.findViewById(R.id.Titletxt);
myimage.setImageResource(images[position]);
mytitle.setText(titleArray[position]);
return row;
}
}
Ok, this is the XML file of the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background_listview">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Birre Chiare"
android:singleLine="false"
android:textColor="#FFFFFF"
android:gravity="center"/>
<EditText
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<ListView
android:id="#+id/listViewBirreChiare"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
At the end, this is the single_row.xml file:
<?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="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/ichnusa"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/Titletxt"
android:layout_alignBottom="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:layout_marginBottom="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Yes this is definitely possible to do. Android provides a Filterable interface for ArrayAdapters. The interface provides a getFilter() method where you can hook into the filtering process.
When the text changes in your EditText, you call adapter.getFilter().filter("filterString"). The text changing behaviour for the EditText is done by using a TextWatcher on the EditText. This following tutorial, explains it in good detail:
http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html
EDIT:
To implement the filterable interface, just do this in your ArrayAdapter:
static class MyAdapter extends ArrayAdapter<String> implements Filterable {
private Filter myFilter;
#Override
public Filter getFilter() {
if (myFilter == null)
myFilter = new MyFilter();
return myFilter;
}
}
where MyFilter is your own Filter class.
My problem is that my listView repeat all items of my layout.
I need to have the edit text and the button at the bottom of this listView.
But here the edit text and button is repeated for each row of listView and i don't know why.
If someone could help me
The activity :
public class MessActivity extends ListActivity
{
public Channel chan;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(Network.getInstance().Messages);
Bundle b = getIntent().getExtras();
this.chan = (Channel) b.get("chanSelected");
//add all message of selected chan to the messAdapter
for (int i = 0 ; i < this.chan.getListMessage().size() ; i++) {
Message mess = this.chan.getListMessage().get(i);
Network.getInstance().addMessage(mess);
}
setContentView(R.layout.mess_list);
}
}
The adapter :
public class MessageAdapter extends ArrayAdapter<Message>
{
private Context context;
private int textViewResourceId;
public MessageAdapter(Context context, int textViewResourceId)
{
super(context, textViewResourceId);
this.context = context;
this.textViewResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
MessHolder holder = null;
//row null
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder = new MessHolder();
holder.texte = (TextView)row.findViewById(R.id.listMess);
row.setTag(holder);
}
else
{
holder = (MessHolder)row.getTag();
}
Message mess = getItem(position);
holder.texte.setText(mess.getText());
return row;
}
static class MessHolder
{
TextView texte;
}
}
The layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list">
</ListView>
<TextView
android:id="#+id/listMess"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
//edittext
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Envoyer" />
</LinearLayout>
</LinearLayout>
Thank you for your help.
Use ListView method addFooter(View v).
You can define xml layout for this view and inflate it:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
Firstly Reason of this issue is Listview Reuses its view when scrolls , So we should have a logic in our get view to manage that
if(convertview == null)
{
//code here
}else {
//code here
}
, but if you dont want to reuse then you can use following :
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}
I have two list, that need to be processed and displayed, I have used the following technique -
I get the data from 2 sources, process it and have 2 Custom Adapters, that take the data and place it in the relvant View.
However I only can view one list (the one at the top of in the view xml) - and not the other one,
Can someone please help me fix this prob. Its driving me insane.
Main Activity code belwo:
public class TestList extends Activity {
private final static String TAG = "ListSample";
private List<String> items;
private List<String> items2;
private CustomTeamListAdapter m_adapter;
private CustomTeamListAdapterTwo t_adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
items.add(i + "|||>>");
}
ListView list = (ListView) findViewById(R.id.customlist);
this.m_adapter = new CustomTeamListAdapter(this, R.layout.listitem,
(ArrayList<String>) items);
list.setAdapter(m_adapter);
/******************************************************************************/
items2 = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
items2.add(i + ">>----");
}
ListView list_two = (ListView) findViewById(R.id.customlisttwo);
this.t_adapter = new CustomTeamListAdapterTwo(this, R.layout.listitem,
(ArrayList<String>) items2);
list_two.setAdapter(t_adapter);
}
private class CustomTeamListAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
public CustomTeamListAdapter(Context context, int textViewResourceId,
ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
String o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.sampletext);
tt.setText(o + "FirstSet");
tt.setId(position);
}
return v;
}
}
private class CustomTeamListAdapterTwo extends ArrayAdapter<String> {
private ArrayList<String> items;
public CustomTeamListAdapterTwo(Context context, int textViewResourceId,
ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
String o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.sampletext);
tt.setText(o + "SecondSet");
tt.setId(position);
}
return v;
}
}
}
Main layout XML below
<?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/customlisttwo"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/randomtext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#CCCCCC"
android:text="Random Text" />
<ListView android:id="#+id/customlist" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
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="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/customlisttwo"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/randomtext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#CCCCCC"
android:text="Random Text" />
<ListView android:id="#+id/customlist" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Thanks in advance for your thoughts
Try using Merge Adapter by Mark Murphy https://github.com/commonsguy/cwac-merge. You cant have two list views in the same activity. You can add that text view between the two layouts.