old listview still there - android

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.

Related

Android Drag&Drop in a ListView with custom adapter

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.

how to display value of textbox on same screen in android application just like whatsaap

This is my main file where i am calling the click event on button to pass the value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle("aakash");
SetContentView(R.id.lst);
Button b = (Button) findViewById(R.id.sendbtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(android.view.View v) {
startActivity(new Intent(View.this,View.class));
}
});
}
**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="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ScrollView
android:id="#+id/lst"
android:background="#fefefe"
android:layout_below="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_holder"
>
</ScrollView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out
Thnks in advance
I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically.
call notifydatasetchanged() every time when you add a new item to it.
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ListView
android:id="#+id/listView_chats"
android:layout_width="match_parent"
android:layout_height="100dp"
>
</ListView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
Write a custom adapter for the listView and add the code to manage the list data.
i got the answer to my question
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
public void setText(){
Button myButton = (Button)findViewById(R.id.button1);
final TextView myText = (TextView)findViewById(R.id.text1);
final EditText myInput = (EditText)findViewById(R.id.edit);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
myText.setText(myInput.getText());
}
});
}
}

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

button events in dynamic ListAcitvity android

Hi I have generated a dynamic list. I have SeekBar and a ToggleButton for each of the rows in List. I have been trying to generate events for the toggle and seek from the last 1 week but in vain. Please help me with it.
Here is the XML code with the List(smartunits.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/unitsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grid_gradient"
android:orientation="vertical" >
<ListView
android:id="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Now the code for each row(listunits.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" >
<TextView
android:id="#+id/unitName"
android:layout_width="40px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20px"
android:textColor="#ffffff" />
<TextView
android:id="#+id/unitCodeId"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#ffffff"
android:visibility="invisible" />
<SeekBar
android:id="#+id/unitsseek"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:background="#drawable/off_grey"
android:textOff=""
android:textOn="" />
</LinearLayout>
Here is the code for ListActivity
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleCursorAdapter;
import android.widget.ToggleButton;
public class UnitsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// put the bottom code here
setContentView(R.layout.smartunits);
Bundle extras = getIntent().getExtras();
final AutomationDBAccessor db = new AutomationDBAccessor(this);
Cursor cUnits = db.getUnits(extras.getInt("roomFloorId"), extras.getInt("roomId"));
String[] from = new String[] { AutomationDBAccessor.colUnitName, AutomationDBAccessor.colUnitCodeID };
int[] to = new int[] { R.id.unitName, R.id.unitCodeId };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.listunits, cUnits, from, to);
this.setListAdapter(sca);
View viewUnits = LayoutInflater.from(getBaseContext()).inflate(R.layout.listunits, null);
final ToggleButton toggle = (ToggleButton) viewUnits.findViewById(R.id.toggle);
toggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // TODO Auto-generated method stub
if (toggle.isChecked()) {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
} else {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
}
}
});
}
}
Now I am unable to handle the ToggleButton events.
I never try this, But you can do something like,
In your listunits.xml
<ToggleButton
android:id="#+id/toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:textOff=""
android:textOn=""
android:onClick="toggleButtonClick" />
And in your UnitsListActivity
write this method..
public void toggleButtonClick(View view)
{
ToggleButton toggle = (ToggleButton) view;
if (toggle.isChecked()) {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
} else {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
}
}
Run this code and let me know what happen..

Populating Custom List View with data from multiple Edit Texts

So I made a form with 3 EditTexts and two Buttons, "Ok" and "Cancel"
Here's my Code for it:
package com.examples.edtTxtCustomList;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class InputPage extends Activity implements OnClickListener{
private Button btnGo,btnCancel;
EditText et_name,et_email,et_phone;
List<String> data = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
et_name = (EditText) findViewById(R.id.et_name);
et_email =(EditText)findViewById(R.id.et_email);
et_phone = (EditText)findViewById(R.id.et_phno);
btnGo = (Button) findViewById(R.id.btn_ok);
btnGo.setOnClickListener(this);
data.add("Welcome");
}
#Override
public void onClick(View src) {
String nm=et_name.getText().toString();
String no = et_email.getText().toString();
String mail= et_phone.getText().toString();
Intent i = new Intent(this,CustomLayout.class);
i.putExtra("name", nm);
i.putExtra("email", mail);
i.putExtra("phone", no);
startActivity(i);
}
} `
And here's the XML file of my form:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:text="Name :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_name"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_name"
android:layout_alignTop="#id/tv_name"
android:layout_marginBottom="10dip"
android:layout_marginLeft="8dip"
android:textSize="15dip"/>
<TextView
android:id="#+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:layout_below="#id/tv_name"
android:text="E-Mail :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_email"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_email"
android:layout_alignTop="#id/tv_email"
android:layout_below="#id/et_name"
android:inputType="textEmailAddress"
android:layout_marginLeft="6dip"
android:layout_marginBottom="10dip"
android:textSize="15dip"/>
<TextView
android:id="#+id/tv_phno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:layout_below="#id/tv_email"
android:text="Phone :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_phno"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_phno"
android:layout_alignTop="#id/tv_phno"
android:layout_below="#id/et_email"
android:inputType="phone"
android:layout_marginLeft="5dip"
android:layout_marginBottom="10dip"
android:textSize="15dip"/>
<Button
android:id="#+id/btn_ok"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/et_phno"
android:layout_marginTop="30dip"
android:layout_marginLeft="50dip"
android:text="OK"
android:textSize="20dip"/>
<Button
android:id="#+id/btn_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/et_phno"
android:layout_marginTop="30dip"
android:layout_marginRight="50dip"
android:text="Cancel"
android:textSize="20dip"/>
</RelativeLayout>
</LinearLayout>
now what I want is,when i click on "OK" all the data from all three of the edit texts should be displayed in the customlistview.
I tried to make a dynamic listview as follows:
package com.example.customlist;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class CustomListLayoutActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.custom_row_layout,
new String[] {"Name","Email","Phone"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("Name","Anurag Kulkarni");
temp.put("Email", "kulkarni_anurag#yahoo.com");
temp.put("Phone", "+91-9904475805");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("Name","Rahul Shah");
temp1.put("Email", "rahul201290#gmail.com");
temp1.put("Phone", "+91-9898434909");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("Name","Pratik Thakkar");
temp2.put("Email", "iamcool#cooldood.com");
temp2.put("Phone", "+91-8539524925");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("Name","Utsav Patel");
temp3.put("Email", "patel_in_a_hotel#dhokla.com");
temp3.put("Phone","+91-9843500999");
list.add(temp3);
HashMap<String,String> temp4 = new HashMap<String,String>();
temp4.put("Name","Karan Mohan");
temp4.put("Email", "masala_dosa#sankalp.co.in");
temp4.put("Phone", "+91-9944843974");
list.add(temp4);
}
}
Can you please help me with it? I am still learning the basics,so any help would be appriciated.
Thanks.
As in another post of mine...
Whenever you want to do processing with the views in a ListView you
need to create a custom adapter that will handle your logic
implementation and pass that information to the views as necessary.
A custom adater would inflate the views piece by piece, this can be
dynamic of fixed.
Example:
Link 1

Categories

Resources