listview item cannot be click in android - android

i have created Custom listview and i add a onclick listener to it but it was not working.the below is my code
This is my MainActivity Class:
public class MainActivity extends Activity {
public boolean[] status;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
// Attach the adapter to a ListView
listView = (ListView) findViewById(R.id.lvUsers);
populateUsersList();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
Toast.makeText(getBaseContext(),"Hello", Toast.LENGTH_SHORT).show();
}
});
}
private void populateUsersList() {
// Construct the data source
ArrayList<User> arrayOfUsers = User.getUsers();
// Create the adapter to convert the array to views
CustomUsersAdapter adapter = new CustomUsersAdapter(this, arrayOfUsers);
listView.setAdapter(adapter);
}
}
this is my adapter class:
public class CustomUsersAdapter extends ArrayAdapter<User> {
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ToggleButton tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
}
}
i want to get the value from listview when the appropriate toggle button is checked .and want to save the value of toggle button and the appropriate text in arraylist.
this my xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
This is my second 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:padding="10dp"
>
<TextView
android:id="#+id/tvName"
android:layout_alignBaseline="#+id/ivUserIcon"
android:layout_toRightOf="#+id/ivUserIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="6dp"
android:text="Sarah"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvHometown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvName"
android:layout_below="#+id/tvName"
android:text="San Marco"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/tvName"
android:layout_marginRight="15dp"
android:text="ToggleButton" />
</RelativeLayout>

Change your RelativeLayout to
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:descendantFocusability="blocksDescendants"
>
This view will block any of its descendants from getting focus, even if they are focusable.

You must set the button on clicklistener.
tgbtn.setOnClickListener(new OnClickListener(position));
Dont know it it works but try anything like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ToggleButton tgbtn = (ToggleButton) convertView.findViewById(R.id.toggleButton1);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
tgbtn.setOnClickListener(new OnButtonClickListener(position));
// Return the completed view to render on screen
return convertView;
}
then a class for the ButtonClick:
private class OnButtonClickListener implements OnClickListener {
int _postion;
// constructor
public OnButtonClickListener(int position) {
this._postion = position;
}
#Override
public void onClick(View v) {
}
}

below of your code that there is a ListView and you wanna able clicked function to the list item, write this code.
public void onListItemClick(ListView parent, View v, int position, long id){
// do something here
// Example below make a toast message
Toast.makeText(getApplicationContext(), "I clicked item of list number " + position,Toast.LENGTH_SHORT).show();
}

Related

How to create Listview items + Button in each row?

I am new to android programming and this task is really need for my school project. Please kindly help me.
I've string array List - (retrieved from csv)
list = new ArrayList<>(Arrays.asList("111,222,333,444,555,666".split(",")));
myList.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.cell,list));
The result is showing only line by line text of arrayList. I want to add button to each generated line by line to delete clicked row.
Please how can I do this. Thank you for understanding my problem.
You have to create a custom layout xml which having a single item then you will add your button to this layout along with any other items.
CustomLayout.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" >
<TextView
android:id="#+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />
</RelativeLayout>
Now after creating custom item layout you need listview which holds all items.
MainActivity.xml
.
.
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
.
.
Now in java file just set adapter with our custom layout xml
.
.
list = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));
listview.setAdapter(new MyCustomAdapter(list, context) );
.
.
Custom adapter Class
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.CustomLayout, null);
}
//Handle TextView and display string from your list
TextView tvContact= (TextView)view.findViewById(R.id.tvContact);
tvContact.setText(list.get(position));
//Handle buttons and add onClickListeners
Button callbtn= (Button)view.findViewById(R.id.btn);
callbtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
.
}
});
return view;
}
}
We have need ListviewActivity for listing your data
SchoolAdapter which is custom adapter to inflate each individual row
activity_listview which is layout for ListviewActivity
view_listview_row which is required for each individual row
Now create all file as below
For ListviewActivity,
public class ListviewActivity extends AppCompatActivity {
private ListView mListview;
private ArrayList<String> mArrData;
private SchoolAdapter mAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mListview = (ListView) findViewById(R.id.listSchool);
// Set some data to array list
mArrData = new ArrayList<String>(Arrays.asList("111,222,333,444,555,666".split(",")));
// Initialize adapter and set adapter to list view
mAdapter = new SchoolAdapter(ListviewActivity.this, mArrData);
mListview.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
For SchoolAdapter,
public class SchoolAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mArrSchoolData;
public SchoolAdapter(Context context, ArrayList arrSchoolData) {
super();
mContext = context;
mArrSchoolData = arrSchoolData;
}
public int getCount() {
// return the number of records
return mArrSchoolData.size();
}
// getView method is called for each item of ListView
public View getView(int position, View view, ViewGroup parent) {
// inflate the layout for each item of listView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.view_listview_row, parent, false);
// get the reference of textView and button
TextView txtSchoolTitle = (TextView) view.findViewById(R.id.txtSchoolTitle);
Button btnAction = (Button) view.findViewById(R.id.btnAction);
// Set the title and button name
txtSchoolTitle.setText(mArrSchoolData.get(position));
btnAction.setText("Action " + position);
// Click listener of button
btnAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Logic goes here
}
});
return view;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}}
For activity_listview,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1FFFF"
android:orientation="vertical">
<ListView
android:id="#+id/listSchool"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0000CC"
android:dividerHeight="0.1dp"></ListView>
For view_listview_row,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="7.5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="7.5dp">
<TextView
android:id="#+id/txtSchoolTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="20dp" />
<Button
android:id="#+id/btnAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Click Me" />
At last but not least, do not forgot to add your activity in manifest.xml
Create a custom list view in another file with the only content of each item in the list.
Then create a Custom Adapter extending BaseAdapter and bind it.
Please refer to this website for example.
https://looksok.wordpress.com/tag/listview-item-with-button/
OR
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Why ListView onClick does not work?

Trying to implement a onlick functionality for the android listview but something seems to not working. I tried to find some solutions over the internet and I found that there were recommendation to change the focusable property of imageview and textview elements. Tried it and it did not work. Appreciate if you guys can recommend any other alternatives.
Layout item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:id="#+id/rootItemId"
android:clickable="true"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable='false'
android:background="#drawable/frame">
<ImageView
android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="#string/hello_world"
android:padding="5dp"
android:focusable='false'
android:src="#drawable/ic_launcher"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:focusable='false'
android:layout_toRightOf="#id/ivIcon">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable='false'
android:textAppearance="#android:style/TextAppearance.Medium"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
list fragment
public class EventsListFragment extends ListFragment{
ArrayList<Event> eventsChildren;
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.e("RedditListingsClick",position + " " + id);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
eventsChildren = new ArrayList<Event>();
EventsChildAdapter adapter = new EventsChildAdapter(inflater.getContext(), eventsChildren);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
list adapter
public class EventsChildAdapter extends ArrayAdapter<Event> {
Context context;
RelativeLayout root;
private static class ViewHolder {
TextView title;
ImageView thumbnail;
}
public EventsChildAdapter(Context context, ArrayList<Event> eventsChildren) {
super(context, R.layout.item_listing, eventsChildren);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Event child = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_listing, null);
//root=(RelativeLayout)convertView.findViewById(R.id.rootItemId);
viewHolder.title = (TextView) convertView.findViewById(R.id.tvTitle);
//viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.ivIcon);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
System.out.println(child.getTitle());
viewHolder.title.setText(child.getTitle());
Picasso.with(this.context).load(child.getThumbnailImageURL()).resize(200, 100).into(viewHolder.thumbnail);
// Return the completed view to render on screen
return convertView;
}
}
In addition to the above, I have already tried adding android:descendantFocusability="blocksDescendants" to relativelayout but still did not work. Finally I tried to make the root relativelayout clickable and removed focus from the other elements and this did not work either.
You can try this:
public class EventsListFragment extends ListFragment implement OnItemClickListener {
...
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// the child callback here.
}
}
and this in onActivityCreated
setListAdapter(Your adapter);
getListView().setOnItemClickListener(this);

How to access listview items generated by ArrayAdapter by CLICKING ON BUTTON

I'm using a ListView with ArrayAdapter. There is a Button along with TextView in the listview.xml. I want to retrieve data from that TextView by Clicking over corresponding buttons.
My listview.xml is as follows:
<?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/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="90dp"
android:id="#+id/btConfirmfriend"
android:text="Confirm Friend"
/>
</RelativeLayout>
My PendingRequest Class is as follows:
public class PendingRequest extends AddFriend {
TextView tvPending;
Button btConfirmfriend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pendingrequest);
final String[] s= getIntent().getExtras().getStringArray("pending");
System.out.println(s[1]);
final ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listview,R.id.label,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
final View inflatedView = getLayoutInflater().inflate(R.layout.listview, null);
btConfirmfriend = (Button) inflatedView.findViewById(R.id.btConfirmfriend);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ServerConnection sc = new ServerConnection();
//here I want to retrieve the value from List
//System.out.println(s[adapter.]);
int status = sc.confirmRequest(s[listView.getSelectedItemPosition()]);
AlertDialog.Builder alert = new AlertDialog.Builder(PendingRequest.this);
if (status == 1) {
alert.setMessage("Friend has been Added");
alert.show();
} else {
alert.setMessage("Sorry for inconvinient!!Try Again");
alert.show();
}
}
});
}
}
My pendingrequest.xml file is:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#drawable/background"
android:layout_height="match_parent">
<Button
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#e9abab"
android:id="#+id/btSend"
android:text="Pending Requests"
/>
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
On clicking over the button nothing happening.onClick() method is not executing.Please help.
Since you need a click listener on a view inside your list item, you need to write your own adapter for that -
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] list) {
super(context, 0, list);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView label = (TextView) convertView.findViewById(R.id.label);
Button btConfirmfriend = (Button) convertView.findViewById(R.id.btConfirmfriend);
// Populate the data into the template view using the data object
label.setText(item);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getItem(position); //Get your selected text
}
});
return convertView;
}
}
And this is how you will set the adapter -
final CustomAdapter adapter = new CustomAdapter(this,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);

get position of selected button in listview

I have a list view every item in list contains textviews and button what I want is when I click on the button of any item in list I want to printout the position of button if i clicked the button in first line i want to print out 0 and go on but it doesn't work
this is my method to display view
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
PhonesDbAdapter.KEY_NAME,
PhonesDbAdapter.KEY_CONTINENT,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.continent,
R.id.name
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.phone_layout,
cursor,
columns,
to,
0);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
public void print(View v)
{
}
and here how my item look like
<?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:orientation="vertical"
android:padding="6dip"
android:background="#f0f0f0" >
<TextView
android:id="#+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#275a0d"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView"
android:textColor="#000"/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/call"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1"
android:background="#drawable/ic_launcher"
android:onClick="print"/>
</RelativeLayout>
and this the layout that contain the listview
<?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"
android:background="#f0f0f0">
<EditText android:id="#+id/myFilter" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10">
</EditText>
<ListView android:id="#+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
i spent alot of time with this problem and i hope that u can help me and im really sorry for my bad english
Are you implementing the method getView() on your adapter?
That's where you want to add the OnClickListener to your Button
Once you do that you can set the position as the tag of the Button and retrieve it on the onClick method.
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.your_item_layout, parent, false);
Button myButton = (Button) row.findViewById(R.id. call);
myButton.setOnClickListener(new OnClickListener {
#Override
public void onClick(View view) {
int position = (int)view.getTag();
//Do whatever you want with the position here
}
});
myButton.setTag(position);
return row;
}
From last 3 three years I am not working on android so I am not sure my suggestion is correct or not. You need to test it for you purpose.
In your above case you cannot use onItemClickListner because it can only be used when you are tracking your complete list row. What I think is you can create a custom adapter and override its getView method. In getView method you will have your position and you can set button click listner for you button in getview. So In this way you can get your button position.
You try it once if you need some code then I can try to write it for you...
THIS IS HOW YOU CAN CREATE CUSTOM ADAPTER.
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
ArrayList<String> elements = null;
public my_custom_adapter(Context context, int type, ArrayList<String> elements)
{
super(context, type, elements);
this.elements = elements;
this.context = context;
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public Button cb;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (Button) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// IF YOUR BUTTON TAG HERE AND YOU CAN HAVE POSITION USING "position" PARAMETER
}
});
}
return rowView;
}
}

Android list view show hidden edit text when an item is clicked

I want to hide the edit text and button field initially in list view and show that edit text and button only for a particular row (clicked row) in list view when that row is clicked.
I used following custom layout to create the list view
<?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" >
<ImageView
android:id="#+id/emp_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="#drawable/person" />
<TextView
android:id="#+id/emp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/emp_avatar"
android:layout_toRightOf="#+id/emp_avatar"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/empPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/emp_avatar"
android:layout_alignRight="#+id/emp_number"
android:layout_toRightOf="#+id/emp_avatar"
android:ems="10"
android:inputType="number"
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="invisible">
<requestFocus />
</EditText>
In this lay out I have used android:visibility="invisible"
Here is the code of Activity.java
public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();
ListView list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list = (ListView) findViewById(R.id.employeeListView);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
et.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
});
private void populateEmployeeList() {
...
}
private class MylistAdapter extends ArrayAdapter<Employee>{
public MylistAdapter(){
super(MainPortal.this,R.layout.item_view,employees);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
Employee currentEmployee = employees.get(position);
//Avatar
ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
imageView.setImageResource(currentEmployee.getAvatarId());
//Employee number
TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
emp_id.setText(currentEmployee.getId());
et = (EditText) itemView.findViewById(R.id.empPin);
return itemView;
}
}
}
I used following codes withing the on click listener of list view to make edit text visible for item click.
editText.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
But after clicking a row edit text field does not appears. What I'm doing wrong here ? does any body have an idea how to do this ?
Initialization of EditText:
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
}
et = (EditText) itemView.findViewById(R.id.editText1);
...
Thanks in advance!
You have EdiText in item_view.xml not in activity_main_portal. You are inflating a custom layout in getView
You need to initialize edittext
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View v = (View)view.getParent();
EditText ed1 = (EditText) v.findViewById(R.id.empPin);
ed1.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
}
});

Categories

Resources