I'm developing an Customer details for a shop using Grid view in android. I get the details of my Customers using my Database. I need to update the Stocks handled by the Customer using an alert box for entering the Stock Quantity while performing the Transaction..
customergrid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="357dp"
android:numColumns="1"
android:stretchMode="columnWidth" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel" />
</LinearLayout>
And, for each Row I have used a customized Xml file..
customerrow.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/row_ID"
android:padding="5px"
android:layout_weight="1" />
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/key_ID"
android:padding="5px"
android:layout_weight="1" />
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/key_Description"
android:padding="5px"
android:layout_weight="1" />
</TableRow>
</TableLayout>
And I have Used customergrid.xml on the Oncreate() method and use customerrow.xml in the Grid creation like as the following code..
public void FillGrid() {
DatabaseHelper sqdb = new DatabaseHelper(this);
sqdb.openDataBase();
Cursor cursor;
GridView grid = (GridView) findViewById(R.id.grvData);
cursor = sqdb.getGridData();
sqdb.close();
if (cursor != null) {
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.customerrow, cursor, new String[] { CustomerActivity.KEY_ROW_ID,
CustomerActivity.KEY_ID, CustomerActivity.KEY_DESCRIPTION }, new int[] {
R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription } );
adapter.setViewResource(R.layout.gridlayout);
grid.setAdapter(adapter);
}
And finally I used alert box with an editbox for getting the input of Customer's Stock Quantity. I use this for that task..
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
AlertDialog.Builder b=new AlertDialog.Builder(MarkSheet.this);
b.setTitle("Employee Details");
LayoutInflater li=LayoutInflater.from(MarkSheet.this);
View v=li.inflate(R.layout.dialog, null);
b.setIcon(android.R.drawable.ic_input_get);
b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtName);
final EditText Quantity=(EditText)v.findViewById(R.id.Quantity);
// Here I need to update the table while clicking the positive button, if I click negative button I need to cancel the Dialog box..
}
});
Anybody help me for Complete this task..
you can find the code for updating the database table here and the concept of alert box here
You need to call the update method of the sqlite from the code where you clicked the button "ok".
Related
I have get the data from local DB and display it in a ListView and I had a custom row with button
main.xml will contain ListView only
custom-row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
style="#color/background_gradient_start"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.6"
android:background="#color/background_gradient_start"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="177dp"
android:layout_height="75dp"
android:layout_weight="5" >
<TextView
android:id="#+id/textViewFlightNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textViewCodes"
android:layout_alignBottom="#+id/textViewCodes"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/textViewCodes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="18dp"
android:background="#color/transparent"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="142dp"
android:layout_height="match_parent"
android:layout_weight="5">
<Button
android:id="#+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:alpha="1"
android:background="#3b3974"
android:focusable="false"
android:focusableInTouchMode="false"
android:minWidth="90dp"
android:text="Share"
android:textColor="#f1ecef" />
</RelativeLayout>
</LinearLayout>
Activity
so under on Create
String [] fromFiledName =new String[]{db.KEY_CODE};
int[] toViewIDs=new int[]{R.id.textViewCodes};
SimpleCursorAdapter myCurAdapter=new SimpleCursorAdapter(
this, //context
R.layout.listview_row_codes,
cursor,
fromFiledName,
toViewIDs
);
// set addapter to list view
ListView mylistPRN=(ListView) findViewById(R.id.codeslist);
mylistPRN.setAdapter(myCurAdapter);
mylistPRN.setAdapter(myCurAdapter);
mylistPRN.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Intent intent2 = new Intent(this, PromoCodeActivity2.class);
Intent sendIntent = new Intent();
//intent.putExtra(PromoCodeActivity.requestString, httpRequestString);
//getItemAtPosition(position);
Cursor c = (Cursor) parent.getAdapter().getItem(position);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "share code "+c.getString(c.getColumnIndex("code")));
startActivity(Intent.createChooser(share, "Share Text"));
//startActivity(intentD);
}
});
The code is working and when I click on the list view item it open for sharing but I need it to open when the button is clicked as currently I tried to link the button but I couldn't. Any help
here is what you can do add following two lines to your buttons xml
android:focusable="false"
android:focusableInTouchMode="false"
Then inside adapter getView method attach onclickListener to this button and you are done. Currently there is no listener attached to button in row.
you don't need to use onItemClickListner as you are not clicking on the list Items, rather you have to set the onClickListner on buttons in your getview method
something like this this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
Button yourBtn = (Button)rowView.findViewById(R.id.your_btn_id);
yourBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Do your work here
}
});
return rowView;
}
Also set following properties on your button
android:focusable="false"
android:focusableInTouchMode="false"
I am trying to fire a event when a user click the delete icon in the list , But I could not able to do it.I am using a simple cursor adapter with setviewbinder to display items in the list.In the setviewBinder method I set the textview layout clicklistener to be null that's why, view is not clicked when a user click in the textview.When a user is swipe in the list immediately delete icon is appeared.I need to fire a click event in the deleteIcon.Currently, when a user is clicked the delete icon, the whole view is clickable.I need to achieve when a user is clicked the delete icon, need to fired the event, only in the delete icon.
public boolean setViewValue(final View view, final Cursor data, int columnIndex) {
LinearLayout layoutTaskView=(LinearLayout)view.findViewById(R.id.layoutTaskView),
layoutTaskDelete = (LinearLayout) view.findViewById(R.id.LayoutDeleteTask);
boolean crossed = Boolean.valueOf(data.getString(columnIndex));
if(crossed){
icon.setImageState(new int[] { android.R.attr.state_checked }, true);
text1.setPaintFlags(text1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
text2.setPaintFlags(text2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
layoutTaskView.setOnClickListener(null);
}
}
Layout Xml File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/layoutTaskView"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="15dp" >
<TextView
android:id="#+id/taskTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/taskName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:paddingLeft="10dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/LayoutDeleteTask"
android:clickable="false">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:padding="15dp"
android:src="#drawable/checkmark" />
</LinearLayout>
</LinearLayout>
ListView xml
<com.myexample.CrossView
android:id="#+id/crossview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</com.myexample.CrossView>
Adapter Code
String[] from = new String []
{
DatabaseHelper.COLUMN_CATEGORY,DatabaseHelper.COLUMN_TASKNAME,
DatabaseHelper.COLUMN_CROSSED};
int[] to = new int[] {R.id.taskTime,R.id.taskName, android.R.id.content};
adapter = new SimpleCursorAdapter(context, R.layout.layout_list_item, data, from, to, 0);
adapter.setViewBinder(new CrossBinder());
lv.setAdapter(adapter);
public void onItemClick(AdapterView<?> parent, View v, final int position, final long id) {
ImageView icon = (ImageView)v.findViewById(android.R.id.icon);
icon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == android.R.id.icon){
}
}
}
You need to use android:descendantFocusability = "afterDescendants" parameter to root layout of list item. In your case, the root LinearLayout.
For more information check this link.
EDIT : remove android:clickable="false" from your LinearLayout containing ImageView. Put android:clickable="true" in ImageView. Moreover, you can use ImageButton instead ImageView.
P.S. I have suggested to use android:descendantFocusability in main parent LinearLayout.
Create custom adapter like that answer Custom adapter for a list of items that have multiple child items?
This answer give tutorial link to create custom dapters for listview.
I am having a issue.
listview position 1
button1
listview position 2
button2
This is my designof listview . My adapter is as follow
adapter = new SimpleCursorAdapter(this, R.layout.appareal_listview_text ,cursor, new String[]{"Key_ProductName","Key_SellingPrice"} , new int[] {R.id.title_info_txt_v,R.id.description_info_txt_v});
Now i am giving my xml file appareal_listview_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#drawable/layout1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title_info_txt_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="3dip"
android:layout_marginRight="3dip"
android:textColor="#000000"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="#+id/description_info_txt_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginLeft="3dip"
android:layout_marginRight="3dip"
android:layout_marginTop="4dip"
android:textColor="#000000"
android:textSize="12dp"
android:textStyle="bold" />
<Button
android:id="#+id/btnBuyNow_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="15dp"
android:background="#drawable/buynow"
android:clickable="true"
android:onClick="buynowClick" />
</LinearLayout>
Now my porblem is that define my button in xml which is call by adapter and button is define only i just once in xml , and i want to perform different task on each click of each position of listview.For that i used
#Override
public void onListItemClick(ListView parent, View view, int position,
long id) {
Intent intent = new Intent(this, asasdad.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
System.out.println("list is click");
}
and as well as i can not use this
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
}
});
So how i will recognize which list button position is clicked to perform different task.
You should use following code.
button.setTag("position");
When button clicked,
String strPos = view.getTag();
Here you will get position of button.
I want to display data, from a database, in a gridview.
Means I have a data in my table (let it's a customer detail) and I want to show it in gridview (or any other control to display details) same as we done in asp.net.
Solution:--
public void FillGrid() {
DatabaseHelper dbh = new DatabaseHelper(this);
dbh.openDataBase();
Cursor cursor;
GridView grv = (GridView) findViewById(R.id.grvData);
cursor = dbh.getGridData();
dbh.close();
if (cursor != null) {
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.gridlayout, cursor,
new String[] { GridTestActivity.KEY_ROW_ID, GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION }
,new int[] { R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription } );
adapter.setViewResource(R.layout.gridlayout);
grv.setAdapter(adapter);
}
}
Here's the full example.
Also if your beginning Android this would be a good book for you.
Check this post for gridview: http://developer.android.com/resources/tutorials/views/hello-gridview.html
and this for cursor adapter: http://developer.android.com/reference/android/widget/CursorAdapter.html
Hope this helps!
You can Set the GridView With 3 Columns. It will give you a look of Table form.
OR
You can use customized GridView for your application Using the Following type of Codes..
customergrid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="357dp"
android:numColumns="1"
android:stretchMode="columnWidth" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel" />
</LinearLayout>
And, for each Row use a customized Xml file..
customerrow.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/row_ID"
android:padding="5px"
android:layout_weight="1" />
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/key_ID"
android:padding="5px"
android:layout_weight="1" />
<TextView
android:layout_width="50px"
android:layout_height="wrap_content"
android:id="#+id/key_Description"
android:padding="5px"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Use customergrid.xml on the Oncreate() method and use customerrow.xml in the Grid creation like as your code..
public void FillGrid() {
DatabaseHelper dbh = new DatabaseHelper(this);
dbh.openDataBase();
Cursor cursor;
GridView grv = (GridView) findViewById(R.id.grvData);
cursor = dbh.getGridData();
dbh.close();
if (cursor != null) {
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.customerrow, cursor, new String[] { GridTestActivity.KEY_ROW_ID,
GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION }, new int[] {
R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription } );
adapter.setViewResource(R.layout.gridlayout);
grv.setAdapter(adapter);
}
}
You can get the data from the Database on the gridview now.
This is my list layout:
<?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">
<LinearLayout android:layout_width="fill_parent" android:id="#+id/linearLayout1"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout2"
android:layout_height="fill_parent">
<CheckBox android:id="#+id/checkBox1" android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:id="#+id/linearLayout3" android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/row1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="#+id/row2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Although I have an excess of Layouts, all this is is a checkbox and a couple of TextViews. I want each listItem to be clickable(as in go to a different intent), but when I click a button (DELETE BUTTON)in my activity, I want to be able to show the checkboxes and select the items to delete and delete it. Sample code appreciated.
Java code:
adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
from, to);
listthings.setAdapter(adapter);
listthings.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(this, Item1.class);
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime", timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("painLocation", painLocation);
startActivity(intent);
}
Delete checked items
private void deleteCheckedItems() {
int count = this.mainListView.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.mainListView.isItemChecked(i)) {
painItems.remove(i)
}
}
}
More info: http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)
and then notify the adapter to update:
adapter.notifyDataSetChanged();
public void notifyDataSetChanged ()
Since: API Level 1 Notifies the attached View that the underlying data
has been changed and it should refresh itself.