I want to fix a button on top of the layout and just below the button a dynamic edit text is created. I am creating my own Adapter. On button click another activity starts and returns string which is to be shown on the list view items below the button. How can I do this ?
My XML Layout -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/etList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</LinearLayout>
My list view adapter -
package com.example.tasktable;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class TaskAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
Button click;
TextView textView;
public TaskAdapter(Context context, String[] values) {
super(context, R.layout.list_view, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflator.inflate(R.layout.list_view, parent, false);
click = (Button) rowView.findViewById(R.id.button1);
textView = (TextView) rowView.findViewById(R.id.etList);
textView.setText(values[position]);
return rowView;
}
}
I don't want to inflate button with every edit text.
You can position layout elements with Relative Layouts https://developer.android.com/reference/android/widget/RelativeLayout.html
//try this way, hope this will help you...
**XML** code
**activity.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="#+id/lnrEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
**list_activity.xml**
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
**list_item.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="#+id/txtValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"/>
</LinearLayout>
**ACTIVITY** code
**MyActivity**
public class MyActivity extends Activity{
private Button btnAdd;
private LinearLayout lnrEdit;
private final int GET_LIST_ITEM = 1;
private HashMap<String,String> listItemMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
btnAdd = (Button) findViewById(R.id.btnAdd);
lnrEdit = (LinearLayout) findViewById(R.id.lnrEdit);
listItemMap = new HashMap<String, String>();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this,MyListActivty.class);
startActivityForResult(intent,GET_LIST_ITEM);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == GET_LIST_ITEM);
{
String getListValue = data.getStringExtra("value");
int index = data.getIntExtra("index",0);
if(listItemMap.containsKey(String.valueOf(index))){
Toast.makeText(this,"This item already taken !!!",Toast.LENGTH_LONG).show();
} else{
listItemMap.put(String.valueOf(index),getListValue);
EditText editText = new EditText(this);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setId(index);
editText.setText(getListValue);
editText.setTag(getListValue);
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = v.getTag().toString();
Toast.makeText(MyActivity.this,value,Toast.LENGTH_SHORT).show();
}
});
lnrEdit.addView(editText);
}
}
}
}
}
**MyListActivity**
public class MyListActivty extends ListActivity{
private String[] listArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity);
listArray = new String[]{"Android4.1","Android4.2","Android4.3","Android4.4","Android4.5"};
setListAdapter(new TaskAdapter(this,listArray));
}
class TaskAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public TaskAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
holder.button = (Button) convertView.findViewById(R.id.btnSelect);
holder.textView = (TextView) convertView.findViewById(R.id.txtValue);
convertView.setTag(holder);
} else{
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(values[position]);
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("index",position);
intent.putExtra("value",values[position]);
setResult(RESULT_OK,intent);
finish();
}
});
return convertView;
}
#Override
public int getCount() {
return values.length;
}
}
static class ViewHolder{
Button button;
TextView textView;
}
}
Related
Check this image sample i have done it Material Design Gridview Image
I have done the same Material Design for grid view Activity on my app, i like to add the onItemClick to new Activity,
I don't know how to do this , please give the brief explanation for the solution
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:id="#+id/appbar_layout"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_android_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="#dimen/expanded_toolbar_title_margin_start"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/code"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/nestedscrollview"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
res/layout/gridview_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_gridview_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<com.andexert.library.RippleView
android:id="#+id/more"
rv_centered="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:rv_color="#fff"
app:rv_rippleDuration="200">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="vertical">
<ImageView
android:id="#+id/gridview_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/gridview_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/grid_image"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Grid View Item"
android:textColor="#444"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</com.andexert.library.RippleView>
</LinearLayout>
src/MainActivity.java
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"Android",
"Java",
"GridView",
"ListView",
"Adapter",
"Custom GridView",
"Material",
"XML",
"Code",
};
public static int[] gridViewImages = {
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("Material Grid");
}
}
src/CustomAndroidGridViewAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by HP on 5/11/2016.
*/
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
PLease give me the solution for call another Actvivty when mouse click on Image (Image 1, Image 2, etc) in Gridview this
Replace your method with mine
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,YourActivity.class);
context.startActivity(intent);
}
});
} else {
grid = (View) convertView;
}
return grid;
}
here is the code to go another activity after click on every image.
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"Android",
"Java",
"GridView",
"ListView",
"Adapter",
"Custom GridView",
"Material",
"XML",
"Code",
};
public static int[] gridViewImages = {
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic,
R.drawable.android_ic
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
if(gridViewStrings[position].equalsIgnoreCase(String.valueOf(1))){
Intent intent=new Intent(MainActivity.this,SecoundActivity.class);
intent.putExtra("image",gridViewImages[position]);
startActivity(intent);
}
else if(gridViewStrings[position].equalsIgnoreCase(String.valueOf(2))){
Intent intent2=new Intent(MainActivity.this,ThirdActvity.class);
intent2.putExtra("image2",gridViewImages[position]);
startActivity(intent2);
}
//Like this create activites how many images are there in gridViewImages[]array.you have 9 images so create nine activities..you have to send the url in every activity from one activity to another activity according to your requirement
}
});
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("Material Grid");
}
}
create SecoundActivity.class
public class SecoundActivity extends AppCompactActivity{
private ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
int position = intent.getExtras().getInt("image");
setContentView(R.layout.secound_view);
image=(ImageView) findViewById(R.id.image);
image.setImageResource(position);
}
}
then Finally create the secound_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_horizontal_margin">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/image"
/>
</LinearLayout>
After 10 seconds in Google...
Starting Another Activity
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
And Click Listener/Event
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
In your MainActivity onCreate method, add an OnItemClick listener for the GridView:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Your Intent goes here
}
});
I am just starting with android, so I don't have much to show of what I have so far...
I am trying to make a to-do list. I have seen tutorials on how to do this with a menu for input and have the tasks displayed in a ListView, however I would like the user to input the task into an EditText instead. I was using this (which doesn't use a ListView) to just have a button click make a new TextView underneath the EditText, however I couldn't add buttons next to the TextView which would remove it (when the user finishes a task).
Using a ListView seems much easier for this, but how can I have the EditText input go to a ListView with all the tasks and buttons to remove them (permanently, not just set visibility to 0)? I would also like the tasks to be stored, so the user can close the app and return with the tasks still there.
Any ideas? I haven't made much progress.
I recommend you to use RecyclerView instead of Listview. compile this in your Gradle
compile 'com.levelupstudio:expandable-recyclerview:1.0.1'
Here is the mainActivity.java
public class MainActivity extends ActionBarActivity implements
RecyclerViewAdapter.OnItemClickListener{
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
EditText nameField;
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
/*
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
*/
myRecyclerViewAdapter = new RecyclerViewAdapter(this);
myRecyclerViewAdapter.setOnItemClickListener(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
nameField = (EditText)findViewById(R.id.namefield);
btnAdd = (Button)findViewById(R.id.addbutton);
btnAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String newName = nameField.getText().toString();
Context context= getApplicationContext();
myRecyclerViewAdapter.add(0,newName);
Toast.makeText(context,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
Toast.makeText(this,
"Remove " + position + " : " + item.getItemName(),
Toast.LENGTH_SHORT).show();
}}
And AdapterClass for Recycler
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
}
#Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.list_item, parent, false);
return new ItemHolder(itemView, this);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.setItemName(itemsName.get(position));
}
#Override
public int getItemCount() {
return itemsName.size();
}
public void setOnItemClickListener(OnItemClickListener listener){
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener(){
return onItemClickListener;
}
public interface OnItemClickListener{
public void onItemClick(ItemHolder item, int position);
}
public void add(int location, String iName){
itemsName.add(location, iName);
notifyItemInserted(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RecyclerViewAdapter parent;
TextView textItemName;
public ItemHolder(View itemView, RecyclerViewAdapter parent) {
super(itemView);
itemView.setOnClickListener(this);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.item_name);
}
public void setItemName(CharSequence name){
textItemName.setText(name);
}
public CharSequence getItemName(){
return textItemName.getText();
}
#Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if(listener != null){
listener.onItemClick(this, getPosition());
}
}
}}
and your xml file
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/namefield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/addbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/myrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
list_xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:id="#+id/item_name"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I'm looking for a way to implement a dialog which asks for confirmation when clicking on the delete button of my ListView row. I tried to do it inside my custom ArrayAdapter, but as it is no Activity I don't know how to do it.
When I put the whole onClick-Listener inside the MainActivity, I have no clou how to find out which position the button was clicked so that I can remove it afterwards.
public class ServiceAdapter extends ArrayAdapter<Service> {
private final Singleton singleton = Singleton.getInstance();
private ArrayList<Service> services;
public ServiceAdapter(Context context, ArrayList<Service> services) {
super(context, 0, services);
this.services = services;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Service service = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.listview_row, parent, false);
}
// Lookup view for data population
TextView quantity = (TextView) convertView
.findViewById(R.id.QUANTITY_CELL);
TextView description = (TextView) convertView
.findViewById(R.id.DESCRIPTION_CELL);
Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);
// Populate the data into the template view using the data object
quantity.setText(String.valueOf(service.getQuantity()));
description.setText(service.getDescription());
// Set up the listener for the delete button.
final View view = convertView;
view.setTag(Integer.valueOf(position));
delete.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Integer index = (Integer) view.getTag();
services.remove(index.intValue());
notifyDataSetChanged();
}
});
// Return the completed view to render on screen
return convertView;
}
}
public class MainActivity extends Activity {
private ListView serviceList;
private ArrayList<Service> services;
private ServiceAdapter adapter;
private final Singleton singleton = Singleton.getInstance();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceList = (ListView) findViewById(R.id.service_list);
adapter = new ServiceAdapter(this, services);
serviceList.setAdapter(adapter);
serviceList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
Service temp = services.get(position);
singleton.setQuantity(temp.getQuantity());
singleton.setDescription(temp.getDescription());
setPosition(position);
openDetailedEntry();
}
});
}
public void openDetailedEntry() {
Intent i = new Intent(this, DetailedEntryActivity.class);
// Check if the meant Activity is actually resolvable
if (i.resolveActivity(getPackageManager()) != null)
startActivity(i);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<TableLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:paddingTop="8dp"
>
<ListView
android:id="#+id/service_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp" />
</TableLayout>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="4dip"
android:paddingBottom="4dip"
android:paddingLeft="4dip"
android:paddingRight="4dip"
android:orientation="horizontal">
<TextView android:id="#+id/QUANTITY_CELL"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="20sp"
/>
<TextView android:id="#+id/DESCRIPTION_CELL"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_weight="6" />
<Button
android:id="#+id/BUTTON_DELETE"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:textSize="12sp"
android:focusable="false"
android:text="#string/delete" />
</LinearLayout>
Let me know if you need something.
Construct AlertDialog in ServiceAdapter Like this,
private AlertDialog mDialog;
private int mListRowPosition;
public ServiceAdapter(Context context, ArrayList<Service> services) {
super(context, 0, services);
this.services = services;
//Create AlertDialog here
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Your Message")
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Use mListRowPosition for clicked list row...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object
mDialog = builder.create();
}
Create method in ServiceAdapter Like,
private void showDialog(int position)
{
mListRowPosition = position;
if(mDialog != null)
mDialog.show();
}
Now in onClick() Just call
showDialog(position); // But make position of getView() as final...
I want to display ArrayList of object in AlertDialog but my list view dont show. Here is my code.
public class PrzystanekDialog extends DialogFragment {
private ListView trasaPrzejazdu;
private ArrayList<Linia> trasaPrzejazduList;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
LayoutInflater inflater = getActivity().getLayoutInflater();
final View rootView = inflater.inflate(R.layout.dialog_layout, null);
//ustawienie lisview z wygenerowaną linia
trasaPrzejazduList = getArguments().getParcelableArrayList("linia");
trasaPrzejazdu = (ListView) rootView.findViewById(R.id.trasaListView);
final ArrayAdapter<Linia> adapter = new ArrayAdapter<Linia>(getActivity(),R.layout.listbox_dialog_element, trasaPrzejazduList);
trasaPrzejazdu.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final ViewPager mViewPager;
mViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
builder.setView(inflater.inflate(R.layout.dialog_layout, null));
//getActivity().setContentView(inflater.inflate(R.layout.dialog_layout, null));
builder.setMessage(R.string.choose)
.setPositiveButton(R.string.start_trip, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mViewPager.setCurrentItem(1);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getActivity().getApplicationContext(), "Anulowano podróż.", Toast.LENGTH_LONG).show();
}
});
return builder.create();
}
}
Here is my ArrayAdapter
public class DialogListAdapter extends BaseAdapter {
private List<Linia> listData;
private LayoutInflater layoutInflater;
public DialogListAdapter(Context context, List<Linia> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Linia getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listbox_dialog_element, null);
holder = new ViewHolder();
holder.start = (TextView) convertView.findViewById(R.id.przystanekStartowy);
holder.end = (TextView) convertView.findViewById(R.id.przystanekDocelowy);
holder.vehicleImage = (ImageView) convertView.findViewById(R.id.line_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//listData.get(position).getHeadline()
if(listData.get(position).getTypLini().toString() == "bus")
holder.vehicleImage.setImageResource(R.drawable.autobus_ico);
else
holder.vehicleImage.setImageResource(R.drawable.tram_ico);
holder.start.setText(listData.get(position).getStopPrzystanki().get(0).getNazwa_przystanku());
holder.end.setText(listData.get(position).getStopPrzystanki().get(listData.get(position).getStopPrzystanki().size()-1).getNazwa_przystanku());
return convertView;
}
static class ViewHolder {
ImageView vehicleImage;
TextView start;
TextView end;
}
}
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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/trasaListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I have no compile errors. Thank you very much for help.
EDIT:
<?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:orientation="vertical" >
<ImageView
android:id="#+id/line_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/autobus_ico"
android:layout_marginLeft="5dip"/>
<TextView
android:id="#+id/przystanekStartowy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:typeface="sans"
android:layout_marginLeft="60dip"/>
<TextView
android:id="#+id/przystanekDocelowy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text=""
android:textColor="#343434"
android:textSize="12sp"
android:layout_marginLeft="60dip"/>
</LinearLayout>
how to select row item using Tick mark like iphone in android?iam using imageview in list_row.xml.when i click the list row item then i show image in row imageview.
if(getItem(position)!=null){
img.setvisibilty(View.Visible);}
else{System.out.println("imagenull");}
iam using this but image display in last row only.please help me how to select item using tickmark image.
public class DistanceArrayAdapter extends ArrayAdapter<Constant>{
public static String category,state,miles;
public ImageView img;
private Context context;
private int current = -1;
ArrayList<Constant> dataObject;
public DistanceArrayAdapter(Context context, int textViewResourceId,
ArrayList<Constant> dataObject) {
super(context, textViewResourceId, dataObject);
this.context=context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView=convertView;
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.category_row, parent, false);
}
//TextView textView = (TextView) rowView.findViewById(R.id.text1);
TextView textView1 = (TextView) rowView.findViewById(R.id.text2);
//textView.setText(""+getItem(position).id);
textView1.setText(""+getItem(position).caption);
img=(ImageView)rowView.findViewById(R.id.img);
img.setVisibility(View.GONE);
if(position%2==1)
{
rowView.setBackgroundResource(R.color.even_list);
}
else
{
rowView.setBackgroundResource(R.color.odd_list);
}
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(img.getVisibility()==View.GONE)
{
img.setVisibility(View.VISIBLE);
System.out.println("1");
}
if(img.getVisibility()==View.VISIBLE){
img.setVisibility(View.GONE);
System.out.println("12");
}
miles=getItem(position).caption;
System.out.println("miles"+miles);
}
});
return rowView;
}
}
Drawing from https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
String[] GENRES = new String[] {"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"};
private CheckBoxAdapter mCheckBoxAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.lv);
listView.setItemsCanFocus(false);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
listView.setAdapter(mCheckBoxAdapter);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder result = new StringBuilder();
for (int i = 0; i < GENRES.length; i++) {
if (mCheckBoxAdapter.mCheckStates.get(i) == true) {
result.append(GENRES[i]);
result.append("\n");
}
}
Toast.makeText(MainActivity.this, result, 1000).show();
}
});
}
public void onItemClick(AdapterView parent, View view, int position, long id) {
mCheckBoxAdapter.toggle(position);
}
class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener {
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;
String[] gen;
private SparseBooleanArray mCheckStates;
private SparseBooleanArray mCheckStates;
CheckBoxAdapter(MainActivity context, String[] genres) {
super(context, 0, genres);
mCheckStates = new SparseBooleanArray(genres.length);
mInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gen = genres;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return gen.length;
}
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
And the XML file for the checkboxes:
<?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:layout_marginLeft="15dp"
android:layout_marginTop="34dp"
android:text="TextView" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:layout_marginRight="22dp"
android:layout_marginTop="23dp" />
</RelativeLayout>
When you click the button a toast message with list of item choosen is displayed. You can modify the above according to your requirements.
Set selection mode on your ListView
//if using ListActivity or ListFragment
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//or
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//myListView is reference to your ListView
1.Make visibility gone to you tick mark image
2.Implement view.setOnClickListener in arrayadapter.
3.In that check image.getVisibility()==View.GONE then make image.setVisibity(View.Visible)
4.if image.getVisiblity()==View.VISIBLE then make your image.setVisibity(View.GONE)
Try this.