checkbox in listview to select and delete - android

This is my code I have one list view in the fragment which read sms using cursor I have set check box in adapter and I have 3 buttons outside the list view in main layout I need the followings
1.When I click one button it display checkbox in the list
2.when I select and scroll the list selected items wont disappears
3.select all button to select all in the list 4.delete button to delete the selected item remaining messages instantly updated.
public class TabFragment extends Fragment
{
ListView mlistView;
ArrayList<Message> sms = new ArrayList<Message>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_sms, container, false);
mlistView = (ListView) view.findViewById(R.id.SMSList);
Button sdel=(Button)view.findViewById(R.id.sdelete);
populateMessageList();
sdel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
public void populateMessageList() {
fetchInboxMessages();
if (fetchInboxMessages() != null) {
mlistView.setAdapter(new datalist(getActivity().getApplicationContext(),this));
}
}
public ArrayList<Message> fetchInboxMessages() {
Uri muriSms = Uri.parse("content://sms/inbox");
Cursor mcursor = getActivity().getContentResolver().query(muriSms,
new String[] { "_id", "address", "date", "body" }, null, null,
null);
mcursor.moveToFirst();
while (mcursor.moveToNext()) {
Message mMessage = new Message();
mMessage.setmAddress(mcursor.getString(mcursor
.getColumnIndex("address")));
mMessage.setmBody(mcursor.getString(mcursor.getColumnIndex("body")));
mMessage.setmDate(mcursor.getString(mcursor.getColumnIndex("date")));
sms.add(mMessage);
}
return sms;
}
class datalist extends BaseAdapter {
LayoutInflater inflater = null;
Boolean selected;
Boolean[] checkboxstate;
public datalist(Context applicationContext, TabFragment tabFragment) {
inflater = LayoutInflater.from(applicationContext);
} public datalist(Context applicationContext, int simple_list_item_multiple_choice, ArrayList<Message> sms) {
}
#Override
public int getCount() {
return sms.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row;
row = inflater.inflate(R.layout.sms_adapter, parent, false);
ImageView img1 = (ImageView) row.findViewById(R.id.icon2);
TextView txt1 = (TextView) row.findViewById(R.id.text1);
TextView txt2 = (TextView) row.findViewById(R.id.text2);
CheckBox cb=(CheckBox)row.findViewById(R.id.check);
Long timestamp = Long.parseLong(sms.get(position).getmDate());
Calendar mcalendar = Calendar.getInstance();
mcalendar.setTimeInMillis(timestamp);
DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
txt1.setText(sms.get(position).getmBody());
txt2.setText("Sent by" + sms.get(position).getmAddress() + "\n" + mformatter.format(mcalendar.getTime()));
/* if(selected==false)
cb.setVisibility(row.GONE);
else*/
cb.setVisibility(row.VISIBLE);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sms.get(position).setSelected(isChecked);
if(isChecked){
buttonView.setSelected(true);
}else {
buttonView.setSelected(false);
}
}
});
return row;
}
}
public class Message {
private String mAddress;
private String mBody;
private String mDate;
private int icon;
boolean selected;
public void setmAddress(String mAddress) {
this.mAddress = mAddress;
}
public void setmBody(String mBody) {
this.mBody = mBody;
}
public void setmDate(String mDate) {
this.mDate = mDate;
}
public String getmBody() {
return mBody;
}
public String getmAddress() {
return mAddress;
}
public String getmDate() {
return mDate;
}
public boolean isSelected() {
return selected;
}
public int getIcon(){
return icon;} public void setSelected(boolean selected) {
this.selected = selected;
}}}

Related

Apply Listener on some ListView columns

Background:
I have created a ListView with three columns sNo, product and price. First column is defined as TextView (whose value is auto generated) and the next two columns are EditText (whose value is filled up by the user).
What I want:
I want to add a new row to the ListView whenever:
User hit enter key on any EditText
There is no empty EditText (meaning all the EditText defined so far have some value in them).
Basically I want display a new orders list where users can add orders.
My code so far:
ListView Model:
public class NewTableModel {
private String sNo, product, price;
public NewTableModel(String sNo, String product, String price){
this.sNo = sNo;
this.product = product;
this.price = price;
}
public String getProduct(){ return product; }
public String getPrice(){ return price; }
public String getsNo() { return sNo; }
}
ListView adapter:
public class NewTableAdapter extends BaseAdapter {
private ArrayList<NewTableModel> productList;
private Activity activity;
public NewTableAdapter(Activity activity, ArrayList<NewTableModel> productList) {
super();
this.activity = activity;
this.productList = productList;
}
#Override
public int getCount() { return productList.size(); }
#Override
public Object getItem(int position) { return productList.get(position); }
#Override
public long getItemId(int position) { return position; }
public class ViewHolder {
TextView mSno;
EditText mProduct;
EditText mPrice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.new_table_row, null);
holder = new ViewHolder();
holder.mSno = (TextView) convertView.findViewById(R.id.sno);
holder.mProduct = (EditText) convertView.findViewById(R.id.product);
holder.mPrice = (EditText) convertView.findViewById(R.id.price);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
NewTableModel item = productList.get(position);
holder.mSno.setText(item.getsNo());
holder.mProduct.setText(item.getProduct());
holder.mPrice.setText(String.valueOf(item.getPrice()));
return convertView;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private ArrayList<NewTableModel> productList;
private ListView orderView;
private NewTableAdapter orderAdapter;
private void insertNewRow(){ insertNewRow("",""); }
private void insertNewRow(String productVal, String priceVal){
String serialNoVal = String.valueOf(orderView.getCount() + 1);
NewTableModel item = new NewTableModel(serialNoVal, productVal, priceVal);
productList.add(item);
}
private void setupAdapter(){
productList = new ArrayList<NewTableModel>();
orderView = (ListView) findViewById(R.id.newTableContent);
orderAdapter = new NewTableAdapter(this, productList);
orderView.setAdapter(orderAdapter);
orderAdapter.notifyDataSetChanged();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
setupAdapter();
insertNewRow();
}
}
My Listener:
setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER
&& noEmptyColumn())
insertNewRow();
return false;
}
});
Where should I place that listener ? and how would I check if any column is empty or not (define noEmptyColumn()) ?
You should place the listener where any of EditText values are changed. I would add a Button to any row, and set the listener at there. So in your ViewHolder:
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean hasEmpty = false;
for (NewTableModel item: productList) {
if (item.getDesiredField().isEmpty()) {
hasEmpty = true;
break;
}
}
if (!hasEmpty) {
insertNewRow();
notifyDataSetChanged();
}
}
});
Another option could be setting a TextWatcher on EditText :
ed.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
boolean hasEmpty = false;
for (NewTableModel item: productList) {
if (item.getDesiredField().isEmpty()) {
hasEmpty = true;
break;
}
}
if (!hasEmpty) {
insertNewRow();
notifyDataSetChanged();
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Just move both methods to your Adapter class. And note that the second solution is not efficient when there are too many rows.

How to store the position of the items in list view with checkbox checked into the bundle?

I have a listview and each row I have a checkbox. I have a button to show the items in listview being selected when it is clicked. My problem is that when orientation changes, all the selection will be gone. I know I need to store the position of items in the listview with the checkbox checked into the bundle but I do not know how to do it. Can someone help me?
Here is my main activity.
public class MainActivity extends AppCompatActivity {
ListView mainActivity;
// creating arraylist of MyItem type to set to adapter
ArrayList<MyItem> myItems = new ArrayList<>();
Button button;
MyCustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = (ListView) findViewById(R.id.mainactivitylistview);
myItems.add(new MyItem("Greece", "Athens",R.drawable.amsterdam_icon,false));
myItems.add(new MyItem("Emirate of Dubai","Dubai",R.drawable.dubai_icon,false));
myItems.add(new MyItem("Netherlands","Amsterdam",R.drawable.amsterdam_icon,false));
myItems.add(new MyItem("England","London",R.drawable.london_icon,false));
myItems.add(new MyItem("Egypt","Giza",R.drawable.egypt_icon,false));
myItems.add(new MyItem("France","Paris",R.drawable.paris_icon,false));
myItems.add(new MyItem("Russia","Moscow",R.drawable.moscow_icon,false));
myItems.add(new MyItem("Italy","Florence",R.drawable.florence_icon,false));
myItems.add(new MyItem("Switzerland","Zurich",R.drawable.zurich_icon,false));
myItems.add(new MyItem("Japan","Tokyo",R.drawable.tokyo_icon,false));
//Creating Adapter object for setting to list
adapter = new MyCustomAdapter(myItems, MainActivity.this);
mainActivity.setAdapter(adapter);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Your Wishlist");
builder.setMessage(adapter.getWishlist().toString().replace("[","").replace("]","").replace(",",""));
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}}
Here is my adapter.
public class MyCustomAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<MyItem> myList= new ArrayList<>();
private ArrayList<String> wishList = new ArrayList<>();
private ArrayList<Integer> positionList = new ArrayList<>();
public MyCustomAdapter(ArrayList<MyItem> itemArray,Context mContext) {
super();
this.mContext = mContext;
myList=itemArray;
}
#Override
public int getCount() {
return myList.size();
}
#Override
public String getItem(int position) {
return myList.get(position).toString();
}
#Override
public long getItemId(int position) {
return position;
}
public ArrayList<String> getWishlist() {
return wishList;
}
public ArrayList<Integer> getPositionList() {
return positionList;
}
public class ViewHolder {
public TextView name_country;
public TextView name_city;
public CheckBox checkBox;
public ImageView imageView;
}
#Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view = null;
LayoutInflater inflator = ((Activity) mContext).getLayoutInflater();
if (view == null) {
view = new ViewHolder();
convertView = inflator.inflate( R.layout.myadapter, null);
view.name_city = (TextView) convertView.findViewById(R.id.name_city);
view.name_country=(TextView) convertView.findViewById(R.id.name_country);
view.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
view.imageView = (ImageView) convertView.findViewById(R.id.imageView);
view.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here
// we get the position that we have set for the checkbox using setTag.
myList.get(getPosition).setChecked(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
if (isChecked) {
wishList.add(myList.get(position).getName_city() + "\n");
positionList.add(position);
}
else
{
wishList.remove(myList.get(position).getName_city() + "\n");
positionList.remove(position);
}
}
});
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.imageView.setImageResource(myList.get(position).getImgId());
view.checkBox.setTag(position);
view.name_country.setText(myList.get(position).getName_country());
view.name_city.setText(myList.get(position).getName_city());
view.checkBox.setChecked(myList.get(position).isChecked());
return convertView;
}}
Here is MyItem.java.
public class MyItem {
private String name_country;
private String name_city;
private int ImgId;
private boolean checked;
public MyItem(String name_country, String name_city, int imgId, boolean checked) {
this.name_country = name_country;
this.name_city = name_city;
ImgId = imgId;
this.checked = checked;
}
public String getName_country() {
return name_country;
}
public void setName_country(String name_country) {
this.name_country = name_country;
}
public String getName_city() {
return name_city;
}
public void setName_city(String name_city) {
this.name_city = name_city;
}
public int getImgId() {
return ImgId;
}
public void setImgId(int imgId) {
ImgId = imgId;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}}

android rating bar in listview:How to save rating of multiple list in object?

I am designing faculty feedback activity.Below is my Fragment activity where i get faculty names from webservice as object and dispaly them in listview.And there is Rating bar infront every faculty.
I want to collect value of rating and faculty name in array of object and want to send feedback of every faculty to webservice to store when i click submit button.How can i do it?
FragmentTab2.java
public class TabFragment2 extends android.support.v4.app.Fragment implements View.OnClickListener {
ListView FacultyList;
View rootView;
LinearLayout courseEmptyLayout;
FacultyListAdapter facultyListAdapter;
Button upButton;
String feedbackresult,programtype,programname;
Boolean FeedBackResponse;
String FacultiesList[];
public ArrayList<Faculty> facultylist = new ArrayList<Faculty>();
SharedPreferences pref;
FacultyListAdapter adapter;
SessionSetting session;
public TabFragment2(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref = getActivity().getSharedPreferences("prefbook", getActivity().MODE_PRIVATE);
programtype = pref.getString("programtype", "NOTHINGpref");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_studenttab2, container, false);
upButton = (Button) rootView.findViewById(R.id.btnSubmit);
session = new SessionSetting(getActivity());
new FacultySyncerBg().execute("");
courseEmptyLayout = (LinearLayout) rootView.findViewById(R.id.feedback_empty_layout);
FacultyList = (ListView) rootView.findViewById(R.id.feedback_list);
facultyListAdapter = new FacultyListAdapter(getActivity());
FacultyList.setEmptyView(rootView.findViewById(R.id.feedback_list));
FacultyList.setAdapter(facultyListAdapter);
return rootView;
}
//FEEDBACK SUBMISSION BUTTON
#Override
public void onClick(View v) {
new SendFeedbackSyncerBg().execute("");
}
public class FacultyListAdapter extends BaseAdapter {
private final Context context;
public FacultyListAdapter(Context context) {
this.context = context;
if (!facultylist.isEmpty())
courseEmptyLayout.setVisibility(LinearLayout.GONE);
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder TabviewHolder;
if (convertView == null) {
TabviewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_feedback,
parent, false);
TabviewHolder.FacultyName = (TextView) convertView.findViewById(R.id.FacultyName);//facultyname
TabviewHolder.rating = (RatingBar) convertView.findViewById(R.id.rating);//rating starts
TabviewHolder.Submit = (Button) convertView.findViewById(R.id.btnSubmit);
// Save the holder with the view
convertView.setTag(TabviewHolder);
} else {
TabviewHolder = (ViewHolder) convertView.getTag();
}
final Faculty mFac = facultylist.get(position);//*****************************NOTICE
TabviewHolder.FacultyName.setText(mFac.getEmployeename());
TabviewHolder.rating.setRating(mFac.getRatingStar());
// TabviewHolder.ModuleName.setText(mFac.getSubject());
TabviewHolder.rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
feedbackresult =String.valueOf(rating);
TabviewHolder.rating.setRating(Float.parseFloat(feedbackresult));
Log.d("feedback","feedback is: "+ feedbackresult);
}
});
return convertView;
}
/*private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position) {
return new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
FacultyName item = getItem(position);
item.setRatingStar(v);
Log.i("Adapter", "star: " + v);
}
};
}*/
#Override
public int getCount() {
return facultylist.size();
}
#Override
public Object getItem(int position) {return facultylist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}
static class ViewHolder {
TextView FacultyName;
RatingBar rating;
Button Submit;
}
private class FacultySyncerBg extends AsyncTask<String, Integer, Void> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(getActivity(), "Faculty Feedback!","Fetching Faculty List", true);
}
#Override
protected Void doInBackground(String... params) {
//CALLING WEBSERVICE
Faculty(programtype);
return null;
}
#Override
protected void onPostExecute(Void result) {
if (!facultylist.isEmpty()) {
// FacultyList.setVisibiltity(View.VISIBLE) ;
courseEmptyLayout.setVisibility(LinearLayout.GONE);
if (FacultyList.getAdapter() != null)
{
if (FacultyList.getAdapter().getCount() == 0)
{
FacultyList.setAdapter(facultyListAdapter);
}
else
{
facultyListAdapter.notifyDataSetChanged();
}
}
else
{
FacultyList.setAdapter(facultyListAdapter);
}
}else
{
courseEmptyLayout.setVisibility(LinearLayout.VISIBLE);
// FacultyList.setVisibiltity(View.GONE) ;
}
progressDialog.dismiss();
}
}
And here is my object class
Faculty.java
public class Faculty {
private float ratingStar;
String employeeid;
String employeename;
//public List<Faculty> facultylist= new ArrayList<>();
public Faculty()
{
}
public Faculty(int ratingStar,String employeeid,String employeename)
{
this.ratingStar = ratingStar;
this.employeeid =employeeid;
this.employeename =employeename;
}
public float getRatingStar() {
return ratingStar;
}
public void setRatingStar(float ratingStar) {
this.ratingStar = ratingStar;
}
public void setEmployeeid(String employeeid)
{
this.employeeid = employeeid;
}
public String getEmployeeid()
{
return this.employeeid;
}
public void setEmployeename(String employeename)
{
this.employeename = employeename;
}
public String getEmployeename()
{
return this.employeename;
}
}
Any kind of knowledge and information will be thankful
EDIT
My list of ratings look like this.
Image

i have an issue with checked textview

i have used checked textview with base adapter to fill listview it's working fine but when i try to scroll CheckedTextView lost the selection.please find the code bellow and help me.
public class AttendancePage extends AppCompatActivity {
List<String> studentNames = new ArrayList<String>();
String[] sNames;
DatabaseHelper databaseHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance_page);
databaseHelper = new DatabaseHelper(getApplicationContext());
Cursor res = databaseHelper.getStudentNames();
setTitle("Attendance Sheet");
ListView listView = (ListView) findViewById(R.id.listView);
while (res.moveToNext()) {
studentNames.add(res.getString(0));
}
sNames = new String[studentNames.size()];
sNames = studentNames.toArray(sNames);
Student_Attandence_Addapter customAdapter = new Student_Attandence_Addapter(getApplicationContext(), sNames);
listView.setAdapter(customAdapter);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
My custom Adapter class:
public class Student_Attandence_Adapter extends BaseAdapter {
String[] names;
Context context;
LayoutInflater inflter;
String value;
public Student_Attandence_Adapter(Context context, String[] names) {
this.context = context;
this.names = names;
inflter = (LayoutInflater.from(context));
}
#Override
public int getCount() {
return names.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
view = inflter.inflate(R.layout.student_attandence_listview, null);
final CheckedTextView simpleCheckedTextView = (CheckedTextView) view.findViewById(R.id.simpleCheckedTextView);
simpleCheckedTextView.setText(names[position]);
simpleCheckedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (simpleCheckedTextView.isChecked()) {
value = "un-Checked";
simpleCheckedTextView.setCheckMarkDrawable(0);
simpleCheckedTextView.setChecked(false);
} else {
value = "Checked";
simpleCheckedTextView.setCheckMarkDrawable(R.drawable.checked);
simpleCheckedTextView.setChecked(true);
}
}
});
return view;
}
}
Basically what happens is the state of the ChekedTextView will be reset whenever the getView method will be called according to the previous cached state of the list item. So in short you need to store the checked state of an item and when the getView method will be called you need to set it again. For example you need an object containing name and checked state
public class Student {
private String name;
private boolean checked;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public boolean isChecked() {
return checked;
}
}
and your getView method will be like this,
#Override
public View getView(int position, View view, ViewGroup parent) {
/*
* ListView caches the view so only inflate when there
* is no cached view aka null
*/
if (view == null) {
view = inflter.inflate(R.layout.student_attandence_listview, null);
}
Student aStudent = students[position];
final CheckedTextView simpleCheckedTextView = (CheckedTextView) view.findViewById(R.id.simpleCheckedTextView);
simpleCheckedTextView.setText(aStudent.getName());
simpleCheckedTextView.setCheckMarkDrawable(aStudent.isChecked() ? R.drawable.checked : 0);
simpleCheckedTextView.setChecked(aStudent.isChecked());
simpleCheckedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (simpleCheckedTextView.isChecked()) {
aStudent.setChecked(false);
notifyDataSetChanged();
} else {
aStudent.setChecked(true);
notifyDataSetChanged();
}
}
});
return view;
}
That's the theme. Modify it as you need.

Unknown error while trying startActivity from BaseAdapter

I am doing custom ListView in android and I have a share button on each item. After clicking button it must create chooser(facebook, etc). But when I click it the app crashes and logcat shows nothing. Please help me
Here is my Adapter:
public class AdapterQ extends BaseAdapter {
List<Quotes> sss;
Context ss;
Intent sendIntent;
public AdapterQ(Context ss, List<Quotes>items){
this.ss = ss;
this.sss = items;
}
#Override
public int getCount() {
return sss.size();
}
#Override
public Object getItem(int position) {
return sss.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
ss.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.quoteitem, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.legendImage);
final TextView tvTitle = (TextView) convertView.findViewById(R.id.legendName);
ImageView ssss= (ImageView) convertView.findViewById(R.id.Nation);
final Quotes quotes = sss.get(position);
imgIcon.setImageResource(quotes.getIcon());
tvTitle.setText(quotes.getText());
ssss.setImageResource(R.mipmap.ic_share_black_24dp);
ssss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendIntent =new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, tvTitle.getText());
sendIntent.setType("text/plain");
ss.startActivity(Intent.createChooser(sendIntent, "Поделиться..."));
}
});
return convertView;
}
}
Quotes:
public class Quotes {
private String mText;
private int mIcon;
public String getText() {
return mText;
}
public void setText(String mText) {
this.mText = mText;
}
public int getIcon() {
return mIcon;
}
public void setIcon(int mIcon) {
this.mIcon = mIcon;
}
}
Fragment:
public class AllFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rrrr = inflater.inflate(R.layout.fragment_all, container, false);
String[] ss = getResources().getStringArray(R.array.Абай);
String[] qq = getResources().getStringArray(R.array.Абылай);
String[] sr = new String[ss.length + qq.length];
System.arraycopy(ss, 0, sr, 0, ss.length);
System.arraycopy(qq, 0, sr, ss.length, qq.length);
ListView se = (ListView) rrrr.findViewById(R.id.listView2);
shuffleArray(sr);
List<Quotes> ser = new ArrayList<Quotes>();
for (int s = 0; s<sr.length; s++){
if(Arrays.asList(ss).contains(sr[s]) ){
Quotes quotes = new Quotes();
quotes.setText(sr[s]);
quotes.setIcon(R.drawable.abay);
ser.add(quotes);
} else if(Arrays.asList(qq).contains(sr[s])){
Quotes quotes = new Quotes();
quotes.setText(sr[s]);
quotes.setIcon(R.drawable.abylai);
ser.add(quotes);
}
}
AdapterQ ses = new AdapterQ(getActivity().getApplicationContext(), ser);
se.setAdapter(ses);
return rrrr;
}
Here:
AdapterQ ses = new AdapterQ(getActivity().getApplicationContext(), ser);
Try this:
AdapterQ ses = new AdapterQ(getActivity(), ser);

Categories

Resources