android listview context menu - android

I want to put a context menu on a listview. This seems very simple to do but I am getting some interesting behavior. The listview has two fields, a textview and an editText. The long press opens up the menu on the EditText but nowhere else on the listview.
Here are the pieces of code.
public class ManageClass extends ListActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.manageclass);
//Tons of not relevant stuff that I would be happy to provide.
pfdata = new PortfolioData(this);
try {
Cursor cursor = getClasses();
showClasses(cursor);
} finally {
pfdata.close();
}
}
private Cursor getClasses() {
String sql = "select c._id, c.NAME as NAME , c.percentage as PERCENTAGE "
+ "from asset_classes c;";
SQLiteDatabase db = pfdata.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
startManagingCursor(cursor);
return cursor;
}
private void showClasses(Cursor cursor) {
adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor,
FROM, TO, t);
// Log.d("TEST", Integer.toString(adapter.getCount()));
setListAdapter(adapter);
adapter.notifyDataSetChanged();
//I've tried moving this line around but it hasn't made a difference.
registerForContextMenu(getListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 0, 0, "A");
menu.add(Menu.NONE, 1, 1, "B");
menu.add(Menu.NONE, 2, 2, "C");
}
Lastly, the layout itself which I admit is overly complicated
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/editText1"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="#string/TCLabel" />
<TextView
android:id="#+id/classtotalpercentage"
android:layout_width="109dp"
android:layout_height="wrap_content"
android:textSize="50px" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/AddClassLabel"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:text="#string/AddClassLabel" />
<Button
android:id="#+id/addclass_button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="#string/addclassbutton_label" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
android:orientation="horizontal"
android:padding="10sp" >
<TextView
android:id="#+id/assetclassHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/assetclassHeader"
android:width="200dp" />
<TextView
android:id="#+id/percentageHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/assetclassHeader"
android:text="#string/percentageHeader"
android:width="100dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_list_data" />
</RelativeLayout>
</LinearLayout>
I appreciate any advice. I would like the menu on the textview and not the edittext, if possible.
An update. I have found that it has something to do with the EditText. If I change it to a textview or remove it alltogether, then everything works as it should.

I had a similiar problem with checkboxes and found the issue was caused by the checkbox getting the focus. I changed the attribue to:
android:focusable="false"
and it now works correctly.

Related

How to map a list of list to RecyclerView

I am trying to map a List<List<Object>> to a RecyclerView in Android, but the result is totally messed up.
For example, I have a list of list like this (just for explaining, not my real case):
List<List<String>> list = {{a, b, c}, {d, e}, {f, g, h, i}};
the Recyclerview should display like (first row contains three subrows, second has two and the last on has four):
|----a-----|
|----b-----|
|----c-----|
|=======|
|----d-----|
|----e-----|
|=======|
|----f-----|
|----g-----|
|----h-----|
|----i-----|
but the result is not exactly like the above order. some element is duplicated and some disappears. for example:
|----d-----|
|----e-----|
|----c-----|
|=======|
|----d-----|
|----e-----|
|=======|
|----f-----|
|----a-----|
Here is a piece of my code:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
List<Event> list = eventList.get(position);
if (holder.rows < list.size()) {
holder.rowViewGroup.removeAllViews();
holder.rows = 0;
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView startView = new TextView(context);
startView.setLayoutParams(layoutParams);
Event headEvent = list.get(0);
Calendar startCal = headEvent.getStartCal();
startView.setText(String.format("%tD", startCal));
holder.rowViewGroup.addView(startView);
for (final Event event : list) {
View element = LayoutInflater.from(context).inflate(R.layout.element, null);
//start time view
TextView startTimeView = (TextView)element.findViewById(R.id.eventStartTimeInElement);
Calendar startTimeCal = event.getStartCal();
startTimeView.setText(String.format("%tl:%tM %tp", startTimeCal, startTimeCal, startTimeCal));
//end date time view
TextView endView = (TextView)element.findViewById(R.id.eventEndTimeInElement);
Calendar endCal = event.getEndCal();
endView.setText(String.format("%tD %tl:%tM %tp", endCal, endCal, endCal, endCal));
//title view
TextView title = (TextView)element.findViewById(R.id.eventTitleInElement);
title.setText(event.getTitle());
//member name
Drawable divider = context.getResources().getDrawable(R.drawable.divider);
ImageView dividerView = new ImageView(context);
dividerView.setImageDrawable(divider);
holder.rowViewGroup.addView(dividerView);
holder.rowViewGroup.addView(element);
holder.rows++;
}
}
}
Here is my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycleViewRow">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rowView"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/eventStartTimeInRow"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
and element.xml(which is contained by row.xml):
<?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="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/eventStartTimeInElement"
android:layout_weight="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/eventEndTimeInElement"
android:gravity="end"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/eventTitleInElement"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/memberNameInElement"
android:gravity="end"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And I know this is not a really good idea to implement this, so could anybody please tell me a better way to implement this? Thanks...
I think your best bet is to flatten your list of lists into one list.
This is just for purposes of the ListView. Your adapter will act as though the lists have all been strung into one long list.
Start by creating a single-list index for all the items of your nested lists.
For example:
List<List<String>> listOfLists = ... // your original data
List<String> listForView = new ArrayList<String>();
for (List<String> innerList : listOfLists) {
for (String str : innerList) {
listForView.add(str);
}
listForView.add("---"); // e.g. marker value for divider
}
Now in your adapter's getItem() need only return listForView.get(position).
Anytime your nested list structure changes, you redo this flattening operation and call notifyDataSetChanged().
Things get a little trickier if — given the position — you have to figure out which list an item is in, but you could always index the inner lists in a similar fashion.

How to focus an item in Gridview

I am trying a calendar application. I used Baseadapter and Gridview for work the calendar. I want, when an user clicks an item in calendar, clicked items change background color.
I found several methods for this. setSelection method, requestFocus method etc. Then I decided using the requestFocus.
My Problem
When I click an item in calendar, It sends requestFocus and everything is okay. Then I click another item and It works okay. Main problem starts here, When I click an item that I've already clicked on, It doesn't work. So an item works only once.
I hope you can understand my question.
Please suggest a solution to me.
thank you.
PlaceholderFragment.java
calendarAdapter = new CalendarAdapter( whole_list, getActivity() );
calendarGridView = ( GridView ) rootView.findViewById( R.id.calendarGridView );
calendarGridView.setAdapter( calendarAdapter );
calendarGridView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick( AdapterView< ? > parent, View view, int position, long id ) {
DaysSettings get_spesific = whole_list.get( position );
RelativeLayout r = ( RelativeLayout ) view.findViewById( R.id.calendar_border );
r.setFocusable( true );
r.setFocusableInTouchMode( true );
r.requestFocus();
}
} );
calendar_inflate.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:background="#drawable/selector"
android:layout_marginTop="#dimen/calendar_rl_margin"
android:layout_marginBottom="#dimen/calendar_rl_margin"
android:layout_width="#dimen/calendar_circle_wh"
android:layout_height="#dimen/calendar_circle_wh"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/calendar_border">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/calendarparentlayout"
android:layout_margin="1dp"
>
<ImageView
android:contentDescription="Cinsel ilişki olduğunda kalp işareti çıkması için kullanılır."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/had_sex"
android:src="#drawable/heart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/circle_white"
android:padding="3dp"
android:visibility="gone"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="0"
android:id="#+id/calendarTextiew"
android:textColor="#color/main_color_one"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/calendarTextiew"
android:gravity="center_horizontal">
<ImageView
android:contentDescription="İlaç hatırlatırıcı resmidir."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/medicine_reminder"
android:src="#drawable/pill"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:visibility="gone"
/>
<ImageView
android:contentDescription="Note eklendi resmidir."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/adding_note"
android:src="#drawable/plus"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:visibility="gone"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>

ListView OnItemClickListener not Working

I create a statistics activity with listView . but onItemClick Not working I surffed internet but given solution not working for me.
try
{
int counter=0;
db = helper.getReadableDatabase();
c = db.query(DBHelper.Result, null, null, null, null, null, null);
c.moveToFirst();
do
{
counter++;
States state = new States(c.getString(2), c.getString(3), false);
stateList.add(state);
}while(c.moveToNext());
Log.d("counter", ""+counter);
/*adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String [] {DBHelper.R_test,DBHelper.R_testNM}, new int []{R.id.rowTxt1,R.id.rowTxt2});
Lv.setAdapter(adapter);
Lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);*/
db.close();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("Error", ""+e.getMessage());
}
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.row, stateList);
//ListView listView = (ListView) findViewById(R.id.LvStatistics);
// Assign adapter to ListView
Lv.setAdapter(dataAdapter);
Lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("click", "0");
}
});
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical" >
<TextView
android:id="#+id/rowTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/ExamFontColor"
/>
<TextView
android:id="#+id/rowTxt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rowTxt1"
android:layout_marginLeft="20dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/FontBlack"
/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:text="1" />
</RelativeLayout>
exam_statistics.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Background"
android:orientation="vertical" >
<TextView
android:id="#+id/statisticsHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/HeaderColor"
android:gravity="center"
android:text="#string/ButtonStatistics"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/FontWhite"
android:textSize="30sp" />
<ListView
android:id="#+id/LvStatistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnDelete"
android:background="#color/Background"
>
</ListView>
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/statisticsHeader"
android:layout_marginBottom="10dp"
android:background="#drawable/button_green_effect"
android:text="#string/ButtonDelete"
android:textColor="#color/FontWhite"
android:textSize="27sp" />
</RelativeLayout>
I tried a Lot but not Working help me to solve this problem
write this in ur xml inside checkbox tag
android:focusable="false"
try after using this..
If your MyCustomAdapter implements OnItemClickListener so you have to use the following code :
MyCustomAdapter.setOnItemclikListener(dataAdapter);
As there is a checkBox in the row, ListView OnitemClickListener wont work. You can write OnItemClickLister in the row (Adapter) , to get the click event.
If you add args2 in log does it return the click position?
Log.d("click", "Row " + args.toString);

problem in TextView in android

hi all i m making a sample app. for update List.
i have a list class and its xml is like.
<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/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent">
</TextView>
</LinearLayout>
now i have set TouchListener of text View and added the following code in it
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId())
{
case R.id.more:
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
// TODO Auto-generated method stub
return false;
}
You see on the 3rd line of switch statement i have added
more.setText("Click to view More..");
line but when my list is updated . The text View is no longer shows in the bottom .
Please Guide my why this is happening to me and whats the solution??
you can use list footer in that case.
add this code in ur java file. and add footer dynamically in your list.
TextView more = new TextView(this);
more.setText("Click to view more...");
more.setClickable(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, Height);
more.setLayoutParams(params);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
});
ListView listView = (ListView) findViewById(R.id.ListView01);
listView.addFooterView(more);
this can help you.
try this ...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_height="fill_parent" android:layout_above="#+id/more"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_alignParentBottom="true">
</TextView>
</RelativeLayout>
try android:layout_height="fill_parent" android:layout_above="#+id/more" in the ListView. Now even if ur ListView grows it wont hide the TextView.
UPDATE
<RelativeLayout android:id="#+id/relativeList"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/LinearBottom3">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/chathlist"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/LinearBottom3"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"><!--Make it as center-->
<TextView android:id="#+id/more" android:layout_height="wrap_content"
android:text="Click to view more..." android:textStyle="normal|bold" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent"/>
</ReletiveLayout>
try this..
footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:textSize="30dip" android:gravity="center_horizontal"
android:text="Click to view More.." android:layout_width="fill_parent"
android:id="#+id/more"></TextView>
</LinearLayout>
in class file
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout footer = (LinearLayout)inflater.inflate(
R.layout.footer, null);
TextView more= (TextView)footer.findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//List Update Code
}
});
use the following code..
footer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="footer_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
</LinearLayout>
and use the following in code:
public class listactivty extends ListActivity{
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
use seperate XML file for ListView..
Thanks
Shahjahan Ahmad

Implementing a listview inside a sliding drawer with a listview already present

I have an app whose main class extends ListActivity:
public class GUIPrototype extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Cursor c = managedQuery(People.CONTENT_URI, null, null, null, null);
String[] from = new String[] {People.NAME};
int[] to = new int[] { R.id.row_entry };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.drawer,c,from,to);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
}
I have a sliding drawer included in my XML, and I'm trying to get a separate listview to appear in the sliding drawer. I'm trying to populate the second listview using an inflater:
View inflatedView = View.inflate(this, R.layout.main, null);
ListView namesLV = (ListView) inflatedView.findViewById(R.id.content);
String[] names2 = new String[] { "CS 345", "New Tag", "Untagged" };
ArrayAdapter<String> bb = new ArrayAdapter<String>(this, R.layout.main, R.id.row_entry, names2);
namesLV.setAdapter(bb);
This compiles, and runs, but the slidingdrawer is completely blank. My XML follows:
<SlidingDrawer
android:id="#+id/drawer"
android:handle="#+id/handle"
android:content="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom">
<ImageView
android:id="#id/handle"
android:layout_width="48px"
android:layout_height="48px" android:background="#drawable/icon"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#id/content"/>
</SlidingDrawer>
I feel like I'm missing a vital step. I haven't found any resources on my problem by Googling, so any help would be greatly appreciated.
Edit: This was for a problem a long time ago, and the solution I found was to just redesign my layout. I am unable to accept an answer as I don't have the means to test them.
I Suppose I might have found the solution.
All these above solutions did not worked for me.
But then, what I did was add onClickListener to actual view which I return from adapter and BAM it started working for me.
Here is the sample code:
May layout XML (Not complete one....)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:id = "#+id/scrolling"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:paddingBottom="30dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/listingIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
............
</ScrollView>
<SlidingDrawer
android:id="#+id/slidingDrawerShowMore"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:topOffset="132dip"
android:handle="#+id/handle"
android:content="#+id/content">
<LinearLayout
android:id="#+id/handle"
android:padding = "5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<TextView
android:id="#+id/title"
android:layout_alignParentRight="true"
android:textSize="14dp"
android:layout_below="#id/rate"
android:singleLine="true"
android:textColor="#3F48CC"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/show_more"/>
</LinearLayout>
<LinearLayout
android:id="#id/content"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center"
android:background="#android:color/black">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:background="#drawable/dark_header">
<TextView
android:id="#+id/otherTitle"
android:layout_alignParentRight="true"
android:layout_below="#id/rate"
android:singleLine="true"
android:textSize="21px"
android:paddingLeft="10px"
android:textColor="#EBEBEB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.6"
android:text="#string/someString"/>
<ProgressBar
android:id="#+id/pbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/Widget.ProgressBar.Small"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Now to handle click events all I had to do was to add onClickListener in my adapter
public View getView(int position, View convertView, ViewGroup parent) {
convertView.setOnClickListener(this);
}
That's it. Problem is I could not get my onItemClickListener working for this ListView. But right now on click listener works for me. One day I would love to find out reason behind this.
It looks like the problem could be that you are inflating a new instance of a ListView rather than using the one in your view.
Try getting the ListView with ListView listView = (ListView) findViewById(R.id.content);
Then apply your adapter to it.
Have you tried
View inflatedView = View.inflate(this, R.layout.main, null);
SlidingDrawer sliding=(SlidingDrawer) inflatedView.findViewById(R.id.drawer);
ListView namesLV = (ListView) sliding.findViewById(R.id.content);

Categories

Resources