How to show id and name in toast after clicking per button - android

I am unable to Toast every id and name after clicking button. Every time last id and name are showing after clicking different button. I want to show every list of data in Toast after clicking specific button
Activity
public class ListViewActivity extends AppCompatActivity {
ListView studentListView;
ArrayList<Student> studentList = new ArrayList<>();
StudentAdapter studentAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
studentListView = (ListView) findViewById(R.id.studentListView);
studentList.add(new Student(01, "Monir"));
studentList.add(new Student(02, "Jibon"));
studentList.add(new Student(03, "Mahmud"));
studentList.add(new Student(04, "Saiful"));
studentList.add(new Student(05, "Arif"));
studentAdapter = new StudentAdapter(this, studentList);
studentListView.setAdapter(studentAdapter);
}
public void viewStudentClick(View view) {
Toast.makeText(getApplicationContext(),studentAdapter.nameText.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),studentAdapter.idText.getText(), Toast.LENGTH_SHORT).show();
}
}
Adapter
public class StudentAdapter extends ArrayAdapter<Student> {
private Activity context;
private ArrayList<Student> students;
TextView idText;
TextView nameText;
public StudentAdapter(Activity context, ArrayList<Student> student) {
super(context, R.layout.student_info, student);
this.context = context;
this.students = student;
}
//Array Adapter
public View getView(int position, View rowView, ViewGroup parent){
LayoutInflater layoutInflater = context.getLayoutInflater();
rowView = layoutInflater.inflate(R.layout.student_info, parent, false);
idText = (TextView) rowView.findViewById(R.id.idText);
nameText = (TextView) rowView.findViewById(R.id.nameText);
idText.setText(students.get(position).getId()+"");
nameText.setText(String.valueOf(students.get(position).getName()));
return rowView;
}
}

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
studentListView = (ListView) findViewById(R.id.studentListView);
studentList.add(new Student(01, "Monir"));
studentList.add(new Student(02, "Jibon"));
studentList.add(new Student(03, "Mahmud"));
studentList.add(new Student(04, "Saiful"));
studentList.add(new Student(05, "Arif"));
studentAdapter = new StudentAdapter(this, studentList);
studentListView.setAdapter(studentAdapter);
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ListViewActivity.this,studentList.get(position).getId() +
studentList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
}

Use :
studentListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// selected item
String name = (String) parent.getItemAtPosition(position);
Toast toast = Toast.makeText(getApplicationContext(),"Position:"+position+", Name: "+name, Toast.LENGTH_SHORT).show();
}
});

Related

custom list view passing multiple data to next activity

i have done custom listview
it has 6 textview and 1 image view in each row
whati did is when press on item it just goes to my second activity
but what it want is to pass the items of the row to the second activity
i don't know how to do it or where to add it in the list or in the adapter
also what to do in the second activity
here is my first activity
public class ImageListActivity extends AppCompatActivity {
private DatabaseReference mDatabaseRef;
private List<ImageUpload> imgList;
private ListView lv;
private ImageListAdapter adapter;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_list);
imgList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listViewImage);
//Show progress dialog during list image loading
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait loading list image...");
progressDialog.show();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(ListActivity.FB_Database_Path);
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
progressDialog.dismiss();
//Fetch image data from firebase database
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//ImageUpload class require default constructor
ImageUpload img = snapshot.getValue(ImageUpload.class);
imgList.add(img);
}
//Init adapter
adapter = new ImageListAdapter(ImageListActivity.this, R.layout.image_item, imgList);
//Set adapter for listview
lv.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
startActivityForResult(myintent, 0);
}
});
}
}
and this is adapter
public class ImageListAdapter extends ArrayAdapter<ImageUpload> {
private Activity context;
private int resource;
private List<ImageUpload> listImage;
public ImageListAdapter(#NonNull Activity context, #LayoutRes int resource, #NonNull List<ImageUpload> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
listImage = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(resource, null);
TextView tvName = (TextView) v.findViewById(R.id.tvImageName);
TextView tvModel = (TextView) v.findViewById(R.id.tvImageModel);
TextView tvBrand = (TextView) v.findViewById(R.id.tvImageBrand);
TextView tvPrice = (TextView) v.findViewById(R.id.tvImagePrice);
TextView tvyear = (TextView) v.findViewById(R.id.tvImageYear);
TextView tvDesc = (TextView) v.findViewById(R.id.tvImageDesc);
ImageView img = (ImageView) v.findViewById(R.id.imgView);
tvModel.setText(listImage.get(position).getModel());
tvName.setText(listImage.get(position).getName());
tvBrand.setText(listImage.get(position).getBrand());
tvyear.setText(listImage.get(position).getYear());
tvDesc.setText(listImage.get(position).getDesc());
tvPrice.setText(listImage.get(position).getPrice());
Glide.with(context).load(listImage.get(position).getUrl()).into(img);
return v;
}
}
What I understand, you want to transfer the entire object namely ImageUpload of the list item you clicked on. For this you can create ImageUpload model class as Parcelable and and then pass selected ImageUpload object in your intent and retrieve it back from Intent using getParcelable().
on list item click:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
myintent.putExtra("selected_image_upload", ((ImageUpload) adapter.getItem(position)));
startActivityForResult(myintent, 0);
}
});
on Buy1Activity, to get the selected ImageUpload object in onCreate():
ImageUpload imgUploadObj = getIntent().getExtras().getParcelable("selected_image_upload");
You can get hint from here to create a class Parcelable.
Try this
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageUpload img = adapter.get(position);
Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
myintent.putParcelableArrayListExtra("parcel_data", img);
startActivityForResult(myintent, 0);
}
});
In your Buy1Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageUpload img = (ImageUpload) getIntent().getParcelableArrayListExtra("parcel_data");
}
Hope it help!

how to remove custom listview item in android

i want to remove listview item in my list.. But When I click On Button nothing happened. please tell ME Where I m Doing Wrong...this code can add item but only remove item cant...
//listview java
public class MemberActivity extends AppCompatActivity implements BaseColumns {
ListView mylist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member);
final ListView listview;
final ListViewAdapter adapter;
//final ArrayList<String> items = new ArrayList<String>();
mylist = (ListView) findViewById(R.id.listview1);
final LinearLayout linewarLayout1 = (LinearLayout) findViewById(R.id.addmember);
final LinearLayout linewarLayout2 = (LinearLayout) findViewById(R.id.buttongroup);
adapter = new ListViewAdapter();
listview = (ListView) findViewById(R.id.listview1);
final View header = getLayoutInflater().inflate(R.layout.listview_header, null, false);
listview.setAdapter(adapter);
Button addButton = (Button) findViewById(R.id.add);
addButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mylist.setVisibility(View.INVISIBLE);
linewarLayout2.setVisibility(View.INVISIBLE);
linewarLayout1.setVisibility(View.VISIBLE);
}
});
final EditText name = ((EditText) findViewById(R.id.etName));
final EditText ID = ((EditText) findViewById(R.id.etID));
final EditText Major = ((EditText) findViewById(R.id.etMajor));
Button btnDone = (Button) findViewById(R.id.btnDone);
btnDone.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//TextView cnttxt = (TextView)findViewById(R.id.count);
//cnttxt.setText(adapter.getCount());
adapter.addItem(name.getText().toString(), ID.getText().toString(), Major.getText().toString());
name.setText("");
ID.setText("");
Major.setText("");
Toast.makeText(getApplicationContext(), "add.", Toast.LENGTH_LONG).show();
mylist.setVisibility(View.VISIBLE);
linewarLayout2.setVisibility(View.VISIBLE);
linewarLayout1.setVisibility(View.INVISIBLE);
}
});
Button btnCancel = (Button) findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
name.setText("");
ID.setText("");
Major.setText("");
mylist.setVisibility(View.VISIBLE);
linewarLayout2.setVisibility(View.VISIBLE);
linewarLayout1.setVisibility(View.INVISIBLE);
}
});
// delete button
Button deleteButton = (Button) findViewById(R.id.delete);
deleteButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int count, checked;
count = adapter.getCount();
if (count > 0) {
listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
checked = listview.getCheckedItemPosition();
if (checked > -1 && checked < count) {
adapter.removeitem(checked);
listview.clearChoices();
adapter.notifyDataSetChanged();
}
}
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
// get item
ListViewItem item = (ListViewItem) parent.getItemAtPosition(position);
String nameStr = item.getname();
String IDStr = item.getID();
String majorStr = item.getmajor();
}
});
}
}//adapter
public class ListViewAdapter extends BaseAdapter {
private ArrayList<ListViewItem> listViewItemList = new ArrayList<ListViewItem>();
public ListViewAdapter() {
}
#Override
public int getCount() {
return listViewItemList.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
final Context context = parent.getContext();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_item, parent, false);
}
TextView nameTextView = (TextView) convertView.findViewById(R.id.list_name);
TextView IDTextView = (TextView) convertView.findViewById(R.id.list_ID);
TextView majorTextView = (TextView) convertView.findViewById(R.id.list_major);
ListViewItem listViewItem = listViewItemList.get(position);
nameTextView.setText(listViewItem.getname());
IDTextView.setText(listViewItem.getID());
majorTextView.setText(listViewItem.getmajor());
return convertView;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return listViewItemList.get(position);
}
public void removeitem(int position) {
listViewItemList.remove(position);
notifyDataSetChanged();
}
public void addItem(String name, String ID, String major) {
ListViewItem item = new ListViewItem();
item.setname(name);
item.setID(ID);
item.setmajor(major);
listViewItemList.add(item);
notifyDataSetChanged();
}
}
//ListViewItem java
public class ListViewItem {
private String nameStr;
private String IDStr;
private String majorStr;
public void setname(String name) {
nameStr = name;
}
public void setID(String ID) {
IDStr = ID;
}
public void setmajor(String major) {
majorStr = major;
}
public String getname() {
return this.nameStr;
}
public String getID() {
return this.IDStr;
}
public String getmajor() {
return this.majorStr;
}
}
Update your code as below. Set choice mode to CHOICE_MODE_SINGLE when you crate listview and before set adapter. Remove CHOICE_MODE_SINGLE from button click.
listview = (ListView) findViewById(R.id.listview1);
listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listview.setAdapter(adapter);
Also, You need to setItemChecked in onItemClick of listview.
Check below example code.
public class MainActivity extends Activity {
private ListView mListView;
private String[] mData = new String[] { "xxx", "yyy", "zzz", "aaa" };
private BaseAdapter mAdapter;
private int mLastCorrectPosition = -1;
private int mButtonPosition = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, mData);
mListView.setAdapter(mAdapter);
mListView.setSelector(new ColorDrawable(0));
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == mButtonPosition) {
if (mLastCorrectPosition != -1) {
mListView.setItemChecked(mLastCorrectPosition, true);
}
else {
mListView.setItemChecked(mButtonPosition, false);
}
// here show dialog
}
else {
mLastCorrectPosition = position;
// here refresh fragment
}
}
});
}
}

Adding item to listview through a dialog

I'm using a custom listview which I want to add an item to my listview. But it seems to be skipping all the code to add the item. Can someone please tell me how I should adjust my code to achieve this.
Thank you in advance.
This is my Main Activity in which I call a custom Dialog
public class MainActivity extends ActionBarActivity {
TextView threadId;
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button newMessage = (Button) findViewById(R.id.new_message_button);
final Context context = this;
final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.MessageFeedData());
listView.setAdapter(itemAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
listView.getAdapter().getItem(position);
MessageItem itemAtPos = (MessageItem) parent.getItemAtPosition(position);
Intent intent = new Intent(MainActivity.this, ConversationView.class);
intent.putExtra("threadId", String.valueOf(itemAtPos.ThreadId));
startActivity(intent);
}
});
newMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.write_message_layout);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
final Button post_button = (Button) dialog.findViewById(R.id.button_post);
final EditText new_write_message = (EditText) dialog.findViewById(R.id.messge_msg);
final EditText to_message = (EditText) dialog.findViewById(R.id.to_newmsg);
post_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items.add(new MessageItem(5458, to_message.getText().toString(), "imh", DateTime.now(), new_write_message.getText().toString()));
itemAdapter.notifyDataSetChanged();
if (v.getId() == R.id.button_post);
to_message.setText("");
new_write_message.setText("");
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
.show();
}
});
dialog.show();
}
});
}
public ArrayList<MessageItem> MessageFeedData() {
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
//recieved_item_click_actions fields
items.add(new MessageItem(1, "Bob Doe", "image", DateTime.now(), "Hello how are you?"));
items.add(new MessageItem(200, "Simon Pink", "image", DateTime.now(), "Hello what are you doing"));
return items;
}
class ActivityFeedTask extends AsyncTask<Integer, Void, Void> {
ArrayList<MessageItem> recentTracks;
#Override
protected Void doInBackground(Integer... page) {
try {
recentTracks = new ArrayList<MessageItem>();
Thread.sleep(3000);
MessageItem data = null;
for (int i = 0; i < 10; i++) {
recentTracks.add(data);
}
} catch (Exception e) {
}
return null;
}
}
public class ActivityAdapter extends ArrayAdapter<MessageItem> {
private final Context context;
private final ArrayList<MessageItem> items;
//private int currentPage = 0;
public ActivityAdapter(Context context, ArrayList<MessageItem> recentTrackArrayList) {
super(context, 0, recentTrackArrayList);
this.context = context;
this.items = recentTrackArrayList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = getLayoutInflater().inflate
(R.layout.message_list_item, parent, false);
rowView = convertView;
TextView comment2 = (TextView) rowView
.findViewById(R.id.messaging_username);
comment2.setText(items.get(position).Username);
ImageView comment3 = (ImageView) rowView
.findViewById(R.id.messaging_photo);
if (items.get(position).Image == null) {
comment3.setImageResource(R.drawable.ic_launcher);
}
TextView comment4 = (TextView) rowView
.findViewById(R.id.messaging_date);
comment4.setText(items.get(position).DateTimeStamp.toString());
TextView comment5 = (TextView) rowView
.findViewById(R.id.messaging_string);
comment5.setText(items.get(position).MessageString);
}
return convertView;
}
}
}
This is my custom Dialog
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/to_newmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TO" />
<EditText
android:id="#+id/messge_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/to_newmsg"
android:text="MESSAGE" />
<Button
android:id="#+id/button_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messge_msg"
android:text="Send" />
</RelativeLayout>
You have this
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
before onCreate
while this
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
in MessageFeedData() is local.
Use a single list and add items to the same then call notifyDateSetChanged() to refresh listview.
As you can see you do not use items as an arg to constructor
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.MessageFeedData());
Edit:
final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
items = this.MessageFeedData());
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), items);

How to get a String-Array of the whole ListView

I have a ListView with dynamically added TextViews and how do I get all TextView objects of the ListView in a String-Array ?
For testing I would like to display the whole String-Array in my Toast in the onItemClickmethod
onCreate:
final ListView sessionLv = (ListView) findViewById(R.id.session_list);
sessionLv.setAdapter(sessionAdapter);
sessionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView c = (TextView) view.findViewById(R.id.stepTime);
String playerChanged = c.getText().toString();
Toast toast = Toast.makeText(getApplicationContext(),
playerChanged +" wurde angeklickt!",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
The adapter for the dynamically added TextView objects in my ListView
class SessionItemAdapter extends ArrayAdapter<Map> {
ArrayList<Map> values;
Context context;
SessionItemAdapter(Context context, ArrayList<Map> values) {
super(context, R.layout.session_list_layout_row, values);
this.values = values;
this.context = context;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.session_list_layout_row, parent, false);
TextView tv = (TextView) rowView.findViewById(R.id.stepTime);
final int rowId = (Integer) values.get(position).get("id");
int beepStringId = getResources().getIdentifier("exercise_typ_" + values.get(position).get("type"), "string", getPackageName());
rowView.setTag(rowId);
tv.setText(getResources().getString(beepStringId));
ImageButton deleteSession = (ImageButton) rowView.findViewById(R.id.session_item_del);
deleteSession.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*if (!startStopToggle.isChecked()) {*/
sessionAdapter.remove(values.get(position));
sessionAdapter.notifyDataSetChanged();
}
});
return rowView;
}
Hope this will help,
//in your item click
sessionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SessionItemAdapter adapter = (SessionItemAdapter) parent.getAdapter();
List<String> allPlayers = adapter.getAllPlayers();
// Log your list or whatever you want to do with it
Log.i(TAG, "allPlayers: " + allPlayers.toString());
}
});
//in your adapter getAllPlayers() would be something like this
public List<String> getAllPlayers(){
List<String> list=new ArrayList<String>();
for (Map item : values) {
int beepStringId = context.getResources().getIdentifier("exercise_typ_"
+ item.get("type"), "string", context.getPackageName());
String str = context.getResources().getString(beepStringId);
list.add(str);
}
return list;
}

Add item in listview arrayadapter

Please tell me how to add items in listview arrayadapter?
I found only how to make for standard adapter
Activity:
public class Activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m);
ListView lv=(ListView) findViewById(R.id.lv);
String[] Id1={"1","2","3"}, Text1={"one","two","three"};
CustomAdapter ad = new CustomAdapter(this, Id1 , Text1);
ad.setCustomListener(new LVListener() {
public void onClick(String text) {
Log.d("APP", text);
}
});
lv.setAdapter(ad);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setVisibility(View.GONE);
String[] Id2={"4","5","6"},Text2={"four","five","six"};
// add Id2 and Text2 in listview
}
});
}
}
CustomAdapter:
public class CustomAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] id, text;
private ListViewListener micl;
public CustomAdapter(Context context, String[] id, String[] text) {
super(context, R.layout.list, id);
this.context = context;
this.id = id;
this.text = text;
}
public void setCustomListener(ListViewListener micl) { this.micl = micl; }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View View = inflater.inflate(R.layout.list, parent, false);
final int pos = position;
final TextView tView = (TextView) View.findViewById(R.id.textView);
tView.setText(text[pos]);
rowView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (micl != null)
micl.onClick(text[pos]);
}
});
return View;
}
}
I tried to do with notifyDatasetChanged() but nothing happened.
Please tell me how to do that.
ArrayAdapter has the add method, but in order to use it the dataset you provide to the super can not be an array, that's because the using Arrays.asList(objects), that returns an immutable list. From the documentation
Returns a List of the objects in the specified array. The size of the
List cannot be modified, i.e. adding and removing are unsupported, but
the elements can be set. Setting an element modifies the underlying
array.

Categories

Resources