I have implemented CustomAdapter using ViewHolder. I am listening to the onclick event on item of List View with the following method
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
How can I access the description test field ?
The following is the xml which is instantiated by the adapter
<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/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="25dp"
android:textColor="#cccccc"
android:textSize="20sp" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="25dp"
android:textColor="#cccccc"
android:visibility="invisible"
android:textSize="20sp" />
</RelativeLayout>
One way
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.title);
String value = tv.getText().toString();
TextView desc = (TextView) view.findViewById(R.id.title);
String descp = desc.getText().toString();
}
});
In onItemClick you can use the view object to initialize views. Then get the value from textview.
The second way use
parent.getItemAtPosition(position); // this is better
Here you will use the adapterview (parent) and get the item at that postion
Example :
HashMap<String,String> map = (HashMap<String,String> parent.getItemAtPosition(position);
// assuming you are populating data using hashmap in your case might be different
Then
String title = map.get("key");
Related
I am using FunDapter for my project.When i try to add imagebutton to my layout other texts become unclickable.Does nothing when i click them.How can i add button to this adapter?
Below is list_layout where everything works fine
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Isimsiz"
android:id="#+id/Housename"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="2dp"
android:clickable="false"
android:textColor="#241d1d"
android:layout_row="0"
android:layout_column="0"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Aciklama Yok"
android:id="#+id/Desc"
android:layout_below="#+id/Housename"
android:clickable="false"
android:layout_alignStart="#+id/Housename"
android:textColor="#241d1d"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_alignEnd="#+id/Housename" />
</RelativeLayout>
When i add image button nothing is clickable except imagebutton
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_alignTop="#+id/Housename"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/Desc"
android:clickable="true"
android:onClick="berk"
/>
And this is part of the class where i create my adapter
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.Housename, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.HouseID;
}
});
dict.addStringField(R.id.Desc, new StringExtractor<Product>() {
#Override
public String getStringValue(Product product, int position) {
return product.Desc;
}
});
FunDapter<Product> adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.layout_list, dict);
lvProduct = (ListView)findViewById(R.id.lvProduct);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do sth
}
public void berk(View v) {
//do sth
}
Note:Also when i give clickable attribute to texts in xml they stop working.Like When i play with layout XML everything stops working.
In Adapter Class
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent, false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
}
But you can get an issue - listView row not clickable. Solution:
make ListView focusable android:focusable="true"
Button not focusable android:focusable="false"
First of all if u are using button or edit text in your adapter or row file Then every thing will get stop working .
You should use
1.Set descendant Focusablity="before Descendants" in the list view.
2.Set window Soft Input Mode="adjust Pan" in the manifest
3.Set list view.set Items Can Focus(true)
you have to give separate click event for all views.
Your ImageButton has android:clickable="true" which makes sub view clickable. But you other views are not clickable.
Solution
For click event in textview
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
similar for Imagebutton
remove
android:clickable="true"
hope it helps.
I'm a beginner in android app development. I write simple app to show listview and items in the list. I want when users click on the item show me row text.
adapter
ArrayAdapter<String> myAdaptor = new ArrayAdapter<String>(MainActivity.this, R.layout.rowlayout,R.id.label, itemsList)
rowlayout.xml
<ImageView
android:id="#+id/icon"
android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="#drawable/maleki" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="50px"
android:layout_gravity="right"
android:paddingLeft="100px"
>
</TextView>
I write this code into the listview click:
ListView myLST=(ListView)findViewById(R.id.listView1);
myLST.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemString=((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), tourdetail.class);
i.putExtra("countrydetail",itemString);
startActivity(i);
}
});
but in the this line I get error:
String itemString=((TextView) view).getText().toString();
How can I solve this problem? Thanks.
Try:
String itemString = ((TextView) view.findViewById(R.id.label)).getText().toString();
or
String itemString = myAdaptor.getItem(position)
Why would you cast the selected view item to TextView, which is a combination of ImageView and TextView. Check this for your solution:
myLST.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView label= (TextView) view.findViewById(R.id.label);
String text = label.getText().toString();
//remaining stuff
}
});
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();
}
});
I am developing an application in which i created a custom list view as:
The list view xml code as :
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="15dip"
android:divider="#623b42"
android:dividerHeight="0.5dip"
android:cacheColorHint="#00000000"
android:overScrollMode="never" >
</ListView>
And the adapter xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/textview_rupee_mlsaa"
android:gravity="center_vertical"
android:padding="15dip"
android:textColor="#color/color_black"
android:textSize="15sp"
tools:ignore="SelectableText" />
<TextView
android:id="#+id/textview2"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:padding="15dip"
android:textColor="#color/color_black"
android:textSize="15sp"
tools:ignore="SelectableText" />
</RelativeLayout>
The .java file of adapter is :
public class ListAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] dataset1;
private int[] dataset2;
/** Constructor Class */
public ListAdapter(Context context,String[] ListArray1, int[] ListArray2) {
super(context,R.layout.list_adapter_layout,ListArray);
this.context = context;
this.dataset1= ListArray1;
this.dataset2= ListArray2;
}
/** Implement getView method for customizing row of list view. */
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
// Creating a view of row.
View rowView = inflater.inflate(R.layout.list_adapter_layout, parent, false);
TextView tv1 = (TextView)rowView.findViewById(R.id.textview1);
TextView tv2 = (TextView)rowView.findViewById(R.id.textview2);
tv1.setText(data1[position]);
tv2.setText(""+data2[position]);
return rowView;
}
}
Then i set on item click listener on list view as :
listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);
ListAdapter adapter = new ListAdapter(this,list1,list2);
listView.setAdapter(adapter);
Then i used onItemClickListener on item click as :
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
name = (String) arg0.getItemAtPosition(position);
}
Here i am getting the value of first text view of custom listview. But i want also the value of second text view. and if i add more then how to get values of that.
Please suggest me the way to do that.
Try like this inside ur OnItemClick
TextView text = (TextView) arg1.findViewById(R.id.urtextID);
^^^^^^
String tEXT = text.getText().toString();
just a simplified version of above answer
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// code in the commented section works in case of normal single textView
// TextView temp=(TextView) view;
// Toast.makeText(this,
// temp.getText()+" is pressed "+position,Toast.LENGTH_SHORT
// ).show();
TextView temp = (TextView) view.findViewById(R.id.textView1);
String str = temp.getText().toString();
Toast.makeText(this,
str + " is pressed " + position,
Toast.LENGTH_SHORT).show();
}
Try this,
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
TextView textView = (TextView) arg1.findViewById(R.id.urtextID);
String text = textView.getText().toString();
Log.d("TEXT", "Text is" + text);
}
above answers are same like my answer.....
I am trying to implement list. Here is start.xml file which have a text box i which i am typing any word and a list of words will open containing this word:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
for list view i am using this ListView.xml file:
<ImageView
android:id="#+id/image_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/abacus_thumbnail"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/imageView_color_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/blue_thumbnail" />
<TextView
android:id="#+id/title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image_list"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/image_list"
android:gravity="center"
android:text="Abacus"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/textView_meaning_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title_list"
android:layout_below="#+id/title_list"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp" />
i am using EfficientAdapetr to convert one View from one to another when my app start the EditBox will be empty and a background image . I have ClickListner on list item when a word is type a list opens and when click on item this word will open. Now problem is that when i click on item of list the background populate with word but listView not remove. on Item click i want to open my background with the word.
Here is my code:
listAdapter = new EfficientAdapter2(this);
setListAdapter(listAdapter);
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.ListView, null);
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
holder.word = (TextView) convertView.findViewById(R.id.title_list);
holder.meaning = (TextView) convertView
.findViewById(R.id.textView_meaning_list);
holder.image = (ImageView) convertView
.findViewById(R.id.image_list);
holder.image_color = (ImageView) convertView
.findViewById(R.id.imageView_color_list);
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
try {
"functionalty here"
} catch (IOException e) {
e.printStackTrace();
}
return convertView;
}
here is onItemClick:
protected void onListItemClick(ListView l, View v, int position, long id) {////////
super.onListItemClick(l, v, position, id);
//Log.i("Position",""+position);
changeEverything(position);// here i am converting my ListView to
Start.xml but its on happening
motherOfIndex = position;
}
first debug your app and check weather onListItemClick() event firing. this won't fire if you have clickable items in your listView. if that's the case you have to set isClickable=false for all the items in ListView.xml file. in order to dismiss the the listView, you have to set it's visibility to View.GONE
However if you want to popup a suggestion list when something type on a EditText, this is not the best practice to do that. you can use a AutoCompleteTextView and directly set your ListView adapter to it. they it will create a listView for your and will automatically handle the set selected text to the AutoCompleteTextView and will dismiss the listView