Reputation: 832
i m creating webservice application. it has ArrayAdapter class that is present the web data to Listview. and i want to get index of selected Listview Item and send the index value to next activity to do other task. but i m not able to get Index from ListView. ListView id is default Android API id
I used almost everything like setOnItemSelectedListener and setOnClickListener OnItemSelectedListener but did not work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "xxxxxxxx";
// json class doing background data processing
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
@Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
lv.setAdapter(adapter);
}
THIS IS ArrayAdapter class
public class ApplicationAdapter extends ArrayAdapter<Application> implements OnItemClickListener{
private List<Application> items;
ListView listview;
public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_custom_list, items);
this.items = items;
MainActivity.lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id)
{
String text = ((TextView)childView.getTag()).toString();
Log.d("index",text);
}
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
}
@Override
public int getCount() {
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_custom_list, null);
}
Application app = items.get(position);
if(app != null) {
ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
for(int i=1; i<=5; i++) {
ImageView iv = new ImageView(getContext());
if(i <= app.getRating()) {
iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_checked));
}
else {
iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_unchecked));
}
ratingCntr.addView(iv);
}
}
}
return v;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String[] lv_arr = null;
// TODO Auto-generated method stub
String itemname = lv_arr[position];
Log.d("string from ListView",itemname);
}
}
Please help me to get Index of Selected Item of ListView. if anyone can suggest me what should i use , i will be grateful to them
thanks in Advance
Upvotes: 1
Views: 4532
Reputation: 5361
at the bottom of protected void onCreate(Bundle savedInstanceState) {
add
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get position index of item here.
String indexid = String.valueOf(position);
//and do whatever afterwards.
});
}
and remove public void onItemClick(AdapterView<?> parent, View view, int position, .......
in your adapter
Upvotes: 2