As title says, my listview won't get filled.
I know that the adapter is getting filled with 3 items, debugged my way through it. But my xml isnt showing anything at all:
The adapter is a global variable if that should do anything.
Code:
private void listChange(CustomCursor c){
List<SimpleListItem> itemsList = new ArrayList<SimpleListItem>();
while (c.moveToNext()) {
String picture = c.getString("picture");
String text = c.getString("name");
itemsList.add(new SimpleListItem(text, picture));
}
productListAdapter = new ProductAdapter(getBaseContext(),R.layout.product_item, itemsList);
setListAdapter(productListAdapter);
productListAdapter.notifyDataSetChanged();
}
XML of the activity:
<?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" >
<dk.foo.views.PageHeaderView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</dk.foo.views.PageHeaderView>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Modify the orientation of your LinearLayout to vertical if you want to see the ListView.
Right now with the orientation of your parent LinearLayout set to horizontal and the width of your custom view PageHeaderView set to FILL_PARENT, your ListView is pushed out of the screen.
Related
Right so, I have a navbar layout. The <FrameLayout> in content_main is replaced with stand1.xml when the stand1 button in my navbar is pressed. This works without any issue.
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior" tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></FrameLayout>
</RelativeLayout>
stand1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Stand1list">
</ListView>
</LinearLayout>
from main activity
if (id == R.id.Stand1) {
fm.beginTransaction().replace(R.id.content_frame, new Stand1()).commit();
setTitle(R.string.Stand1);
Following tutorials online this is my Stand1.java file which is used to populate the a list with a array of strings
public class Stand1 extends Fragment {
ListView mList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row_layout,pair);
mList.setAdapter(adapter);
}
}
and row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android" />
This is working, It displays the 5 strings. However i want to make it so i can also have a check box in each row.
If i try place linear layout in row_layout.xml , i get error
ArrayAdapter requires the resource ID to be a TextView
I was following stackoverflow which sorted other errors which suggested that i had to make the list at class level and inflate it in onCreate.
I know i prob have to make an custom array adapter, but my problem is how do i deal with the inflation of the view in the on create.
Im not really sure how i go about doing this. So can somebody tell me from here, how do i go about having a check box in each row.
Thanks for the help
ArrayAdapter needs to know where to put the text from your string array. By default, it assumes the layout you pass it is a TextView and will set its text to one of the strings. If you pass it a more complicated layout, you need to tell it the id of the TextView you want it to use. You can do that with this constructor.
obviously not a profound question, but I think theres probably an error in my approach.
So I've added the file listview.xml to my project in the layout folder.
it contains this:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
I've also tried:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
And i my main activity function generally looks like this:
public class ToDoManagerActivity extends ListActivity {
private static final int ADD_TODO_ITEM_REQUEST = 0;
private static final String FILE_NAME = "TodoManagerActivityData.txt";
private static final String TAG = "Lab-UserInterface";
// IDs for menu items
private static final int MENU_DELETE = Menu.FIRST;
private static final int MENU_DUMP = Menu.FIRST + 1;
ToDoListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
// TODO - Inflate footerView for footer_view.xml file
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
// NOTE: You can remove this block once you've implemented the assignment
if (null == footerView) {
return;
}
// TODO - Add footerView to ListView
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
This keeps breaking with the error:
...java.lang.RunTimeException: content must have a ListView whose id attribute is 'android.R.id.list'
I noticed that if I comment out the setContentView(R.layout.footer_view) bit the error disappears, which is quite confusing as well, because it seems like the error should trigger before there.
This is confusing the hell out of me because as far as I can tell this element exists. Its seems like maybe I'm missing a step to load the ListView? I'm banging my head against the wall here and this seems like something really basic, and being a n00b sucks...So any help is much appreciated!
Cheers!
EDIT:
footer_view.xml contents:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
EDIT2:
Current onCreate after suggested edits:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
setListAdapter(mAdapter);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footerView);
}
For an Activity that is extending ListActivity, the ListView needs to exist within the xml file that you are using in the activity, in your case that's the footer_view file.
Taken from the Google Dev Site - "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list""
To incorporate your footer below your list, try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
</LinearLayout>
Here, try this in your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
That is exactly what I have in my app.
Then in your onCreate(), just this:
setContentView(R.layout.activity_main);
Try it and let me know if it works. That code should work. From there, we can continue on to isolate the problem.
EDIT:
Man, now I see your problem. Not a big deal here. Let's look at your code (edited to be shorter). The onCreate() method has this:
super.onCreate(savedInstanceState);
mAdapter = new ToDoListAdapter(getApplicationContext());
getListView().setFooterDividersEnabled(true);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
Do you see R.layout.activity_main anywhere there? There's your issue! Your R.layout.footer_view only contains a textview, it doesn't contain the ListView, and your Activity keeps looking for the ListView that you promised it that you'd have by extending a ListActivity.
Try this - change your onCreate() method to this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new ToDoListAdapter(getApplicationContext());
setListAdapter(mAdapter);
}
Then your activity_main.xml layout file should look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
This should work. Let me know.
EDIT2:
Now add this to the end of your onCreate() method:
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
Voila! Should work now.
My layout includes an unpopulated horizontal linear layout with a multicolumn listview below. The listview is populated from an sqlite table via a custom cursor adapter in a separate java file. each time a particular view in a listview row is tapped, a button is dynamically created and added to the linear layout. For reasons I don't understand, when this happens, the listview content disappears. If I change the orientation of the device, it goes through the onCreate and the display builds correctly with the linear layout buttons showing correctly over the listview.
The main layout file:
<?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" >
<LinearLayout
android:id="#+id/llRunnerList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<ListView
android:id="#+id/list_data"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and the view listener code in the custom cursor adapter:
private OnClickListener strRunnerOnClickListener = new OnClickListener() {
#Override
//
// When the Runner field is clicked, add the Runner name to the Runner row.
//
public void onClick(View v) {
final DBAdapter databaseHelper = new DBAdapter(ctxThis);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tvRunner = (TextView) v;
String strRunner = (String) tvRunner.getText();
if(RaceSheetActivity.blnRaceRunning) {
// get the ListView and the position in it.
ListView lv = (ListView) v.getParent().getParent();
final int position = lv.getPositionForView((View) v.getParent());
// get the cursor pointing to that row
Cursor c = (Cursor) lv.getItemAtPosition(position);
long lngCurrentId = c.getInt(0);
long lngRaceId = c.getInt(1);
c.close();
// Add to database if doesn't exist
Cursor cSNRec = databaseHelper.getSNRecByCompetitorId(lngCurrentId);
if(cSNRec.moveToFirst()) {
Toast.makeText(v.getContext(), strRunner + " already in preempt list!", Toast.LENGTH_SHORT).show();
}
else {
databaseHelper.insertSN(strRunner, lngCurrentId);
// Add a button and initialise it
ToggleButton btnRunner = new ToggleButton(ctxThis);
btnRunner.setText(strRunner);
btnRunner.setTextOn(strRunner);
btnRunner.setTextOff(strRunner);
btnRunner.setTag(lngCurrentId);
btnRunner.setLayoutParams(params);
btnRunner.setOnClickListener(RaceSheetActivity.btnRunnerOnClickListener);
RaceSheetActivity.llRunnerList.addView(btnRunner); // to delete view removeViewAt(int index)
changeCursor(databaseHelper.getRaceSheetDataForRace(lngRaceId));
notifyDataSetChanged();
}
}
}
};
Would really appreciate any ideas as to how I can keep the listview visible after tapping the runner field and the button appearing. I have tried notifyDatasetChanged without success, reread the database table into the cursor adapter.
Set your layout heights to wrap_content because your ItemList with the buttons will overlap your Listview if it's not empty.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/llItemList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="#+id/list_data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
Actually you are not initializing the HorizontalScrll view And just close the Horizontalscrollview due to that App return this type of behaviour
Actually, solved the problem eventually. The listview was disappearing because I had used a cursor to get the adapter values for the row that was returned, and subsequently closing the cursor apparently cleared the listview.
Thanks to both those who offered solutions albeit they weren't the cause of the problem.
The below is the xml that iam inflating within my customList
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<exp.viswanath.dropper.CustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#e1e1e1"
android:padding="4dp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/custom_view" />
</RelativeLayout>
</LinearLayout>
And my custom view 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:orientation="vertical" >
<exp.viswanath.dropper.CustomList
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Iam getting the height of Layout with id parent and listview as -1 and -2 . But iam getting the correct height for element with id custom_view as 105. Iam inflating this within the constructor of my CustomList
CODE
View rowView = LayoutInflater.from(context).inflate(R.layout.activity_dropper, null);
parentLayout = (RelativeLayout)rowView.findViewById(R.id.parent);
Log.d("", "DEBUG:pl:" + parentLayout.getLayoutParams().height);
myView = (CustomView)parentLayout.findViewById(R.id.custom_view);
_viewHeight = myView.getLayoutParams().height;
initList();
_height = (-1) * _viewHeight;
Log.d("","height:" + _height);
listView = (ListView)parentLayout.findViewById(R.id.listView);
Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);
Does any one knows the problem or experienced the same? The layout is not visible at all
PROBLEM
MY VIEW IS NOT VISIBLE
The problem is that myView.getLayoutParams().height; will not return you the height as it has not been measured yet. In tghis case it returns you the constant associated with the view such as FILL_PARENT, WRAP_CONTENT etc. See documentation here:
Information about how tall the view wants to be. Can be one of the
constants FILL_PARENT (replaced by MATCH_PARENT , in API Level 8) or
WRAP_CONTENT. or an exact size.
To get the size of a layout, your should be using getWidth() or getMeasuredWidth() methods. However, these methods won't give you a meaningful answer until the view has been measured. Read about how android draws views here.
You can get the correct size, by overriding the onWindowFocusChanged() as mentioned in this thread.
Alternatively, you could do this:
myView.post(new Runnable() {
#Override
public void run() {
Log.d("", "DEBUG:lv:" + myView.getMeasuredHeight());
}
});
Try this way
listView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);
}
});
I tried to used android:scrollbarThumbVertical="#drawable/scrollbar" parameter to add image as scrollbar. The scroll bar was changed but the problem is, it won't show the original size, it was streted depending on listview size (items). If the List item count is 20 means it show right dimension, it goes around 50 means, the scroll bar is stretched in entire view. Is there a way to avoid stretching. Here i will add my code with scroll bar image as well as my out put too.
Main.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:background="#234232"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/hello"
android:textSize="20sp" />
<ListView
android:id="#+id/listView1"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:fadeScrollbars="false"
android:fadingEdge="none"
android:scrollbarSize="12dip"
android:scrollbarStyle="outsideInset"
android:scrollbarThumbVertical="#drawable/scrollbar" >
</ListView>
</LinearLayout>
CustomScrollBarAndroidActivity.java
public class CustomScrollBarAndroidActivity extends Activity {
private ListView list;
private static List<String> listItems = initListItems();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems));
}
private static List<String> initListItems() {
List<String> list = new ArrayList<String>();
boolean isNotFinish = true;
int i = 0;
while(isNotFinish){
list.add("list item #" + i);
if (i > 50){
isNotFinish = false;
}
i++;
}
return list;
}
}
,
my Scroll bar image is
Use draw9 patch for this image :)