Android Drag&Drop in a ListView with custom adapter - android

I am trying to make a ListView with a custom adapter and add an onClickListener and a drag-and-drop functionality to sort the list.
I was looking at a lot of examples and similar problems, but I couldn't make it work, so I think my problem may be specific, because I use a whole layout as an item of the list, but I am not sure.
If I can provide any further information, please tell me.
You can find the whole project here
The implementation of my ListActivity is:
package com.jimdo.welcomeghosts.cipherrsa;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class TabKeyActivity extends ListActivity {
//Defining an adapter which will handle the data of the listview
KeyAdapter adapter;
private ListView list;
//dummy data field TODO fill this shit up by using ArrayList
Key key_data[] = new Key[]
{
new Key(1, R.drawable.iconpair1, "This"),
new Key(2, R.drawable.iconpair2, "This'"),
new Key(3, R.drawable.iconpair1, "That"),
new Key(4, R.drawable.iconpair2, "That'")
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keymanager);
//Setting an ADD button as last item of the ListView
list = (ListView) findViewById(android.R.id.list);
list.addFooterView(getLayoutInflater().inflate(R.layout.footerview,null));
//Setting adapter for adding Items to ListView
adapter = new KeyAdapter(this, R.layout.key, key_data);
setListAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
This is my Key.xml (a row element in the List)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"><RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/checkBox"
android:layout_toStartOf="#+id/checkBox"
android:id="#+id/keyRow">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="50px"
android:layout_height="50px"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:longClickable="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/imgIcon"
android:paddingLeft="5dp">
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing the shit."
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textSize="#dimen/abc_text_size_headline_material" />
</FrameLayout>
</RelativeLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" />
</RelativeLayout>

Drag-Sort Listview is what you need.
Have a look at the following thread.

Related

Trouble catching onClick event from listview

I'm asking my user for a postcode via an editText then i use a button to fetch and display store locations in a listView using a simpleCursorAdapter.
Now i want to be able to launch a new activity by selecting an item in the listView and passing along some string information.
I can't get the onClickListener to register my clicks.
Is it because i'm using an Activity instead of a ListActivity?
I'd rather have both the input (the EditText) and the results (ListView) both in the same activity.
activity_pcentry:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/enter_post" />
<EditText
android:id="#+id/etPostCode"
android:imeOptions="actionSearch"
android:inputType="textCapCharacters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search_button"
android:onClick="DoPostSearch" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
stockrow_group:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/branch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
<TextView
android:id="#+id/post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
<TextView
android:id="#+id/telephone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
</LinearLayout>
PostCodeEntryActivity.java:
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class PostCodeEntryActivity extends Activity{
private MyDatabase stockistsDB;
public EditText enteredCode;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pcentry);
stockistsDB = new MyDatabase(this);
}
public void DoPostSearch(View v) {
enteredCode = (EditText)findViewById(R.id.etPostCode);
String postalCode = enteredCode.getText().toString();
Cursor results = stockistsDB.getStockistsFromPostCode(postalCode);
if (results != null && results.getCount() > 0) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.stockrow_group,
results, new String[]{"fld_BranchName","fld_PostCode","fld_Tel"},
new int[]{R.id.branch,R.id.post,R.id.telephone},
0);
listView = (ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(PostCodeEntryActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
listView.setAdapter(adapter);
} else {
Toast.makeText(PostCodeEntryActivity.this, "No results found, please check your postcode", Toast.LENGTH_SHORT).show();
}
}
}
Do you have any clickable elements on the rows themselves? Please post the XML from R.layout.stockrow_group.
If you have buttons on that layout for example you might need to add the following to those buttons:
android:focusable="false"
android:focusableInTouchMode="false"
This will make sure the click on the list item is not getting blocked by the clickable item on the row itself.
The actual solution:
Do not make the rows of the listview clickable. This will "eat" up the click event by registering an empty click handler. Remove the following line from the rows of the list view:
android:clickable="true"
Implement AdapterView.onItemClickListener in your activity`
Change
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
To
listView.setOnItemClickListener(this)
Add unimplemented methods

Android: Items on the ListView are not clickable

My activity contains a ListView that is populated from an SQLite Database. However I find that the items on the ListView are not clickable. They are not clickable at all in the emulator or on the device. I know there are a lot of similar questions and I tried every possible one of them but to no avail. Please help me make the items clickable.
My Activity Code :
package com.tintin.scheduler_3;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
public class List_Course extends ListActivity {
DatabaseHelper db;
SimpleCursorAdapter dataAdapter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.course_list);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(List_Course.this, Add_Course.class));
}
});
Log.v("Button", "On Click Done");
db = new DatabaseHelper(List_Course.this);
Log.v("Button", "On Click Done2");
displayList();
Log.v("Button", "On Click Done3");
db.close();
}
public void onResume(){
Cursor newCursor = db.getAllCourses();
dataAdapter.changeCursor(newCursor);
super.onResume();
db.close();
}
public void displayList(){
Cursor cursor = db.getAllCourses();
String from [] = new String[] {db.colName,db.colDisplay};
int to[] = new int[] {R.id.textView1, R.id.textView2};
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem, cursor, from, to, 0){
public View getView(int position, View convertView, ViewGroup parent){
final View row = super.getView(position, convertView, parent);
if(position % 2 == 0)
row.setBackgroundColor(Color.parseColor("#D8D8D8"));
else
row.setBackgroundColor(Color.parseColor("#D0D0D0"));
return row;
}
};
ListView lv = getListView();
lv.setAdapter(dataAdapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("Click","Anything "+arg2+" "+arg3);
}
});
cursor.close();
db.close();
}
}
The Layout of this Activity :- (NOTE: I found that without the TextView the list populated from bottom up even though android:stackfromBottom was not set. So I put in the TextView and put the ListView below that)
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#D0D0D0" >
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Add a Course" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="1dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" >
</TextView>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv"
android:layout_above="#+id/add"
android:layout_alignParentLeft="true"
android:divider="#FFFFFF"
android:clickable="true"
android:dividerHeight="2dp" >
</ListView>
</RelativeLayout>
And the listitem.xml that defines how rows are to be shown in the listview :-
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
EDIT:: - I noticed that the items on my ListView are actually clickable. The problem however is that on clicking the list items the orange selection color is not shown. So unless the log is checked, it is not apparent which row was clicked on.However if I remove the part where I am overriding the getView method of the SimpleCursorAdapter while defining dataAdapter, on clicking the items there is an orange selection color that appears in the background of the clicked row.
In other words I am not getting any color for the focused row in list view.
Please run it in one of AVDs to understand what I am trying to say here. I would appreciate if anyone can help me get that color while also overriding the getView method.
I am almost 100% sure that your problem is that some of the views rendered inside the ListView's items is avoiding the ItemClick event. In order to solve this, start by removing the follwing attributes from the TextView:
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAppearance="?android:attr/textAppearanceSmall"
Try to override onListItemClick() instead of setting OnItemClickListener. Subclasses of ListActivity should override this method.
http://developer.android.com/reference/android/app/ListActivity.html
I think all problems are in your xml's code.
Change your listitem.xml as..
?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="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
And from code behind use below code.Ofcourse use this in your onCreate() method
ls1 = (ListView) findViewById(R.id.yourListviewName);
ls1.setOnItemClickListener(new OnItemClickListener()
{
#Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
}
});
Alternatively see similar issues here..
android-how-to-programmatically-click-select-tap-a-listview-item/
how-to-click-a-listview-item-in-android

Button below listView

I am new to Android, and the first project I am working on is putting players onto a team. I have a listview that grabs all of the players from a database. Each name is supposed to have a checkbox next to them and then there is a button at the bottom that is supposed to update the database. So far this is what I am getting.
http://dyp.im/W79JdmfIk
Here is my activity
package com.example.sqllitetest;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Tester extends ListActivity {
MyAdapter mListAdapter;
PlayersDBAdapter mDbHelper;
private Button button;
private Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new PlayersDBAdapter(this);
mDbHelper.open();
Cursor myCur = mDbHelper.fetchAllPlayers();
startManagingCursor(myCur);
setContentView(R.layout.playertoteam);
// registerForContextMenu(getListView());
// ((Tester) context).setContentView(R.layout.playertoteam);
//getListView().addFooterView(button);
mListAdapter = new MyAdapter(Tester.this, myCur);
setListAdapter((ListAdapter) mListAdapter);
Button button = (Button) findViewById(R.id.alertBox);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
finish();
}
});
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.playertoteam, cur);
}
#Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.playertoteam, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.code);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.checkBox1);
tvListText.setText(cur.getString(cur.getColumnIndex(mDbHelper.KEY_NAME)));
cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(mDbHelper.KEY_ID))==1? false:true));
}
}
}
And here is my xml 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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/confirm" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/alertBox"
/>
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
</LinearLayout>
I would like to know how to remove the buttons from the rows and the check box that is underneath the main confirm button. Also I would like to know if this is the best way to do something like this, because, once again, I am new and I don't know how to search for the correct methods.
ResourceCursorAdapter creates views ,for every row of the ListView, that are inflated in newView() method. You are getting button in every row because of this reason.
Soln: Create a separate xml defining the row layout for the ListView and inflate this new layout in the newView() method. And have only the button and ListView in main layout i.e. playertoteam.xml
playertoteam.xml
<?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="wrap_content"
android:padding="6dip" >
<Button
android:id="#+id/alertBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/confirm" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/alertBox" />
</RelativeLayout>
row_layout.xml
<?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="wrap_content">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"/>
</RelativeLayout>
You can find example for ListView here
you can use addFooterView method of listview.
View footerView =
((LayoutInflater)
ActivityContext.
getSystemService
(Context.LAYOUT_
INFLATER_SERVICE)).inflate
(R.layout.footer_
layout, null, false);
ListView.addFooterView
(footerView);

Android UI - add buttons dynamically to Gridview

This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture
I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.
Please provide some directions.
create_team_new.xml(layout where gridview resides)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/parentcreateteam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TextView
android:id="#+id/citytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city"
android:layout_alignTop="#+id/teamcity"
android:layout_alignParentLeft="true" />
<AutoCompleteTextView
android:id="#+id/teamname"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/teamcity"
android:layout_alignRight="#+id/gridviewplayer"
android:text="#string/teamname"
android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/createteam"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Create Team"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/red_button"
style="#+style/button_text" />
<TextView
android:id="#+id/teamnametextview"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_alignBaseline="#+id/teamname"
android:layout_alignBottom="#+id/teamname"
android:layout_alignParentLeft="true"
android:text="#string/teamname" />
<AutoCompleteTextView
android:id="#+id/teamcity"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cricket"
android:layout_alignRight="#+id/gridviewplayer"
android:layout_below="#+id/teamname"
android:layout_marginTop="22dp"
android:ems="10"
android:text="#string/city"
android:textSize="18sp" >
</AutoCompleteTextView>
<TextView
android:id="#+id/radiotextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/teamcity"
android:layout_marginTop="15dp"
android:text="Pick a Sport" />
<GridView
android:id="#+id/gridviewplayer"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_above="#+id/createteam"
android:layout_below="#+id/pick_sport"
android:layout_centerHorizontal="true"
android:background="#drawable/gv_bkg"
android:padding="5dp"
android:numColumns="4" >
</GridView>
<RadioGroup
android:id="#+id/pick_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/createteam"
android:layout_below="#+id/radiotextview"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/cricket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket" />
<RadioButton
android:id="#+id/soccer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer" />
</RadioGroup>
</RelativeLayout>
gv_createplayer.xml(view to be inflated inside gridview)
<?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/btn_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text= "+"
android:background="#drawable/red_button">
</Button>
</LinearLayout>
PlayerAdapter.java(gridview Adapter)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
// Keep all Images in array
public Integer[] mThumbIds = {};
/* R.drawable.gridview_createteam, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15
};
*/
// Constructor
public PlayerAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressWarnings("static-access")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = new View(mContext);
view.inflate(mContext, R.layout.gv_createplayer,null);
return view;
}
}
1) How to create a list view inside a main view, only using a half of the screen.
This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.
2) How to populate list view in grid manner (a row of two columns will repeat itself)
Well, with the GridView (the adapter API is the same) (:

old listview still there

I have an activity that is works fine and displays data obtained from a table query. With an OnItemClick method, I have it set up so that if a row is selected, a new activity starts with a new ListView, but a lot of the elements from the original ListView are in the new listview, including a toggle button on each row, and a text view. Any idea what could be causing this?
This is the initial ListView. The buttons don't show up, just everything in the ListView:
<?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="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<Button
android:id="#+id/btnAddNurseToRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:focusable="true"/>
<Button
android:id="#+id/btnRemoveNurseFromRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnAddNurseToRoster"
android:layout_toRightOf="#+id/btnAddNurseToRoster"
android:text="#string/delete"
android:focusable="true"/>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/btnAddNurseToRoster"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</RelativeLayout>
New ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<TextView
android:id="#+id/tvAssignmentsText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FFFFFF"
android:text="Assignments"
android:gravity="center_horizontal"
android:background="#663399"
/>
<ListView
android:id="#+id/lvAssignmentsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tvAssignmentsText"
android:paddingTop="20dp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</
And here's the code that calls the new ListView:
package com.deadEddie.staffingmanagement;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ShowAssignments extends Activity {
DbCommunicator getAssignmentsList;
ListView assignmentsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.assignments);
displayList();
}
private void displayList() {
// instantiate ListView object
assignmentsList = (ListView) findViewById(R.id.lvAssignmentsList);
// instantiate variables for CursorAdapter
int [] to = new int [] {R.id.rbAssignmentsList };
String [] from = new String [] {DbCommunicator.KEY_ROOM_NUMBER};
// instantiate getAssignmentsList with new DbCommunicator object and open
getAssignmentsList = new DbCommunicator(this);
getAssignmentsList.open();
// get and manage cursor
Cursor assignmentsCursor = getAssignmentsList.getAssignments(this);
startManagingCursor(assignmentsCursor);
// list adapter
ListAdapter assignmentsListAdapter = new SimpleCursorAdapter(this, R.layout.nurse_list, assignmentsCursor, from, to, 0);
assignmentsCursor.moveToNext();
// set ListView
assignmentsList.setAdapter(assignmentsListAdapter);
assignmentsList.setItemsCanFocus(true);
}
}
Adding the code used to populate the original ListView:
<?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="wrap_content" >
<ToggleButton
android:id="#+id/rosterDutyStatus"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:textOff="Off Duty"
android:textOn="On Duty"
android:textSize="10dp"
android:focusable="false"/>
<TextView
android:id="#+id/rosterListLname"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterDutyStatus"
android:layout_toRightOf="#+id/rosterDutyStatus"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/rosterListFname"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListLname"
android:layout_toRightOf="#+id/rosterListLname"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/rosterListMI"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListFname"
android:layout_toRightOf="#+id/rosterListFname"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/rosterListID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListMI"
android:layout_toRightOf="#+id/rosterListMI"
android:visibility="invisible" />
<TextView
android:id="#+id/rosterViewAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rosterDutyStatus"
android:paddingRight="50dp"
android:text="#string/assigned"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/firstAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterViewAssignment"
android:layout_toRightOf="#+id/rosterViewAssignment"
android:text="#string/noAssignments"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/secondAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/firstAssignment"
android:layout_toRightOf="#+id/firstAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/thirdAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/secondAssignment"
android:layout_toRightOf="#+id/secondAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/fourthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thirdAssignment"
android:layout_toRightOf="#+id/thirdAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/fifthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/fourthAssignment"
android:layout_toRightOf="#+id/fourthAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/sixthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/fifthAssignment"
android:layout_toRightOf="#+id/fifthAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>"
-->
</RelativeLayout>
Code that calls original layout I'm trying to change:
package com.deadEddie.staffingmanagement;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class EditRoster extends ListActivity implements OnClickListener {
String TAG = "EditRoster";
Button addNurse;
Button deleteNurse;
DbCommunicator rosterView;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_roster);
addNurse = (Button) findViewById(R.id.btnAddNurseToRoster);
deleteNurse = (Button) findViewById(R.id.btnRemoveNurseFromRoster);
displayNurseRoster();
displayDialog();
addNurse.setOnClickListener(this);
deleteNurse.setOnClickListener(this);
}
// method to put timer on dialog
public void timerDelayRemoveDialog(long time, final Dialog dialogView){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialogView.dismiss();
}
}, time);
}
private void displayDialog() {
Dialog d = new Dialog(this);
d.setTitle("Select Nurse to Adjust Assignments");
d.show();
timerDelayRemoveDialog(2000, d);
}
public void displayNurseRoster(){
listView = (ListView) findViewById(android.R.id.list);
// int variables filled with NURSE_TABLE data
int[] to_nurseTable = new int[] {
// int list for data from NURSE_TABLE fields
R.id.rosterDutyStatus,
R.id.rosterListLname,
R.id.rosterListFname,
R.id.rosterListMI,
R.id.rosterListID,
// int list for data from ASSIGNMENTS_TABLE fields
R.id.firstAssignment};
// String array holding data fields from NURSE_TABLE
String[] from_nurseTable = new String [] {
// fields from NURSE_TABLE
DbCommunicator.KEY_DUTY_STATUS,
DbCommunicator.KEY_LNAME,
DbCommunicator.KEY_FNAME,
DbCommunicator.KEY_MI,
DbCommunicator.KEY_NURSE_ROWID,
DbCommunicator.KEY_ROOM_NUMBER};
// instantiate instance of DbCommunicator object
rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// get & manage cursor for NURSE_TABLE data
Cursor nurseTableCursor = rosterView.getNurseRosterCursor(this);
startManagingCursor(nurseTableCursor);
// instantiate cursor adaptor
ListAdapter nurseTableAdapter = new SimpleCursorAdapter(this,
R.layout.nurse_list, nurseTableCursor, from_nurseTable, to_nurseTable);
nurseTableCursor.moveToNext();
// set listView
listView.setAdapter(nurseTableAdapter);
rosterView.close();
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(TAG, "onItemClickListener set");
Intent showAssignments = new Intent("com.deadEddie.staffingmanagement.SHOWASSIGNMENTS");
startActivity(showAssignments);
}
});
}// displayNurseRoster()
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnAddNurseToRoster:
Intent add = new Intent("com.deadEddie.staffingmanagement.ADDNURSETOROSTER");
startActivity(add);
break;
case R.id.btnRemoveNurseFromRoster:
break;
} // switch
}
}
Okay. Got it. Just realized that I mistakenly used the same layout (nurse_list) when I instantiate the ListAdapter in each method so it tried to apply my new data query to the original layout (which was screwed up of course). Thanks for taking a look though, really appreciate it.
Sorry if this wastes anyone's time. Maybe a lesson for other newbies out there.

Categories

Resources