This is my code:
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
View vie = (View)parent.getParent();
TextView tv = (TextView) vie.findViewById(R.id.tv_id);
String genus = (String) tv.getText();
Toast.makeText(getBaseContext(), genus+"--"+id, Toast.LENGTH_SHORT).show();
}
});
return adapter;
}
Every time it is returning the same value for every item. Here are my list items:
<?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/tv_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/iv_flag"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_below="#id/tv_country"
android:layout_centerVertical="true"
android:padding="5dp"
/>
<TextView
android:id="#+id/tv_country_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/iv_flag"
android:layout_below="#id/tv_country" />
<TextView
android:id="#+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_country_details"
/>
</RelativeLayout>
Here is my view file.
<RelativeLayout 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" >
<ListView
android:id="#+id/lv_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".NearbyGymList" />
<Button android:id="#+id/btnNoResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lv_countries"
android:text="No More Results"/>
</RelativeLayout>
How can fix this problem?
Remove this line:
View vie = (View)parent.getParent();
And change to use view from parameter, because it is the view of item which is clicked:
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//Remove this line: View vie = (View)parent.getParent();
//Use view because it is the view of item which is clicked.
TextView tv1 = (TextView) view.findViewById(R.id.tv_id);
//Add this
TextView tv2 = (TextView) view.findViewById(R.id.tv_country_details);
String genus = (String) tv1.getText().toString();
String genus2 = (String) tv2.getText().toString();
Toast.makeText(getBaseContext(),genus+"--"+id+" "+genus2 , Toast.LENGTH_SHORT).show();
}
});
return adapter;
}
You're not adding a reference to your others TextViews. Do it this way:
Remove this line
View vie = (View)parent.getParent();
so your code will become:
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView tv1 = (TextView) view.findViewById(R.id.tv_id);
TextView tv2 = (TextView) view.findViewById(R.id.tv_country_details);
String genus = (String) tv1.getText().toString();
String genus2 = (String) tv2.getText().toString();
Toast.makeText(getBaseContext(),genus+"--"+id+" "+genus2 , Toast.LENGTH_SHORT).show();
}
});
return adapter;
}
To access the element in the dataset at position you can use
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object object = parent.getItemAtPosition(position);
and cast it to the correct type.
In Adapter Class
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.yourlayout, parent, false);
TextView textView = (TextView) row.findViewById(R.id.textView);
textView .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do whatever you want
});
Related
I have a list view from which the user must choose two teams by checking the boxes and then validate by pressing the OK button. The problem is I want to make it so that there can only be two checked boxes at any time
Example :
if the user picks team 1 and 2 then the boxes for 1 and 2 should be checked but if the user then picks team 3, 1 should un-check automatically.
I've already managed to isolate and store the position of the box that needs to be unchecked but I don't know what to do with it.
Thanks!!
heres the listView
<Button
android:id="#+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
/>
<ListView
android:id="#+id/listEquipe"
android:layout_below="#+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and the following 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="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txtIdEquipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<CheckedTextView
android:id="#+id/txtNomEquipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="5dp"
android:textSize="12pt"
android:textColor="#000000"
/>
</LinearLayout>
This is one way to achieve it (I used stupid data in the adapter):
public class MainActivity extends AppCompatActivity {
private List<Integer> selectedItems = new ArrayList<>();
private String[] teams = new String[]{ "Team A", "Team B", "Team C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView) findViewById(R.id.listEquipe);
final BaseAdapter adapter = new BaseAdapter() {
#Override
public int getCount() {
return teams.length;
}
#Override
public Object getItem(int position) {
return teams[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.item, null);
((TextView) view.findViewById(R.id.txtIdEquipe)).setText((String) getItem(position));
CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.txtNomEquipe);
ctv.setChecked(selectedItems.contains(position));
return view;
}
};
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(selectedItems.contains(position)) {
selectedItems.remove(position);
}
else {
selectedItems.add(position);
if(selectedItems.size() > 2) {
selectedItems.remove(0);
}
}
adapter.notifyDataSetChanged();
}
});
}
}
Sorry for my english. I spent meny times try fix my problem, but I could not do it. I have listView, in this list view for some elements create spinner. Spinner created in adapter. I cant get value spinner. Its vary hard from me. Please healp
My adapter
public class ExpListAdapter extends BaseExpandableListAdapter {
private ArrayList<ArrayList<String>> mGroups;
private ArrayList<DeviceObject> deviceObList;
private ArrayList<RoomSuggestion> roObjList;
private Context mContext;
public ExpListAdapter (Context context,ArrayList<ArrayList<String>> groups, ArrayList<DeviceObject> deviceObList, ArrayList<RoomSuggestion> roObjList){
mContext = context;
mGroups = groups;
this.deviceObList = deviceObList;
this.roObjList = roObjList;
}
#Override
public int getGroupCount() {
return mGroups.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return mGroups.get(groupPosition).size();
}
#Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return mGroups.get(groupPosition).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_view, null);
}
if (isExpanded){
//Изменяем что-нибудь, если текущая Group раскрыта
}
else{
//Изменяем что-нибудь, если текущая Group скрыта
}
Typeface lightFace = Typeface.createFromAsset(mContext.getAssets(), "font/GothamProLight.ttf");
TextView textGroup = (TextView) convertView.findViewById(R.id.textGroup);
textGroup.setTypeface(lightFace);
textGroup.setText("Thereses gate 46");
return convertView;
}
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_view, null);
}
Typeface mediumFace = Typeface.createFromAsset(mContext.getAssets(), "font/GothamProMedium.ttf");
TextView textChild = (TextView) convertView.findViewById(R.id.textChild);
textChild.setTypeface(mediumFace);
textChild.setText( mGroups.get(groupPosition).get(childPosition) );
RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.bg_button_screen);
if( !deviceObList.get(childPosition).getProduct_id().equals("0") ) {
rl.setBackgroundColor(Color.parseColor("#4fcc54"));
View linearLayoutG = convertView.findViewById(R.id.container);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 10, 30);
linearLayoutG.setLayoutParams(layoutParams);
RelativeLayout spinnerOpen = (RelativeLayout) convertView.findViewById(R.id.spinnerOpen);
View linearLayout = convertView.findViewById(R.id.spinnerL);
ImageView imageS = (ImageView)convertView.findViewById(R.id.spinnerImage);
imageS.getLayoutParams().width = 20;
imageS.getLayoutParams().height = 20;
imageS.setImageResource(R.drawable.spin_ok);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < roObjList.size(); i++) {
list.add(roObjList.get(i).getName() );
}
final Spinner spinner = new Spinner(mContext);
//Make sure you have valid layout parameters.
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100));
spinner.setBackgroundResource(R.drawable.bg_spinner);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(mContext,
R.layout.spinner_item, list);
spinner.setAdapter(spinnerArrayAdapter);
//open spinner
spinnerOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner.performClick();
}
});
((LinearLayout) linearLayout).addView(spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("selected", String.valueOf(parent.getItemAtPosition(position)) );
Log.e("childPosition", String.valueOf(childPosition));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} else {
rl.setBackgroundColor(Color.parseColor("#e5910d"));
}
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
My xml from adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/bgchilddevice"
android:layout_width="match_parent"
android:layout_weight="0.3"
android:layout_marginLeft="5dp"
android:background="#26ffffff"
android:layout_marginRight="2.5dp"
android:layout_height="60dp">
<TextView
android:id="#+id/textChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="18dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/bg_button_screen"
android:layout_weight="0.9"
android:layout_marginRight="5dp"
android:background="#4fcc54"
android:layout_width="match_parent"
android:layout_marginLeft="2.5dp"
android:layout_height="60dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/check"
android:id="#+id/imageView6"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#26ffffff"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/spinnerL"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="0.2"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/spinnerOpen"
android:layout_width="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.8"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerImage"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
And that i try get spinner elements
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i < listView.getAdapter().getCount(); i++ ) {
for(int j = 0; j < listView.getChildCount(); j++ ) {
View _mainView = listView.getChildAt(j);
LinearLayout _linearLayout = (LinearLayout) _mainView.findViewById(R.id.spinnerL);
Spinner spinner = (Spinner) _linearLayout.getChildAt(0);
String selection = (String) spinner.getSelectedItem();
Log.e("spinner device", selection);
}
}
}
});
And i have
FATAL EXCEPTION: main java.lang.NullPointerException
in line Spinner spinner = (Spinner) _linearLayout.getChildAt(0);
i try write
Spinner spinner = (Spinner) _linearLayout.getChildAt(0);
Spinner spinner = (Spinner) _linearLayout.getChildAt(1);
Spinner spinner = (Spinner) _linearLayout.getChildAt(2);
Spinner spinner = (Spinner) _linearLayout.getChildAt(3);
its error
Why don't you use findViewById() for the spinner?
Spinner spinner =(Spinner) findViewById(R.id.spinnerL);
String value = spinner.getSelectedItem().toString();
I dont know it bad code or not, i try write like this and its work!!!
for(int j = 0; j < listView.getChildCount(); j++ ) {
View _mainView = listView.getChildAt(j);
LinearLayout _linearLayout = (LinearLayout) _mainView.findViewById(R.id.spinnerL);
try{
Spinner spinner = (Spinner) _linearLayout.getChildAt(0);
String selection = (String) spinner.getSelectedItem();
Log.e("spinner device", selection);
}catch(Exception e){
}
}
I have a custom list adapter that populates a listview.
I am trying to get each item in the listview to be clickable. When clicked, I want the app to load another activity and populate it with the proper data. The data comes from a java list of listing.java.
I can't seem to get it to respond to clicks, here is what I've tried so far:
//this is in the onCreate method
final ListView listview = (ListView) findViewById(R.id.listview);
listingsAdapter = new ListingsAdapter(this, mylistings);
listview.setAdapter(listingsAdapter);
here is my first attempt (this was just to get toast working, but it didn't work)
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view,int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG).show();
};
});
I have also tried this:
listview.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent i = new Intent(HomeScreenActivity.this, DetailedViewActivity.class);
startActivity(i);
};
});
Help would be appreciated. Let me know if I should post my adapter as well!
Here is the adaptor:
public class ListingsAdapter extends BaseAdapter{
List<Listing> listings;
Context context;
LayoutInflater inflater;
public ListingsAdapter(Context context, List<Listing> listings){
this.context = context;
this.listings = listings;
inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public boolean isEnabled(int position)
{
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View _localView = convertView;
if (_localView == null){
_localView = inflater.inflate(R.layout.main_cell, parent, false);
}
TextView text1 = (TextView) _localView.findViewById(R.id.firstline);
TextView text2 = (TextView) _localView.findViewById(R.id.secondLine);;
Listing listing = listings.get(position);
text1.setText(listing.getTitle());
text2.setText(listing.getAddress());
return _localView;
}
#Override
public int getCount(){
// TODO Auto-generated method stub
return listings.size();
}
#Override
public Object getItem(int arg0){
return listings.get(arg0);
}
#Override
public long getItemId(int arg0){
// TODO Auto-generated method stub
return arg0;
}
}
this is the main_cell.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dp"
android:background="#CC7C43"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="#+id/firstline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp"/>
</RelativeLayout>
Right, so I am recovering from a severe hangover and I just remembered that I promised to elaborate on the link I posted - disregard the link!
You should add the following piece of code to your ListingsAdapter:
#Override
public boolean isEnabled(int position)
{
return true;
}
And you should edit the RelativeLayout in your main_cell.xml from
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dp"
android:background="#CC7C43"
android:longClickable="true">
to
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dp"
android:background="#CC7C43">
This works, I have tested it.
try to initialize local instance of View:
#Override
public View getView(int position, View convertView, ViewGroup parent){
View _localView = convertView;
if (_localView == null){
_localView = inflater.inflate(R.layout.main_cell, parent, false);
}
TextView text1 = (TextView) _localView.findViewById(R.id.firstline);
TextView text2 = (TextView) _localView.findViewById(R.id.secondLine);;
Listing listing = listings.get(position);
text1.setText(listing.getTitle());
text2.setText(listing.getAddress());
return _localView;
}
UPD:
Also modify other necessary methods in your adapter class, because you need to get id of an item clicked for using it in onItemClick method:
#Override
public Object getItem(int arg0){
// TODO Auto-generated method stub
listings.get(arg0);
}
#Override
public long getItemId(int arg0){
// TODO Auto-generated method stub
return arg0;
}
as an example. Hope this will help you.
I've working on ListView with a custom BaseAdapter which I've watched on the Slidenerd tutorial series here:(It's not important to watch to understand my question)
http://www.youtube.com/watch?v=_l9e2t4fcfM&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa&index=91
After running the code on the virtual device there is no error but not ListView too.
Is it possible to tell me what's the problem of my code?
public class List extends Activity {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new EhsanAdapter(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
}
class SingleRow{
String title;
String description;
int image;
public SingleRow(String title,String description,int image) {
this.title = title;
this.description=description;
this.image=image;
}
}
class EhsanAdapter extends BaseAdapter{
ArrayList<SingleRow> list;
Context context;
public EhsanAdapter(Context c) {
list = new ArrayList<SingleRow>();
context = c;
Resources res = c.getResources();
String[] titles = res.getStringArray(R.array.titles);
String[] descriptions = res.getStringArray(R.array.descriptions);
int[] images = {R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};
for(int i=0;i<10;i++){
list.add(new SingleRow(titles[i], descriptions[i], images[i]));
}
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewGroup,false);
TextView title = (TextView) row.findViewById(R.id.txtTitle);
TextView description = (TextView) row.findViewById(R.id.txtDescription);
ImageView image = (ImageView) row.findViewById(R.id.imgPic);
SingleRow temp = list.get(i);
title.setText(temp.title);
description.setText(temp.description);
image.setImageResource(temp.image);
return row;
}
}
The layout of activity:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".List" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
The layout of single row:
<?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" >
<ImageView
android:layout_margin="10dp"
android:id="#+id/imgPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/image1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="#+id/txtTitle"
android:layout_alignTop="#+id/imgPic"
android:layout_toRightOf="#+id/imgPic"
android:layout_alignParentRight="true">
</TextView>
<TextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imgPic"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/imgPic"
android:layout_marginTop="20dp"
android:text="Description"
android:ems="10">
</TextView>
</RelativeLayout>
Remove the line LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); from getView() method and instead add it in the constructor of EhsanAdapter class.
I see different issues:
The layout of single row contains a relative layout which the android:layout_height="match_parent" should be android:layout_height="wrap_content"
Then inside the Adapter you are neither recycling the views nor using the ViewHolder pattern:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
About ViewHolder pattern implementation optimisation in ListView
http://www.jmanzano.es/blog/?p=166
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){ //The row view is not created, let's do it:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewGroup,false);
TextView title = (TextView) row.findViewById(R.id.txtTitle);
TextView description = (TextView) row.findViewById(R.id.txtDescription);
ImageView image = (ImageView) row.findViewById(R.id.imgPic);
// Here we add the title, description and image inside the ViewHolder
....
}else{
//With the View holder pattern we get the views inside the row view to fill it later
....
}
// Now we get the SingleRow and we fill it using the ViewHolder pattern
SingleRow temp = list.get(i);
....
return row;
}
You only have 5 images and you wrote i<10 you should've wrote i<5 the amount of "titles" and "descriptions" and "images" must be the same.
for(int i=0;i<5;i++){
list.add(new SingleRow(titles[i], descriptions[i], images[i]));
}
| Icon(image) | Title(text) | cross(image) |
| | Description(text)| |
| | | coupon(image) |
Its a list view.
Here i want to get id of different items when clicked separately in list view like cross, coupon, icon is clicked then i will get their id...
I'm a newbie...Please help me out....
<?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="fill_parent" >
<ImageView
android:id="#+id/list_item_iv_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="#string/app_name"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<ImageView
android:id="#+id/list_item_iv_icon_cross"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/cross_selector" />
<TextView
android:id="#+id/list_item_tv_title"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="#+id/list_item_iv_icon"
android:gravity="left"
android:textColor="#CC0033"
android:textIsSelectable="false"
android:textSize="20sp" />
<TextView
android:id="#+id/list_item_tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/list_item_tv_title"
android:layout_toRightOf="#+id/list_item_iv_icon"
android:gravity="left"
android:textColor="#3399FF"
android:textIsSelectable="false"
android:textSize="14sp" />
<ImageView
android:id="#+id/list_item_iv_type"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignBottom="#id/list_item_iv_icon"
android:layout_alignParentRight="true"
android:contentDescription="#string/app_name" />
</RelativeLayout>
I know how to get id of the clicked list. i want to know is how to get item id of a clicked item in list.
Here is the answer for NewBies like Me...
<ImageView
android:id="#+id/list_item_iv_icon_cross"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/cross_selector"
android:onClick="onCrossClick" />
i used onClick on item and to get position in which it has been clicked i used
final int position = listView.getPositionForView((View) v.getParent());
full code :-
public void onCrossClick(View v) {
final int position = listView.getPositionForView((View) v.getParent());
Toast.makeText(this, "click on button " + position, Toast.LENGTH_LONG)
.show();
}
The following is the code to set a listener for the list view which would capture the list item selection event.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
// To get the item from the list use the position value
Object yourObject = yourAdapter.getItem(position);
}
})
Check below Sample code :
public class ListViewAdapter_test extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter_test(Context con) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(con);
}
public int getCount() {
// TODO Auto-generated method stub
return a_product_id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
// return product_id1.size();
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
// return product_id1.get(position).hashCode();
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.scan_row1, null);
holder = new ListContent();
holder.name = (TextView) v.findViewById(R.id.sc_textname);
holder.name1 = (TextView) v.findViewById(R.id.sc_review);
holder.ratings = (RatingBar) v.findViewById(R.id.sc_ratingBar1);
holder.total_rate = (Button) v.findViewById(R.id.button1);
holder.img_p = (ImageView) v.findViewById(R.id.image_prod);
// holder.total_rate.setOnClickListener(mOnTitleClickListener1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.total_rate.setOnClickListener(mOnTitleClickListener3);
holder.img_p.setOnClickListener(mOnTitleClickListener_image);
return v;
}
}
static class ListContent {
ImageView img_p;
TextView name1;
TextView name;
RatingBar ratings;
Button total_rate;
}
public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
public void onClick(View v) {
final int position = list_v
.getPositionForView((View) v.getParent());
Log.d("you are click on Ratings","you are click on Ratings");
}
};
public OnClickListener mOnTitleClickListener_image = new OnClickListener() {
public void onClick(View v) {
final int position = list_v
.getPositionForView((View) v.getParent());
Log.d("you are click on image view","you are click on image view");
}
};