Element of listView highlight not fully - android

I try to do simplest listView. I create layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_menu_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="60dp" >
</TextView>
And ListActivity:
public class HistoryMenu extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
String[] values = new String[] { "Edit", "Send" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.history_menu, values);
setListAdapter(adapter);
}
}
Element of listView highlight not fully, only where text.
How to fix it?

Use this as your list item 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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/history_menu_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="60dp" >
</TextView>
</LinearLayout>

Make changes in Textview
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_menu_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="60dp" />

Related

android.widget.FrameLayout cannot be cast to android.widget.ListView

I don't see any errors and the debug is not very useful. How can I fix that error?
This is part of the code and the error appears inside the last line
Thanks in advance!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selfie_list);
mMatrixCursor = new MatrixCursor(new String[]{"_id","_data","_details"});
ListView lVThumbnail = (ListView) findViewById(R.id.selfie_list_layout);
mAdapter = new SimpleCursorAdapter(
getBaseContext(),
R.layout.activity_selfie_list,
null,
new String[] { "_data","_details"} ,
new int[] { R.id.img_thumbnail,R.id.tv_details},
0
);
lVThumbnail.setAdapter(mAdapter);
And this are the layouts used, the first one is the list fragment (since I use a list and detailed fragments), and the second is the list layout. :
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/selfie_list"
android:name="com.lordpato.dailyselfie.SelfieListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context="com.lordpato.dailyselfie.SelfieListActivity"
tools:layout="#layout/lv_layout" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/selfie_list_layout"
android:layout_height="75dp" >
<ImageView
android:id="#+id/img_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:padding="5dp"
/>
<TextView
android:id="#+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/img_thumbnail"
android:padding="5dp"
/>
</RelativeLayout>
selfie_list_layout is not a ListView.
ListView lVThumbnail = (ListView) findViewById(R.id.selfie_list_layout);
...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/selfie_list_layout"
android:layout_height="75dp" >

ListView with footer in Android

In my Android application, I want to add footer to the ListView.
listfooter.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:padding="3dp"
android:layout_height="fill_parent">
<TextView
android:id="#+id/getmore"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:textSize="16dp"
android:textColor="#D61E5C"
android:text="Load More"
android:textStyle="bold"/>
</RelativeLayout>
This is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:orientation="vertical" >
<ListView
android:id="#+id/expecters_listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/first_view"
android:layout_marginTop="2dp"
android:divider="#A9A9A9"
android:dividerHeight="0.5dp"
android:fadingEdge="none"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="#drawable/scroll_thumb"
android:smoothScrollbar="true" />
</RelativeLayout>
This is my Activity class, MainActivity.java:
public class BestExpectersActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expecters_listView = (ListView)findViewById(R.id.expecters_listView);
footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.listfooter, null, true);
//after getting the result
//set the direct messages to list view
expectersAdapter = new BestExpectersAdapter(BestExpectersActivity.this, remindersResult);
expecters_listView.setAdapter(expectersAdapter);
//set the footer
expecters_listView.addFooterView(footerView);
}
}
List is visible with some items, But footer is not visible (not adding to the ListView).
Try to Add footer before setAdapter like following code.
//set the footer
expecters_listView.addFooterView(footerView);
expecters_listView.setAdapter(expectersAdapter);
I hope this will help you.

Why the item selected return nothing when it was called?

I'm trying to create a Request Responses activity and get the itemSelected for use of next activity which is store them into user friendlist. But the itemSelected return nothing when the "Accept" button is clicked.(It should display itemSelected in toast dialog) Anyone knows what happen it is? Additionally, I like to ask why the item in the listView was located at the not accurate location? it seems like more upper that usual, for the second item.How to adjust it properly?
The figure below is layout of NotificationView.java:
NotificationView.java
public class NotificationView extends ListActivity{
String[] people;
ListView lv;
Button btn1,btn2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
NotificationManager mnotis =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mnotis.cancel(getIntent().getExtras().getInt("notificationID"));
String i = getIntent().getExtras().getString("name");
people = i.split("[,]");
Log.d("how",i);
lv= (ListView) findViewById(android.R.id.list);
btn1 = (Button)findViewById(R.id.acceptbtn);
btn2 = (Button)findViewById(R.id.closebtn);
ArrayAdapter<String> list = new ArrayAdapter<String>(NotificationView.this,R.layout.friendlist_item, R.id.friend_name,people);
lv.setAdapter(list);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
lv = getListView();
int i = lv.getCount();
Log.d("count",String.valueOf(i));
runOnUiThread(new Runnable(){
public void run(){
String itemSelected = "Selected items: \n";
for(int i=0;i<lv.getCount();i++){
if(lv.isItemChecked(i)){
itemSelected += lv.getItemAtPosition(i) + "\n";
}
}
Toast.makeText(NotificationView.this,itemSelected,Toast.LENGTH_SHORT).show();
}
});
}
});
}}
view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<ListView android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<FrameLayout android:id="#+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal" >
<Button
android:id="#+id/acceptbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.46"
android:text="Accept" />
<Button
android:id="#+id/closebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.49"
android:text="Close" />
</LinearLayout>
</FrameLayout>
friendlist_item.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" >
<CheckBox android:id="#+id/friend_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight = "88dip"
android:textSize="20dip"
android:textStyle="bold"/>
</LinearLayout>
Your Checkbox isn't related with the method that you are calling, for listview with multiselect items follow the next guide: http://dj-android.blogspot.com/2012/04/milti-selection-listview-android-with.html

The ListView does not scroll. How do I fix it?

I have a ListView inside a Linearlayout.
The listview has a custom cursoradapter.
Everything works fine, except the ListView does not scroll.
Any suggestion more than welcome!! Thanks. maurizio
Here is the XML of the MainActivity
<?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" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is the relevant piece of java code.
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor tabellaCursor=null;
private ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] to = new int[] {R.id.name_entry};
db = new DatabaseHelper(this);
tabellaCursor=db.getWritableDatabase().rawQuery("SELECT _id, colonna1, colo nna2, colonna3 FROM tabella ORDER BY _id", null);
ListAdapter adapter=new MyAdapter(this, R.layout.list_example_entry, tabe llaCursor, new String[]{"colonna1"},to);
ListView lt = (ListView)findViewById(R.id.list);
lt.setAdapter(adapter);
Button addbtn=(Button)findViewById(R.id.buttonAdd);
addbtn.setOnClickListener(new OnClickListener()
{public void onClick(View v){add(); }
});
}
Your ListViews Layout param is "wrap_content". it will expand as you add new items to litview. therefore it won't scroll. If you set it to "match_parent" it will sure start scrolling.And dont forget that a view does scroll only if its contents(childs view what ever)
size is bigger than it.
Add more list items that exceeds height. then you can scroll.
Please use below code, it will solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/buttonAdd"/>
</RelativeLayout>

Trying to use a listview in .xml with a cursor but without a ListActivity

I'm stumped as to how to properly implement an adapter that can pull a column of data from my sqlite database and add it to a listview (based on the android-provided multiple checkbox). All of the existing examples use the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
However, I don't want a string. Here's my code:
public class ListGroups extends Activity {
/** Called when the activity is first created. */
private ExpensesDbAdapter mDBHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
ListView lv = (ListView)findViewById(R.id.list1);
mDBHelper = new ExpensesDbAdapter(ListGroups.this);
mDBHelper.open();
Cursor c = mDBHelper.fetchAllGroups();
startManagingCursor(c);
String[] from = new String[] {ExpensesDbAdapter.KEY_GROUPNAME};
int[] to = new int[] {R.id.groupname};
ListAdapter ladapter = new SimpleCursorAdapter(ListGroups.this, R.layout.grouprow, c, from, to);
lv.setAdapter(ladapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
Here's the view.xml file:
<?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"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
And, because the SimpleCursorAdapter asks for it, here's the grouprow.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
If I use a ListActivity and don't invoke the layout on Creation, I can get it to work, but I need the wrappers around that ListView, and I can't make the .addHeader and .addFooter methods work for me. Any help on creating the proper adapter would be appeciated.
Answered it with some help. Several things were wrong, mostly in the list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView android:id="#+id/header01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Select Groups"/>
<ListView
android:id="#android:id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_around"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/footerbutton01"
android:text="Get Expense Reports" />
</LinearLayout>
This allows you to tie the ListView to that element in the xml. You don't have a name for it, but you can use getListView() in the onCreate method to access it.

Categories

Resources