I'm creating a ListView with many item, each row of ListView contains a TextView and a "Remove" button, when click on Remove button (not click on the row), that row should show the toast, run the animation and remove from the list but it only show the toast (It works well when I click on the row)
This is my code
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] items = new String[20];
ListView lv;
MyAdapter mAdapter;
ArrayList<String> data = new ArrayList<String>();
private int position;
Animation ani;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 20; i++) {
items[i] = String.valueOf(i + 1);
data.add(items[i]);
}
lv = (ListView) findViewById(R.id.listView1);
mAdapter = new MyAdapter(getApplication(), R.layout.custom_row, data);
lv.setAdapter(mAdapter);
ani = AnimationUtils.loadAnimation(getApplicationContext(), R.animator.fadeout);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "You have clicked on item " + arg2, Toast.LENGTH_SHORT).show();
arg1.startAnimation(ani);
ani.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mAdapter.remove(arg2);
}
});
}
});
}
class MyAdapter extends ArrayAdapter<String> {
ArrayList<String> data;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(layout, parent, false);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(data.get(position));
Button bt = (Button) v.findViewById(R.id.button1);
bt.setOnClickListener(new MyClick(this, position));
return v;
}
class MyClick implements View.OnClickListener {
MyAdapter p;
int i;
public MyClick(MyAdapter parent, int pos) {
p = parent;
i = pos;
}
#Override
public void onClick(View v) {
lv.performItemClick(lv.getAdapter().getView(i, null, null), i, i);
}
}
public void remove(int pos) {
data.remove(pos);
notifyDataSetChanged();
}
int layout;
public MyAdapter(Context c, int l, ArrayList<String> data_name) {
super(c, l, data_name);
layout = l;
data = data_name;
}
}
}
Thank you in advance for your help.
Modify your onClick callback method in MyClick class like below:
#Override
public void onClick(View v) {
lv.performItemClick(lv.getChildAt(i), i, i);
}
Because p.getView(i, null, null) will inflate a new View that isn't the item in ListView, so you should use lv.getChildAt(i) to get the actual item.
Related
I want to set songs as a ringtone or alarm tone or message tone etc in list view by long pressing the list item. I want to show an dialog box which perform these action. How to show a dialog box by long pressing a list item and set action in dialog box? Please help me...
com.example.ring;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Naat extends Activity implements
OnItemClickListener {
private ListView listView;
public static MediaPlayer mp;
Button bt;
PopAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();
public static int[] rings = {R.raw.arabicmusic, R.raw.arabicmusic1, R.raw.arabicmusic12,
R.raw.arabicmusic13, R.raw.arabicmusic14, R.raw.arabicmusic15,
R.raw.arabicmusic18, R.raw.arabicmusic16, R.raw.arabicmusic17};
public static int imgadrss = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naat);
String[] dataArray = getResources().getStringArray(R.array.listdata);
List<String> dataTemp = Arrays.asList(dataArray);
dataItems.addAll(dataTemp);
listView = (ListView) findViewById(R.id.listnaat);
// bt = (Button) findViewById(R.id.button1);
adapter = new PopAdapter(Naat.this, dataItems);
// adapter.setCustomButtonListner(MainActivity.this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
// #Override
// public void onButtonClickListner(int position, String value) {
// mp.create(this, rings[position]);
// mp.start();
//
// Toast.makeText(MainActivity.this, "Button click " + value,
// Toast.LENGTH_SHORT).show();
//
// }
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "" + position, 2000).show();
imgadrss = rings[position];
}
}
Adapter class
com.example.ring;
import java.util.ArrayList;
import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class NaatAdapter extends ArrayAdapter<String> {
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClickListner(int position, String value);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
private Context context;
private ArrayList<String> data = new ArrayList<String>();
public NaatAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.singlerow, dataItem);
this.data = dataItem;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.singlerow, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.textView1);
viewHolder.button = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position, temp);
}
// MainActivity.mp.create(context, MainActivity.imgadrss);
// MainActivity.mp.start();
MediaPlayer mp = MediaPlayer.create(context, Naat.rings[position]);
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
}
} else {
if (mp != null) {
mp.start();
}
}
// mp.stop();
Toast.makeText(context, "" + position, 2000).show();
}
});
return convertView;
}
public class ViewHolder {
TextView text;
Button button;
}
}
You can use this
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// TODO Auto-generated method stub
Log.d("in onLongClick");
//Open alert dialog here
return true;
}
});
I managed to get Dialog to show up on the long click by implementing this code:
ListView list = getListView();
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
Unfortunately, when the dialog opens and prompts for new text input, when clicked ok, no changes are made to the list.
Here is the Dialog file:
package com.example.classorganizer;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
class EditListItemDialog extends Dialog implements View.OnClickListener {
private View editText;
public EditListItemDialog(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
}
#Override
public void onClick(View v) {
((TextView) editText).getText().toString();//here is your updated(or not updated) text
dismiss();
}
}
Here is the file containing list:
package com.example.classorganizer;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class Monday extends ListActivity {
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
ListView list = getListView();
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
}
public String title;
public String content;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
private class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
#Override
public int getCount() {return fragment_monday.size();}
public MyDiary getItem(int i) {return fragment_monday.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}
Here is an xml file for dialog:
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</RelativeLayout>
</LinearLayout>
I don't understand why the rows are not being updated with data entered through Dialog file...
#Override
public void onClick(View v) {
((TextView) editText).getText().toString();//here is your updated(or not updated) text
dismiss();
}
You are getting the value from TextView of the dialog ,have u added the data to the list.
Update the data to the list
fragment_monday.add(((TextView) editText).getText().toString());
Is it possible to have such functionality in android? When user longclicks row in a list popup window opens with option to edit content?
Yes it is. First of all you need to create your own Dialog with appropriate xml:
private static class EditListItemDialog extends Dialog implements View.OnClickListener {
private EditText editText;
public EditListItemDialog(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
Button btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
}
#Override
public void onClick(View v) {
editText.getText().toString();//here is your updated(or not updated) text
dismiss();
}
}
Then add listener at setOnLongClickListener:
ListView listView = new ListView(this);
listView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
new EditListItemDialog(v.getContext()).show();
return true;
}
});
Finally you need to update data in your ListView. The right way is to update Adapter of your list view which finally will update ListView.
Ok, I managed to make Dialog file as in your example:
package com.example.classorganizer;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
class EditListItemDialog extends Dialog implements View.OnClickListener {
private View editText;
public EditListItemDialog(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);
}
#Override
public void onClick(View v) {
((TextView) editText).getText().toString();//here is your updated(or not updated) text
dismiss();
}
}
I created an xml file for Dialog file:
<?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="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
Then I updated my Display List file with this code:
ListView listView = new ListView(Monday.this);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
And here is full file that displays list of rows to be edited upon Long Click:
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class Monday extends ListActivity {
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
}
public String title;
public String content;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
private class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
//new code below
ListView listView = new ListView(Monday.this);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
}); //end of new code
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
#Override
public int getCount() {return fragment_monday.size();}
public MyDiary getItem(int i) {return fragment_monday.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}
There you can see where I put the new code. Unfortunately, upon long click row is not being updated. I mean, the dialog file seems that is not being launched. Is it because I put the code in the wrong place or am I missing something else?
I STUCK. App is fairly simple but I can't get my brain working for this one. I just want to play sound when I press ListView Item. It could be easy, but I don't want to place sounds randomly. Sounds should be placed on the exact ListView Item location. Here what I have for now:
MainActivity.java:
package com.moomob.despicablemesounds;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import ca.demo.demolistview.complex.R;
public class MainActivity extends Activity {
private List<Minions> myMinions = new ArrayList<Minions>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateMinionsList();
populateListView();
registerClickCallback();
}
private void populateMinionsList() {
myMinions.add(new Minions("Assemble the Minions", R.drawable.min1));
myMinions.add(new Minions("Banana Minions", R.drawable.min2));
..................
..................
}
private void populateListView() {
ArrayAdapter<Minions> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.minListView);
list.setAdapter(adapter);
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.minListView);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
// Don't know what I should write in here
}
});
}
private class MyListAdapter extends ArrayAdapter<Minions> {
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, myMinions);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view,
parent, false);
}
// Find the minion to work with.
Minions currentCar = myMinions.get(position);
// Fill the view
ImageView imageView = (ImageView) itemView
.findViewById(R.id.item_icon);
imageView.setImageResource(currentCar.getIconID());
// Make:
TextView makeText = (TextView) itemView
.findViewById(R.id.item_txtMake);
makeText.setText(currentCar.getMake());
return itemView;
}
}
}
Minions.java (just to set up my constructor and getters and setters):
package com.moomob.despicablemesounds;
public class Minions {
private String make;
private int iconID;
public Minions(String make, int iconID) {
super();
this.make = make;
this.iconID = iconID;
}
public String getMake() {
return make;
}
public int getIconID() {
return iconID;
}
public void setMake(String make) {
this.make = make;
}
public void setIconID(int iconID) {
this.iconID = iconID;
}
}
Maybe I need some kind of SoundAdapter but don't know. Hope someone will understand what I want, because I absolutelly stuck with this one :(
You play a sound like this:
mp = MediaPlayer.create(Test.this, R.raw.mysound);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
But you will also need to know which sound blongs to which minion, so concider adding a property to your minion class.
In your onclick handler you have the position clicked, so you could get the clicked minion.
I came across a nice filterable listview tutorial and I was wondering how to determine what listview item is selected and display a toast. Here is the code:
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
public class TestFilterListView extends Activity {
FrameLayout historyContainer;
ViewStub viewStub;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_container);
historyContainer = (FrameLayout) findViewById(R.id.historyContainerLayout);
EditText filterEditText = (EditText) findViewById(R.id.filter_text);
filterEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
historyContainer.removeAllViews();
final List<String> tempHistoryList = new ArrayList<String>();
tempHistoryList.addAll(historyList);
for(String data : historyList) {
if(data.indexOf((s.toString())) == -1) {
tempHistoryList.remove(data);
}
}
viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
{
public void onInflate(ViewStub stub, View inflated)
{
setUIElements(inflated, tempHistoryList);
}
});
historyContainer.addView(viewStub);
viewStub.inflate();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
setViewStub();
}
/********************************************************************************************************/
private void setViewStub()
{
historyList.add("first");
historyList.add("second");
historyList.add("third");
historyList.add("fourth");
historyList.add("fifth");
historyList.add("sixth");
historyList.add("seventh");
viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
{
public void onInflate(ViewStub stub, View inflated)
{
setUIElements(inflated, historyList);
}
});
historyContainer.addView(viewStub);
viewStub.inflate();
}
/********************************************************************************************************/
final List<String> historyList = new ArrayList<String>();
String displayName = "";
ListView historyListView;
private void setUIElements(View v, List<String> historyLists)
{
if (v != null)
{
historyScheduleData.clear();
//historyList.clear();
historyScheduleData.addAll(historyLists);
historyListView = (ListView) findViewById(R.id.historylist);
historyListView.setAdapter(new BeatListAdapter(this));
registerForContextMenu(historyListView);
}
}
private static class BeatListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public BeatListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return historyScheduleData.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.history_list_view, null);
holder = new ViewHolder();
holder.historyData = (TextView) convertView
.findViewById(R.id.historytext);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.historyData.setText(historyScheduleData.get(position));
return convertView;
}
static class ViewHolder {
TextView historyData;
}
}
private static final List<String> historyScheduleData = new ArrayList<String>();
}
I thought about using
protected void onListItemClick(ListView l, View v, int position, long id) {
if(position == 0) {
Intent intent = new Intent(getApplicationContext(), GODoc1Activity.class);
startActivity(intent);
But when I try to implement it, it does not work. Am I missing something here? How do I go about doing this?
you tried this?: (in the onCreate)
historyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//your code
}
});
If you are trying to find the selected item in your ListView:
listView.getAdapter().getItem(listView.getCheckedItemPosition());
Take a look at the documentation here for more information http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPosition%28%29