How to get item value in a listview in android? - java

I have referenced to a tutorial and I have created a custom list view in fragment, but I am unable to get the position or value of any item on item click of list view. I want to show a toast containing the position of the item. I don't know how to implement that.
CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter{
private ArrayList<MyActivityAdapterItem> listData;
private Context context;
//private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<MyActivityAdapterItem> listData) {
this.listData = listData;
this.context = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object 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) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row_layout, null);
// holder = new ViewHolder();
//convertView.setTag(holder);
}
TextView headingView = (TextView) convertView.findViewById(R.id.title);
TextView placeView = (TextView) convertView.findViewById(R.id.place);
TextView reportedDateView = (TextView) convertView.findViewById(R.id.date);
headingView.setText(listData.get(position).getHeading());
placeView.setText("At, " + listData.get(position).getPlace());
reportedDateView.setText(listData.get(position).getDate());
return convertView;
}
}
FindPeopleFragment.java
public class FindPeopleFragment extends Fragment implements OnClickListener {
AlertDialog.Builder builder ;
ListView lv1;
ImageButton delall;
CharSequence options[] = new CharSequence[] {"Delete"};
ArrayList details;
CustomListAdapter ad1;
String msg="abc";
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
delall=(ImageButton) rootView.findViewById(R.id.deleteall);
details = getListData();
lv1 = (ListView) rootView.findViewById(R.id.activitylist);
ad1=new CustomListAdapter(getActivity(), details);
lv1.setAdapter(ad1);
builder= new AlertDialog.Builder(getActivity());
delall.setOnClickListener(this);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//Toast.makeText(getActivity(), , Toast.LENGTH_LONG).show();
/*Bundle args = new Bundle();
args.putString(MyActivitiesFragment.MSG, msg);
MyActivitiesFragment f1=new MyActivitiesFragment();
f1.setArguments(args);
MainActivity.fm.beginTransaction().replace(R.id.frame_container,f1).commit();*/
}
});
return rootView;
}

The position of the clicked item in a ListView can be retrived easily on the onItemClick method as you can see in the documentation:
public abstract void onItemClick (AdapterView parent, View view,
int position, long id)
Callback method to be invoked when an item in this AdapterView has
been clicked.
Implementers can call getItemAtPosition(position) if they need to
access the data associated with the selected item.
Parameters parent The AdapterView where the click happened. view The
view within the AdapterView that was clicked (this will be a view
provided by the adapter) position The position of the view in the
adapter. id The row id of the item that was clicked.
So in your case, the position of the clicked item is the arg2 value. Your code should be like this:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity(), arg2+"", Toast.LENGTH_LONG).show();
/*...*/
}
EDIT:
To get the Item, you can do like this:
(Item) arg0.getAdapter().getItem(arg2);
TL;DR
You can see in this case that it is important to use good names for parameters. With the default names given by Eclipse - or by others IDEs - this line:
(Item) arg0.getAdapter().getItem(arg2);
looks pretty strange. But if you are using the Android names - which gives you onItemClick(AdapterView<Item> parent, View view, int position, long id) - the line becomes:
(Item) parent.getAdapter().getItem(position);
Which is clearer and easier to use.

this should work:
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Selected item at position: " + position, Toast.LENGTH_LONG).show();
}
}

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Intent i1 = new Intent(getApplicationContext(), Plaeeng.class);
// List<ScanResult> wifiList;
// i1.putExtra("cuor", );
// startActivity(i1);
Toast.makeText(ListCours.this, "=>"+parent.getAdapter().getItem(position), 5000).show();
}
});

You are doing well in setOnItemClickListener
the problem its the autocomplete its not working well in parameters you should have something like this:
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
the position is in your arg2, and as a tip you can get the item you click using this snippet:
parent.getAdapter().getItem(position);

Related

Set image and text of list view item in another imageview and textview

I have an array of images and string like this:
String[] stations = new String[] {
"GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};
public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};
In an custom listview how can I replace these in another Image view and texview?
I need to change that imageview and textview everytime with the clicked item's image and text.
Here's my Custom Adapter:
public class TrackAdapter extends BaseAdapter{
String [] description;
Context context;
int [] imageId;
public TrackAdapter(Context c, String[] d, int[] prgmImages) {
description=d;
context= c;
imageId=prgmImages;
}
#Override
public int getCount() {
return description.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.cardviewlayout,null);
}
//get the textview and set its text
TextView tv=(TextView) convertView.findViewById(R.id.tracktitle);
//get the img view and set its img icon
ImageView im=(ImageView) convertView.findViewById(R.id.trackimage);
tv.setText(description[position]);
im.setImageResource(imageId[position]);
return convertView;
}
Here's Main Activity:
private TextView mSelectedTrackTitle;
private ImageView mSelectedTrackImage;
private MediaPlayer mMediaPlayer;
private ImageView mPlayerControl;
ListView lv_tracks;
TrackAdapter track_adapter;
String[] stations = new String[] {
"GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};
public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaPlayer = new MediaPlayer();
//get a reference to our ListView so we can associate it with our custom ArrayAdapter
lv_tracks = (ListView) findViewById(R.id.track_list);
//create a new CustomAdapter
track_adapter =new TrackAdapter(this,stations,images);
lv_tracks.setAdapter(track_adapter);//connect the ListView with myCustomAdapter
//Want to set selected image and title here in these
mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title);
mSelectedTrackImage =(ImageView)findViewById(R.id.selected_track_image);
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
??
}
Do not really understand what to do with onitemselect here .
in order to change image and text.
you can get current item position by using listview OnItemClickListener it will returns position of an Item clicked then you can use this position as your desired operation
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mSelectedTrackTitle.setText(stations[position]);
mSelectedTrackImage.setImageDrawable(ContextCompat.getDrawable(this,images [position]));
// or
mSelectedTrackImage.setImageResource(images [position]);
}
Try something like this
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedTrackTitle.setText( stations[position] );
mSelectedTrackImage.setImageResource( images[positions] );
}
It will get the station name and image from the clicked position in your arrays, and update your views.
You can achieve it like this:
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedTrackTitle.setText(stations[position]);
mSelectedTrackImage.setImageResource(images[position]);
}

Which event is called when user exits spinner

I have a spinner and I want to show/hide something when the user exits the spinner without doing any thing. for example, when the user touches an area outside the spinner.
p.s. the onTouchEvent for the container layout (LinearLayout in my case) is also not called.
here is my implementation for the custom spineer:
public SpinnerHintAdapter(Activity context, int resourceId, int textViewId, List<SpinnerItem> list, Spinner parent){
super(context,resourceId,textViewId, list);
flater = context.getLayoutInflater();
this.items = list;
this.gender = parent;
}
#Override
public int getCount() {
return items.size();
}
#Nullable
#Override
public SpinnerItem getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SpinnerItem spinnerItem = getItem(position);
View rowView = flater.inflate(R.layout.gender_item ,null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.main_text);
txtTitle.setText(spinnerItem.getName());
txtTitle.setTextColor(txtTitle.getResources().getColor(R.color.color_white));
Log.i(Tags.byEmail, "VVVVVVVVVVVVv");;
gender.setVisibility(View.VISIBLE);
return rowView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
firstTime = false;
SpinnerItem rowItem = getItem(position);
View rowView = flater.inflate(R.layout.gender_drop_down_item ,null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.drop_down_text);
txtTitle.setText(rowItem.getName());
if(!isEnabled(position)){
txtTitle.setBackground(txtTitle.getResources().getDrawable(R.drawable.normal_rounded_text_field));
txtTitle.setTextColor(Color.parseColor("#777777"));
}
parent.setBackground(parent.getResources().getDrawable(R.drawable.normal_rounded_text_field));
txtTitle.setEnabled(isEnabled(position));
gender.setVisibility(View.INVISIBLE);
Log.i(Tags.byEmail, "DDDDDDDDDDDDDDDDDD");
return rowView;
}
#Override
public boolean isEnabled(int position) {
if(position == 0)
return false;
return super.isEnabled(position);
}
}
when the user exits without selecting an item the getView function is not called and hence the gender (is the spinner object itself) will not be visible.
I tried OnItemSelected and OnNothingSelected are also not called.
event OnTouch events are not called.
The following Events are not called when the user exits:
1- OnItemSlected
2- OnNothingSelected
3- OnFocusChanged.
4- OnTouch
I think that's event will work for you as magic, no?
public class CustomSpinner extends Spinner {
... Constructors ...
#Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
Log.d(CustomSpinner.class.getSimpleName(), "onWindowFocusChanged: " + hasWindowFocus);
if (hasWindowFocus) {
// User click out of window
} else {
// User click in spinner window
}
}
}
With a Spinner you set an AdapterView.OnItemSelectedListener which implements two methods; onItemSelected and onNothingSelected.
Like this:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// This is called when the user selects an item in the Spinner
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// This is called when the user closes the spinner selecting nothing
}
});

Read text from selected item when there are multiple items in a listview

I have customized an adapter with a list view with four items in each list item:
TextView id = tid
TextView id = tname
TextView id = tgender
TextView id = tage
Now on click, I want to get text from the textview of the item with the id 'tid'.
public class TeacherAdapter extends ArrayAdapter <Teacher>{
private LayoutInflater inflater;
private ArrayList<Teacher> teachers;
public TeacherAdapter(Context context, ArrayList<Teacher> teachers) {
super(context, 0, teachers);
inflater = LayoutInflater.from(context);
this.teachers = teachers;
}
public int getCount() {
return teachers.size();
}
public Teacher getItem(int position) {
return teachers.get(position);
}
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView txtName;
TextView txtMobile;
TextView txtDept;
TextView txtId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.teacher_text_item, null);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.tName);
viewHolder.txtMobile = (TextView) convertView.findViewById(R.id.tMobile);
viewHolder.txtDept = (TextView) convertView.findViewById(R.id.tDepartment);
viewHolder.txtId = (TextView) convertView.findViewById(R.id.tId);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtName.setText(teachers.get(position).getName());
viewHolder.txtMobile.setText(teachers.get(position).getMobile());
viewHolder.txtDept.setText(teachers.get(position).getDepartment());
viewHolder.txtId.setText(teachers.get(position).getId()+"");
return convertView;
}
}
I have four items in a multiple listview: name, mobileno, department, id. All I want to do is getId from the list item when it is clicked.
This code have solved my problem.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (v != null){
TextView textView = (TextView)v.findViewById(R.id.tId);
Log.e("idthatclicked",textView.getText().toString());
}
}
Correct code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
teacher= parent.getItemAtPosition(position)
String text = teacher.getId();
//TextView tid = (TextView) view.findViewById(R.id.tid);
//String text = tid.getText().toString();
}
});
In onItemClick method of listview, write this code to get the id. here you need to pick up the value from the adapter directly as mentioned below
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
int id= adapter1.getItem(position).getId();
}
or
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
int id = getListView().getItemAtPosition(position).getId();
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder holder= (ViewHolder ) view.getTag();
String text = holder.txtId.getText().toString();
}
});
by this way , you can skip the find by resouceId part,use holder instead.
If you have an OnItemClickListener, the view that was clicked is passed to it. Check that the view is not null. Then, do:
#Override
public void onItemClick(AdapterView<?> arg0, View viewThatWasClicked,
int position, long arg3) {
TextView tid = (TextView) viewThatWasClicked.findViewById(R.id.tid);
String text= tid.getText().toString();
}

Android JSON Data not parsing into ListView

Android not parsing JSON data into ListView, I am using this tutorial and just made few changes in ListViewAdapter.java
Like in my new implementation i used ViewHolder, and my code looks like this:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
ViewHolder holder;
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
public ViewHolder(View convertView) {
// TODO Auto-generated constructor stub
}
TextView rank;
TextView country;
TextView population;
ImageView flag;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
// Avoid unneccessary calls to findViewById() on each row, which is expensive!
holder = null;
/*
* If convertView is not null, we can reuse it directly, no inflation required!
* We only inflate a new View when the convertView is null.
*/
if (convertView == null) {
convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);
// Create a ViewHolder and store references to the two children views
holder = new ViewHolder(convertView);
holder.rank = (TextView) convertView.findViewById(R.id.rank);
holder.country = (TextView) convertView.findViewById(R.id.country);
holder.population = (TextView) convertView.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
holder.flag = (ImageView) convertView.findViewById(R.id.flag);
// The tag can be any Object, this just happens to be the ViewHolder
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Capture position and set results to the TextViews
holder.rank.setText(resultp.get(MainActivity.RANK));
holder.country.setText(resultp.get(MainActivity.COUNTRY));
holder.population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
// Capture ListView item click
return convertView;
}
}
Edited: Click on ListItem code
#Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// code to handle click
}
});
}
But i don't no why i am not getting data into ListView !
The issue is that you are not assigning the HashMap to `resultp that has the information you want to display
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
if (convertView == null) {
convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);
holder = new ViewHolder(convertView);
holder.rank = (TextView) convertView.findViewById(R.id.rank);
holder.country = (TextView) convertView.findViewById(R.id.country);
holder.population = (TextView) convertView.findViewById(R.id.population);
holder.flag = (ImageView) convertView.findViewById(R.id.flag);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Here's the change
resultp = data.get(position);
// Here's the change
holder.rank.setText(resultp.get(MainActivity.RANK));
holder.country.setText(resultp.get(MainActivity.COUNTRY));
holder.population.setText(resultp.get(MainActivity.POPULATION));
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
return convertView;
}
To attach OnItemClickListener to your ListView, in the Activity that contains the ListView, add the following:
public class MyActivity implements OnItemClickListener{
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState() {
....
....
// lv initialized here
// adapter of lv set here
attachListeners();
}
private void attachListeners() {
....
....
// attach listeners to other views if you like
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// code to handle click
}
}
Or, if you don't want your Activity to implement OnItemClickListener, then,
public class MyActivity {
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState() {
....
....
// lv initialized here
// adapter of lv set here
attachListeners();
}
private void attachListeners() {
....
....
// attach listeners to other views if you like
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// code to handle click
}
});
}
}
First of all try to fix this:
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}

setOnItemClickListener on Listview that extend activity

Update : I found the solution by
removing
v.setFocusable(true);
v.setClickable(true); in the code
and only add
v.setEnabled(true);
and in my xml (ListView) add
android:drawSelectorOnTop="true"
android:focusable="true"
When I click it nothing happens , even the list view doesn't focus.
I have tried to add all this:
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
Only this will work is i add the following code:
But this doesn't determine what item is being clicked
How to handle ListView click in Android
and the result still the same.
Here's is the code :
public class AppsInspectorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//[...]
ListView app_listView = (ListView)findViewById(R.id.listview);
// I try setOnItemClickListene here - 1
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});
}
Adapter
public class AppAdapter extends BaseAdapter {
Context context;
ArrayList<AppInfo> dataList=new ArrayList<AppInfo>();
public AppAdapter(Context context,ArrayList<AppInfo> inputDataList)
{
this.context = context;
dataList.clear();
for(int i=0;i<inputDataList.size();i++)
{
dataList.add(inputDataList.get(i));
}
}
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
final AppInfo appUnit = dataList.get(position);
ListView app_listView = (ListView)findViewById(R.id.listview);
/** Remove this , i just try to add to see setOnItemClickListener will work at here or not **/
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.app_row, null);
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
}
TextView appName=(TextView)v.findViewById(R.id.appName);
ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);
if(appName!=null)
appName.setText(appUnit.appName);
if(appIcon!=null)
appIcon.setImageDrawable(appUnit.appIcon);
return v;
}
}
}
}
Can you please post the main.xml layout. Seems to me that you have another view on top of the listview that is consuming the touch events.
I found the solution by removing
v.setFocusable(true); v.setClickable(true); in the code
and only add
v.setEnabled(true);
and in my xml (ListView) add
android:drawSelectorOnTop="true" android:focusable="true"
Hi remove the setOnItemClickListener in the getView of the Adapter.Refer this listView
use AppsInspectorActivity.this instead of getApplicationContext()
public class AppsInspectorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//[...]
ListView app_listView = (ListView)findViewById(R.id.listview);
// I try setOnItemClickListene here - 1
app_listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(AppsInspectorActivity.this, "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
}
});
}
and remove the setOnItemClickListener from the getView() method in your adapter
Change your getView() to below one and tell if it works
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
final AppInfo appUnit = dataList.get(position);
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.app_row, null);
v.setClickable(true);
v.setEnabled(true);
v.setFocusable(true);
}
TextView appName=(TextView)v.findViewById(R.id.appName);
ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);
if(appName!=null)
appName.setText(appUnit.appName);
if(appIcon!=null)
appIcon.setImageDrawable(appUnit.appIcon);
return v;
}
You have to set adapter to listview before setting its onItemClick.Here you just get the listview but with no items.so when you click on listview,its onItemClick is never called.

Categories

Resources