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.
Related
I am new in android app dev and I am trying to create a ListFragment consisting of a TextView and a ListView. I want to try and populate the ListView with data from a custom class and thus created a custom ArrayAdapter. My app runs but doesn't show anything in the list and I can't figure out why not. I have logged the data and it is present in the holder and the text seems to be set but something is wrong.
row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/TextViewStation"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:id="#+id/TextViewType"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
</RelativeLayout>
list_layout.xml
<?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" >
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items to display." />
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
SimpleArrayAdapter.java
public class SimpleArrayAdapter extends ArrayAdapter<Warning> {
private final Context context;
private final List<Warning> objects;
public SimpleArrayAdapter(Context context, int resource,
List<Warning> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
holder.v1 = (TextView) row.findViewById(R.id.TextViewStation);
holder.v2 = (TextView) row.findViewById(R.id.TextViewType);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Warning warning = objects.get(position);
if (warning != null) {
holder.v1.setText(warning.getStation());
holder.v2.setText(warning.getType());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
public static class ViewHolder {
TextView v1; // view1
TextView v2; // view2
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Warning getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
}
List_View.java - the ListFragment
public class List_View extends ListFragment {
List<Warning> items = new ArrayList<Warning>();
Gson gson = new Gson();
private SimpleArrayAdapter adapter;
private ListView mListView;
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ---- Get data!
JsonElement je = new JsonParser().parse(result);
JsonObject jo = je.getAsJsonObject();
JsonArray ja = jo.getAsJsonArray("warnings");
for (int i = 0; i < ja.size(); i++) {
Warning war = new Warning();
war = gson.fromJson(ja.get(i).getAsJsonObject(), Warning.class);
items.add(war);
}
// ----
}
// #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater
.inflate(R.layout.list_layout, container, false);
// RESTManager manager = new RESTManager();
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("Adapter", "items.size(): "+ items.size());
adapter = new SimpleArrayAdapter(getActivity(), R.layout.row_layout,
items);
setListAdapter(adapter);
}
}
In SimpleArrayAdapter, me logging the result from holder.v1.getText() returns correct value but still nothing is showing in the ListView in the app.
Try this :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TextView station, type ;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
station = (TextView) row.findViewById(R.id.TextViewStation);
type = (TextView) row.findViewById(R.id.TextViewType);
}
Warning warning = objects.get(position);
if (warning != null) {
station.setText(warning.getStation());
type.setText(warning.getType());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
I found the problem. Stupidly for some reason I had android:layout_width="0dip" for the textviews........ changed to android:layout_width="fill_parent" and boom, there is my data!
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 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" />
I'm looking for a clear way to establish the XML resource for a list of strings with an image attached to it.
Should I use attributes? What is the best practice?
list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView android:id="#+id/row_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
In onCreate()
setContentView(R.layout.list_layout);
ListView lv = (ListView) findViewById(R.id.list);
List<ListRow> list = new ArrayList<ListRow>();
for ( loop through your data ) {
list.add(new ListRow("text", R.drawable.image));
}
YourAdapter adapter = new YourAdapter(this, list);
lv.setAdapter(adapter);
The classes
class ListRow {
private String text;
private int resource;
public ListRow(String text, int resource) {
super();
this.text = text;
this.resource = resource;
}
public int getText() {
return text;
}
public int getResource() {
return resource;
}
}
class YourAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<ListRow> theList;
public YourAdapter (Context context, List<ListRow> theList) {
this.context = context;
this.theList = theList;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
ListRow row = theList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.row_text);
tv.setText(row.getText());
ImageView iv = (ImageView) convertView.findViewById(R.id.row_image);
iv.setBackgroundResource(row.getResource());
return convertView;
}
#Override
public void onClick(DialogInterface dialog, int which) {
}
}
I am adding items to my data source and would like it to appear in my ListView. For some reason nothing is appearing:
Adapter:
private class STCompanyAdapter extends ArrayAdapter<STCompany> {
private ArrayList<STCompany> items;
public STCompanyAdapter(Context context, int textViewResourceId,
ArrayList<STCompany> 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.addstocksrow, null);
}
STCompany q = items.get(position);
if (q != null) {
TextView symbolText = (TextView) v.findViewById(R.id.toptext);
TextView nameText = (TextView) v.findViewById(R.id.bottomtext);
if (nameText != null) {
nameText.setText(q.getSymbol());
}
if (symbolText != null) {
symbolText.setText(q.getName());
}
}
return v;
}
}
Adding items in onCreate:
listView = (ListView) findViewById(R.id.symbolsListView);
this.companiesAdapter = new STCompanyAdapter(this, R.layout.addstocksrow, companies);
listView.setAdapter(this.companiesAdapter);
STCompany myCompany = new STCompany();
myCompany.setName("La La");
myCompany.setSymbol("BP");
companiesAdapter.add(myCompany);
companiesAdapter.notifyDataSetChanged();
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/LinearLayout">
<EditText android:id="#+id/addStocksEditText" android:text="Search company or stock" android:layout_width="200dp" android:layout_height="50dp"></EditText><Button android:layout_height="wrap_content" android:text="Search" android:id="#+id/searchButton" android:layout_width="fill_parent"></Button><ListView android:id="#+id/symbolsListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
try companies.add(myCompany) instead of companiesAdapter.add(myCompany).
Problem was my layout. listview wasn't appearing. Code is ok.