application not working in API 21 - android

I have an application which has a gridview with images in it. The problem is its showing in all phones except for API 21 i.e. Lollipop version. In Lollipop its shows just background and no gridview. The targetSDK is 21 and minSDK is 8. Any help in this would be much appreciated.
MainActivity
public class MainView extends Activity {
ImageView back, home, site, header;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
setContentView(R.layout.mainview);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent myIntent = null;
if(position == 0){
myIntent = new Intent(v.getContext(), TheShow.class);
}
if(position == 1){
myIntent = new Intent(v.getContext(), Exhibitor.class);
}
if(position ==2){
myIntent = new Intent(v.getContext(), Visitor.class);
}
if(position ==3){
myIntent = new Intent(v.getContext(), Conference.class);
}
if(position ==4){
myIntent = new Intent(v.getContext(), ContactUs.class);
}
if(position == 5){
myIntent = new Intent(v.getContext(), ExhibitorReg.class);
}
if(position == 6){
myIntent = new Intent(v.getContext(), VisitorReg.class);
}
if(position == 7){
myIntent = new Intent(v.getContext(), FlrPlan.class);
}
if(myIntent!=null){
startActivity(myIntent);
}
}
});
back=(ImageView)findViewById(R.id.imageView2);
home=(ImageView)findViewById(R.id.imageView3);
site=(ImageView)findViewById(R.id.imageView4);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MainView.this.onBackPressed();
}
});
home.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Same Page", Toast.LENGTH_LONG).show();
}
});
site.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url = "http://india-aviation.in/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
header=(ImageView)findViewById(R.id.imageView1);
header.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainView.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("About Us");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.buttonOK);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("The Show", R.drawable.globecopy1));
items.add(new Item("Exhibitor", R.drawable.workcopy1));
items.add(new Item("Visitor", R.drawable.visitor1copy1));
items.add(new Item("Conference", R.drawable.conferencecopy1));
items.add(new Item("Contact Us", R.drawable.contactus1copy1));
items.add(new Item("Exhibitor Registration", R.drawable.exregistercopy1));
items.add(new Item("Visitor Registration", R.drawable.anothercopy1));
items.add(new Item("Layout Plan", R.drawable.registercopy1));
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i)
{
return items.get(i);
}
#Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}

If you app has a values-21 folder in res folder, either set the layout there or delete the folder if you want persistent layout for all APIs.

Related

ListView CheckBox getting multi selected on just clicking only one time

I'm trying to use CheckBox in my ListView with an ArrayAdapter. When I select any CheckBox onlytime in the list, multiple entries are selected automatically in a random order. Can anyone please tell how I can avoid this.
Here's my code for your reference:
public class SearchListAdapterQ2 extends BaseAdapter {
int layoutId;
ArrayList<SearchListView> searchresultList = new ArrayList<SearchListView>();
public static int companyCpsId;
public static String companyCpsType = "", search_companyName = "",
search_countryName = "", handShakeStatus = "";
public static String handShakeCPSName = "";
public static boolean searchListAdapter_Q2 = false;
SharedPreferences sharedpreferences;
boolean markfavStatus = false;
ListView searchResults_listView;
Context context;
public SearchListAdapterQ2(Context context, int layoutId,
ArrayList<SearchListView> searchresultList,
ListView searchResults_listView) {
// TODO Auto-generated constructor stub
this.layoutId = layoutId;
this.searchresultList = searchresultList;
Log.i("inside searchListAdapter", "inside searchListAdapter");
this.context = context;
sharedpreferences = context.getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
this.searchResults_listView = searchResults_listView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
Log.i("searchresultList",
"searchresultList: " + searchresultList.size());
return searchresultList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return searchresultList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1;
// LayoutInflater inflater = (LayoutInflater)
// context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
char color;
String text = "";
String address = "";
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.qq_searchlist_repeat_items,
parent, false);
holder1 = new ViewHolder1();
holder1.companyName_textView = (TextView) convertView
.findViewById(R.id.companyName_textView);
holder1.companyLogo_textView = (TextView) convertView
.findViewById(R.id.companyLogo_textView);
holder1.companyAddress_textView = (TextView) convertView
.findViewById(R.id.companyAddress_textView);
holder1.handShakeIcon_imageView = (ImageView) convertView
.findViewById(R.id.handShakeIcon_imageView);
holder1.favouritesIcon_imageView = (ImageView) convertView
.findViewById(R.id.favouritesIcon_imageView);
holder1.referIcon_imageView = (ImageView) convertView
.findViewById(R.id.referIcon_imageView);
holder1.sendEnquiry_imageView = (ImageView) convertView
.findViewById(R.id.sendEnquiry_imageView);
holder1.icons_searchResultsPage_relLayout = (RelativeLayout) convertView
.findViewById(R.id.icons_searchResultsPage_relLayout);
holder1.chckbx1 = (CheckBox) convertView.findViewById(R.id.chckbx1);
if (SearchListActivity_Q2.broadcastMode) {
Log.i("icons_searchResultsPage_relLayout is visible",
"icons_searchResultsPage_relLayout is visible");
holder1.icons_searchResultsPage_relLayout
.setVisibility(View.GONE);
holder1.chckbx1.setVisibility(View.VISIBLE);
} else {
holder1.icons_searchResultsPage_relLayout
.setVisibility(View.VISIBLE);
holder1.chckbx1.setVisibility(View.GONE);
}
convertView.setTag(holder1);
} else {
holder1 = (ViewHolder1) convertView.getTag();
}
holder1.id = position;
search_companyName = searchresultList.get(position).getCpsName();
search_countryName = searchresultList.get(position).getCountryName();
try {
String ssearch_companyName = URLDecoder.decode(search_companyName,
"UTF-8");
holder1.companyName_textView.setText(ssearch_companyName);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (searchresultList.get(position).getCpsName().contains(" ")) {
String[] splitText = searchresultList.get(position).getCpsName()
.split("\\s+");
char a = splitText[0].charAt(0);
char b = splitText[1].charAt(0);
text = String.valueOf(a) + String.valueOf(b);
color = b;
} else {
text = searchresultList.get(position).getCpsName().substring(0, 1);
color = searchresultList.get(position).getCpsName().charAt(1);
}
holder1.companyLogo_textView.setText(text.toUpperCase());
if (searchresultList.get(position).getCpsAddress().isEmpty()) {
address = searchresultList.get(position).getCountryName();
} else {
if (searchresultList.get(position).getCpsAddress().length() > 1) {
address = searchresultList.get(position).getCpsAddress() + ", "
+ searchresultList.get(position).getCountryName();
} else {
address = searchresultList.get(position).getCountryName();
}
}
holder1.companyAddress_textView.setText(address);
holder1.companyName_textView
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchListAdapter_Q2 = true;
companyCpsId = searchresultList.get(position)
.getCpsId();
Log.i("$$$ companyCpsId", "companyCpsId" + companyCpsId);
companyCpsType = searchresultList.get(position)
.getCpsType();
Intent intent = new Intent(context,
CompanyProfile_Activity.class);
context.startActivity(intent);
}
});
holder1.referIcon_imageView
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder1.sendEnquiry_imageView
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<Q2_SendEnquiryList> sendEnquiry = new ArrayList<Q2_SendEnquiryList>();
sendEnquiry.add(new Q2_SendEnquiryList(searchresultList
.get(position).getCpsId(), searchresultList
.get(position).getCpsName()));
sendEnquiry.add(new Q2_SendEnquiryList(1, "abcdefgh"));
sendEnquiry.add(new Q2_SendEnquiryList(2, "abcdefg"));
sendEnquiry.add(new Q2_SendEnquiryList(3, "abcdef"));
sendEnquiry.add(new Q2_SendEnquiryList(4, "abcde"));
sendEnquiry.add(new Q2_SendEnquiryList(5, "abcd"));
sendEnquiry.add(new Q2_SendEnquiryList(6, "abc"));
sendEnquiry.add(new Q2_SendEnquiryList(7, "ab"));
Intent intent = new Intent(context,
Q2_SendEnquiryActivity.class);
intent.putParcelableArrayListExtra("sendEnquiry",
sendEnquiry);
context.startActivity(intent);
}
});
holder1.handShakeIcon_imageView
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
companyCpsId = searchresultList.get(position)
.getCpsId();
handShakeCPSName = searchresultList.get(position)
.getCpsName();
handShakeStatus = searchresultList.get(position)
.getHandShakeStatus();
ConstantVariables.handShakeFromAdapter = true;
if (sharedpreferences.getInt("userId_sp", 0) != 0) {
if (sharedpreferences.getInt("profileActiveStatus",
0) > 0) {
if (sharedpreferences.getInt("organizationId",
0) != 0) {
if (handShakeStatus.equalsIgnoreCase("d")) {
ConstantVariables
.handShakeRequest(
context,
companyCpsId,
0,
ConstantVariables.handShakeFromAdapter,
searchResults_listView,
position);
} else if (handShakeStatus
.equalsIgnoreCase("p")) {
ConstantVariables
.handShakeRequestAccept(
context,
companyCpsId,
1,
ConstantVariables.handShakeFromAdapter,
searchResults_listView,
position,
handShakeCPSName);
}
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
context);
// Setting Dialog Title
// alertDialog.setTitle("Please Add Company");
// Setting Dialog Message
alertDialog
.setMessage("Please add your company details");
// Setting Positive "Yes" Button
alertDialog
.setPositiveButton(
"Add",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
Intent intent = new Intent(
context,
Profile_Activity.class);
context.startActivity(intent);
}
});
// Setting Negative "NO" Button
alertDialog
.setNegativeButton(
"Later",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
} else {
ConstantVariables
.requestEmailVerification(context);
}
} else {
ConstantVariables.requestLogin(context);
}
}
});
holder1.favouritesIcon_imageView
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return convertView;
}
static class ViewHolder1 {
TextView companyName_textView, companyAddress_textView,
companyLogo_textView;
ImageView handShakeIcon_imageView, favouritesIcon_imageView,
referIcon_imageView, sendEnquiry_imageView;
CheckBox chckbx1;
int id;
RelativeLayout icons_searchResultsPage_relLayout;
}
}
in your adapter getView() method set the status of the clicked checkbox in model and call notify data set changed, try that
I added the following lines with my code and it started working fine:
ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
final Integer index = new Integer(position);
holder1.chckbx1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
// if checked, we add it to the list
checkedPositions.add(index);
} else if (checkedPositions.contains(index)) {
// else if remove it from the list (if it is present)
checkedPositions.remove(index);
}
}
});
// set the state of the checbox based on if it is checked or not.
holder1.chckbx1.setChecked(checkedPositions.contains(index));

How to add selected spinner values dynamically in listview is in another activity

How to add selected spinner values dynamically in listview is in another activity.
when I have changed spinner value in second time,and open listview activity values are replaced in the listview but not added.
public class MainActivity extends Activity implements OnClickListener {
SharedPreferences SharedPrefs;
String sp_selected;
Spinner sp;
String s1;
String partname;
String partname1;
Button Parts_history;
Imageadapter image_adapter;
private static final String[] paths = { "Select Your Choice", "Type1",
"Type2", "Type3", "Type4" };
private static final int position = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.partsrepair);
image_adapter = new Imageadapter(this);
SharedPrefs = getSharedPreferences("Preference", MODE_PRIVATE);
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new Imageadapter(this));
Parts_history = (Button) findViewById(R.id.partshistory_button);
Parts_history.setOnClickListener((this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View v,
int position, long id) {
SharedPreferences.Editor edit = SharedPrefs.edit();
// partname = parent.getItemAtPosition(position).toString();
partname1 = image_adapter.names[position].toString();
image_adapter.names[position].toString();
Toast.makeText(MainActivity.this, "you Selected:" + partname1,
Toast.LENGTH_SHORT).show();
// edit.putString("SelectPart", partname1);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.repairtype_spinner);
dialog.setCancelable(true);
Spinner sp = (Spinner) dialog.findViewById(R.id.spinner_1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_spinner_item, paths);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position > 0) {
sp_selected = parent.getItemAtPosition(position)
.toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button btnOk = (Button) dialog.findViewById(R.id.Button_sms_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor edit = SharedPrefs.edit();
if ((sp_selected != null)
&& !sp_selected.equals("Select Your Choice")) { //
edit.putString("SelectType", sp_selected);
Toast.makeText(getApplicationContext(),
"You selected:" + sp_selected,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
"Thank You!", Toast.LENGTH_LONG).show();
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this,
"plz Select your Choice",
Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
}
});
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.partshistory_button:
Intent intent = new Intent(MainActivity.this, ListViewItems.class);
intent.putExtra("Part", partname1);
intent.putExtra("Type", sp_selected);
startActivity(intent);
finish();
}
}
}
listviewitems.class:
public class ListViewItems extends Activity {
ArrayList<String> part, type;
ListView list;
public ListViewItems() {
// TODO Auto-generated constructor stub
part = new ArrayList<String>();
type = new ArrayList<String>();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_summary);
Intent i = getIntent();
part.add(i.getStringExtra("Part"));
type.add(i.getStringExtra("Type"));
list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.listview, type);
list.setAdapter(new CustomViewAdapter(ListViewItems.this));
adapter.notifyDataSetChanged();
}
public class CustomViewAdapter extends BaseAdapter {
Context context;
public CustomViewAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
private class ViewHolder {
TextView text_part;
TextView text_type;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Log.d("hao",""+position);
LayoutInflater minflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = minflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text_part = (TextView) convertView
.findViewById(R.id.textView1);
// Log.d("hao", ""+holder.text_desc);
holder.text_type = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.text_type.setText(type.get(position));
holder.text_part.setText(part.get(position));
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return part.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
}

How to add dymnamically selected gridview items in listview

First activity : gridview, whenever u click on gridview item dialogBox is opened which contains spinner. Now, I want to display Selected Gridview item and selected spinner values in Second Activity contains Listview. When u click on secondtime the values are replaced in listview but not added.
Can anyone plz help me...........
MainActivity...
public class MainActivity extends Activity implements OnClickListener {
SharedPreferences SharedPrefs;
String sp_selected;
Spinner sp;
String s1;
String partname;
String partname1;
Button Parts_history;
Imageadapter image_adapter;
private static final String[] paths = { "Select Your Choice", "Type1",
"Type2", "Type3", "Type4" };
private static final int position = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.partsrepair);
image_adapter = new Imageadapter(this);
SharedPrefs = getSharedPreferences("Preference", MODE_PRIVATE);
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new Imageadapter(this));
Parts_history = (Button) findViewById(R.id.partshistory_button);
Parts_history.setOnClickListener((this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View v,
int position, long id) {
SharedPreferences.Editor edit = SharedPrefs.edit();
// partname = parent.getItemAtPosition(position).toString();
partname1 = image_adapter.names[position].toString();
image_adapter.names[position].toString();
Toast.makeText(MainActivity.this, "you Selected:" + partname1,
Toast.LENGTH_SHORT).show();
// edit.putString("SelectPart", partname1);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.repairtype_spinner);
dialog.setCancelable(true);
Spinner sp = (Spinner) dialog.findViewById(R.id.spinner_1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_spinner_item,paths);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position > 0) {
sp_selected = parent.getItemAtPosition(position)
.toString();
}
}
#Override
public void onNothingSelected(AdapterView<?>arg0) {
// TODO Auto-generated method stub
}
});
Button btnOk = (Button)dialog.findViewById(R.id.Button_sms_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor edit = SharedPrefs.edit();
if ((sp_selected != null)
&& !sp_selected.equals("Select Your Choice")) { //
edit.putString("SelectType", sp_selected);
Toast.makeText(getApplicationContext(),
"You selected:" + sp_selected,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
"Thank You!", Toast.LENGTH_LONG).show();
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this,
"plz Select your Choice",
Toast.LENGTH_SHORT).show();
}} });
dialog.show(); }});}
public void onClick(View v) {
switch (v.getId()) {
case R.id.partshistory_button:
Intent intent = new Intent(MainActivity.this, ListViewItems.class);
intent.putExtra("Part", partname1);
intent.putExtra("Type", sp_selected);
startActivity(intent);
finish();
}}}
listview.class
public class ListViewItems extends Activity {
ArrayList<String> part, type;
ListView list;
public ListViewItems() {
// TODO Auto-generated constructor stub
part = new ArrayList<String>();
type = new ArrayList<String>();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_summary);
Intent i = getIntent();
part.add(i.getStringExtra("Part"));
type.add(i.getStringExtra("Type"));
list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.listview, type);
list.setAdapter(new CustomViewAdapter(ListViewItems.this));
adapter.notifyDataSetChanged();
}
public class CustomViewAdapter extends BaseAdapter {
Context context;
public CustomViewAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
private class ViewHolder {
TextView text_part;
TextView text_type;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Log.d("hao",""+position);
LayoutInflater minflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = minflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text_part = (TextView) convertView
.findViewById(R.id.textView1);
// Log.d("hao", ""+holder.text_desc);
holder.text_type = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.text_type.setText(type.get(position));
holder.text_part.setText(part.get(position));
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return part.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}}
Imageadapter.class
public class Imageadapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mlayoutinflater;
public Imageadapter(Context c) {
mContext = c;
mlayoutinflater = LayoutInflater.from(c);
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
ViewHolder childHolder;
if (convertView == null) {
// if it's not recycled, initialize some
convertView = mlayoutinflater.inflate(R.layout.partsnames, null);
childHolder = new ViewHolder();
childHolder.imageView = (ImageView) convertView
.findViewById(R.id.imageView1);
childHolder.textView = (TextView) convertView
.findViewById(R.id.textView1);
convertView.setTag(childHolder);
} else {
childHolder = (ViewHolder) convertView.getTag();
}
childHolder.imageView.setImageResource(mThumbIds[position]);
childHolder.textView.setText(names[position]);
return convertView;
}
public Integer[] mThumbIds = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, };
public String[] names = { "First", "Second", "Third", "Fourth",
"Fifth", "Sixth", "Seventh" };
}
It seems the values are not really being overwritten. Each time the second activity is loaded new part and type ArrayLists are being created. In these new lists only the latest part and type are added (in the onCreate Function) which have travelled with the intent.
If you want the list to keep adding the part and type selected by the user you can maintain the lists on the MainActivity. So upon selection of each item and type add it to the list on the MainActivity and then pass it to the ListViewItems activity.
This way the list will always have all the items selected by you.
I must admit I am not 100% sure if I understood your question correctly. But I have tried to answer with as much as I did.
EDIT:
MainActivity:
public class MainActivity extends ActionBarActivity {
ArrayList<String> list;
int clickCount = 0;
static TextView textViewHelloWorld;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
list = new ArrayList<String>();
textViewHelloWorld = (TextView) findViewById(R.id.textViewHelloWorld);
Button buttonAddElements = (Button) findViewById(R.id.buttonAddElement);
buttonAddElements.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
clickCount = clickCount + 1;
textViewHelloWorld.setText("Click Count" + clickCount);
list.add("Click number " + clickCount);
}
});
Button buttonStartNewActivity = (Button) findViewById (R.id.buttonStartNewActivity);
buttonStartNewActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putStringArrayList("list", list);
startActivity(new Intent(MainActivity.this, Main2Activity.class).putExtras(bundle));
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Main2Activity: (ListView Activity):
public class Main2Activity extends ActionBarActivity {
ArrayList<String> list;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
list = getIntent().getStringArrayListExtra("list");
listView = (ListView) findViewById(R.id.listViewList);
String []dsf = new String[list.size()];
list.toArray(dsf);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, dsf));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2,
container, false);
return rootView;
}
}
}
EDIT2:
MainActivity.Java
public class MainActivity extends Activity implements OnClickListener {
SharedPreferences SharedPrefs;
String sp_selected;
Spinner sp;
String s1;
String partname;
String partname1;
Button Parts_history;
private ArrayList<String> parts;
private ArrayList<String> types;
Imageadapter image_adapter;
private static final String[] paths = { "Select Your Choice", "Type1",
"Type2", "Type3", "Type4" };
private static final int position = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.partsrepair);
image_adapter = new Imageadapter(this);
parts = new ArrayList<String>();
types = new ArrayList<String>();
SharedPrefs = getSharedPreferences("Preference", MODE_PRIVATE);
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new Imageadapter(this));
Parts_history = (Button) findViewById(R.id.partshistory_button);
Parts_history.setOnClickListener((this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View v,
int position, long id) {
SharedPreferences.Editor edit = SharedPrefs.edit();
// partname = parent.getItemAtPosition(position).toString();
partname1 = image_adapter.names[position].toString();
parts.add(partname1);
image_adapter.names[position].toString();
Toast.makeText(MainActivity.this, "you Selected:" + partname1,
Toast.LENGTH_SHORT).show();
// edit.putString("SelectPart", partname1);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.repairtype_spinner);
dialog.setCancelable(true);
Spinner sp = (Spinner) dialog.findViewById(R.id.spinner_1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_spinner_item,paths);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position > 0) {
sp_selected = parent.getItemAtPosition(position)
.toString();
types.add(sp_selected);
}
}
#Override
public void onNothingSelected(AdapterView<?>arg0) {
// TODO Auto-generated method stub
}
});
Button btnOk = (Button)dialog.findViewById(R.id.Button_sms_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor edit = SharedPrefs.edit();
if ((sp_selected != null)
&& !sp_selected.equals("Select Your Choice")) { //
edit.putString("SelectType", sp_selected);
Toast.makeText(getApplicationContext(),
"You selected:" + sp_selected,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),
"Thank You!", Toast.LENGTH_LONG).show();
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this,
"plz Select your Choice",
Toast.LENGTH_SHORT).show();
}} });
dialog.show(); }});}
public void onClick(View v) {
switch (v.getId()) {
case R.id.partshistory_button:
Bundle bundle = new Bundle();
bundle.putStringArrayList("parts", parts);
bundle.putStringArrayList("type", types);
Intent intent = new Intent(MainActivity.this, ListViewItems.class);
intent.putExtra("Part", partname1);
intent.putExtra("Type", sp_selected);
intent.putExtra("bundle", bundle);
startActivity(intent);
finish();
}}}
ListView:
public class ListViewItems extends Activity {
ArrayList<String> part, type;
ListView list;
public ListViewItems() {
// TODO Auto-generated constructor stub
part = new ArrayList<String>();
type = new ArrayList<String>();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_summary);
Intent i = getIntent();
// part.add(i.getStringExtra("Part"));
// type.add(i.getStringExtra("Type"));
part = i.getExtras().getBundle("bundle").getStringArrayList("parts");
type = i.getExtras().getBundle("bundle").getStringArrayList("types");
list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.listview, type);
list.setAdapter(new CustomViewAdapter(ListViewItems.this));
adapter.notifyDataSetChanged();
}
public class CustomViewAdapter extends BaseAdapter {
Context context;
public CustomViewAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
private class ViewHolder {
TextView text_part;
TextView text_type;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Log.d("hao",""+position);
LayoutInflater minflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = minflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text_part = (TextView) convertView
.findViewById(R.id.textView1);
// Log.d("hao", ""+holder.text_desc);
holder.text_type = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.text_type.setText(type.get(position));
holder.text_part.setText(part.get(position));
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return part.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}}

Android how to mange spinner with radio button in listview?

My problem is that when I am clicking on radio button it will change the spinner value randomly at any position..please help here is my adapter class..
public class ManageAdapter extends BaseAdapter {
ArrayList<String> _manageArrayList = null;
private Context _Context = null;
private Integer selectedPosition = 0;
String List1[]=new String[]{"In use","Lost" ,"Stolen" , "For sale"};
private int flag = 0;
public ManageAdapter(ArrayList<String> _manageArrayList, Context _Context) {
super();
this._manageArrayList = _manageArrayList;
this._Context = _Context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _manageArrayList != null? _manageArrayList.size():0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public TextView itemTextView;
public RadioButton myitemButton;
public Spinner inUseSpinner;
public RelativeLayout managelayout;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater)_Context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(v == null)
{
holder = new ViewHolder();
v = inflater.inflate(R.layout.manage_single_row, null,false);
holder.managelayout = (RelativeLayout)v.findViewById(R.id.single_row_layout);
holder.itemTextView = (TextView)v.findViewById(R.id.item_name_txt);
holder.myitemButton = (RadioButton)v.findViewById(R.id.manage_radio_buton);
holder.inUseSpinner = (Spinner)v.findViewById(R.id.Status_spinner);
v.setTag(holder);
ArrayAdapter<String> Adapter = new ArrayAdapter<String>(_Context,R.layout.spinner_color,List1);
Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
holder.inUseSpinner.setAdapter(Adapter);
}else {
holder = (ViewHolder) v.getTag();
}
holder.itemTextView.setText(_manageArrayList.get(position));
holder.inUseSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// int getPosition = (Integer) arg0.getTag();
// Toast.makeText(_Context,List1[position] ,Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if(flag == 0)
{
flag = 1;
holder.myitemButton.setChecked(false);
holder.myitemButton.setTag(position);
}else {
holder.myitemButton.setChecked(position == selectedPosition);
holder.myitemButton.setTag(position);
}
holder.myitemButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = (Integer)v.getTag();
notifyDataSetInvalidated();
}
});
return v;
}
}
Thanks in advance
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_item);
_backButton = (Button)findViewById(R.id.back);
_backButton.setOnClickListener(this);
_headerTextView = (TextView)findViewById(R.id.header_TextView);
_headerTextView.setText("My Items");
_uploadButton = (ImageButton)findViewById(R.id.Upload_file_button);
_uploadButton.setOnClickListener(this);
_addmanullyButton = (ImageButton)findViewById(R.id.add_item_manually);
_addmanullyButton.setOnClickListener(this);
_addphoneButton = (ImageButton)findViewById(R.id.auto_add_phone);
_addphoneButton.setOnClickListener(this);
_modifyButton = (ImageButton)findViewById(R.id.modify_button);
_modifyButton.setOnClickListener(this);
_transferButton = (ImageButton)findViewById(R.id.transfer_ownership);
_transferButton.setOnClickListener(this);
_hideenButton = (Button)findViewById(R.id.hideen_button);
_hideenButton.setOnClickListener(this);
_manageListView = (ListView)findViewById(R.id.manage_listview);
prepareList();
_adapter = new ManageAdapter(listItem, this);
_manageListView.setAdapter(_adapter);
}
public void prepareList() {
listItem = new ArrayList<String>();
listItem.add("Android smart");
listItem.add("sony");
listItem.add("samsung");
listItem.add("micromax");
listItem.add("tablet");
}
#Override
public void onClick(View view) {
Intent intent = null;
switch (view.getId()) {
case R.id.Upload_file_button:
intent = new Intent(ManageItems.this, UploadFileActivity.class);
startActivity(intent);
break;
case R.id.modify_button:
intent = new Intent(ManageItems.this, ModifyActivity.class);
startActivity(intent);
break;
case R.id.transfer_ownership:
intent = new Intent(ManageItems.this, TransferOwnerShip.class);
startActivity(intent);
break;
case R.id.add_item_manually:
intent = new Intent(ManageItems.this, AddManuallyActivity.class);
startActivity(intent);
break;
case R.id.auto_add_phone:
intent = new Intent(ManageItems.this, AutoAddActivity.class);
startActivity(intent);
break;
case R.id.back:
intent = new Intent(ManageItems.this, MenuActivtiy.class);
startActivity(intent);
break;
case R.id.hideen_button:
intent = new Intent(ManageItems.this, MenuActivtiy.class);
startActivity(intent);
break;
}
Thanks all but I got the solution of my question..
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
MyItemListDataHolder myItemListDataHolder = _manageArrayList.get(position);
LayoutInflater inflater = (LayoutInflater)_Context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(v == null)
{
holder = new ViewHolder();
v = inflater.inflate(R.layout.manage_single_row, null,false);
holder.managelayout = (RelativeLayout)v.findViewById(R.id.single_row_layout);
holder.itemTextView = (TextView)v.findViewById(R.id.item_name_txt);
holder.myitemButton = (RadioButton)v.findViewById(R.id.manage_radio_buton);
holder.inUseSpinner = (Spinner)v.findViewById(R.id.Status_spinner);
v.setTag(holder);
}else {
holder = (ViewHolder) v.getTag();
}
holder.itemTextView.setText(myItemListDataHolder.get_itemName());
Adapter = new ArrayAdapter<String>(_Context,R.layout.spinner_color,List1);
Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
holder.inUseSpinner.setAdapter(Adapter);
if(myItemListDataHolder.get_selectedItem() > 0){
holder.inUseSpinner.setSelection(myItemListDataHolder.get_selectedItem());
}else{
holder.inUseSpinner.setSelection(0);
}
holder.inUseSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
private Intent intent;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position1, long arg3) {
_manageArrayList.get(position).set_selectedItem(position1);
// notifyDataSetChanged();
String status=holder.inUseSpinner.getSelectedItem().toString();
if(status.equalsIgnoreCase("Lost"))
{
intent = new Intent (_Context, LostItemActivity.class);
startActivity(intent);
}else if (status.equalsIgnoreCase("Stolen")) {
intent = new Intent (_Context, StolenItemActivity.class);
startActivity(intent);
}else if (status.equalsIgnoreCase("Stolen")) {
intent = new Intent (_Context, LostFoundActivity.class);
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(flag == 0)
{
flag = 1;
holder.myitemButton.setChecked(false);
}else {
holder.myitemButton.setChecked(position == selectedPosition);
}
holder.myitemButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = position;
notifyDataSetChanged();
//_adapter.notifyDataSetChanged();
}
});
return v;
}
}

App crashes on android 4.0+

I have and android app that works on android 4.0 great, but it crashes on android 4.3 and 4.4. I get this from the logCat
01-11 14:40:27.669: E/ACRA(25835): ACRA caught a IllegalStateException exception for quran. Building report.
01-11 14:40:27.789: E/AndroidRuntime(25835): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131099688, class android.widget.ListView) with Adapter(class quran.functions.PlaylistAdapter)]
Here is my code:
public class Playlist extends FragmentActivity {
private ListView list;
private Button manager, downloadAll;
private TextView reciter;
public static PlaylistAdapter adapter;
private ArrayList<Songs> songs;
private int RECITER_ID;
private String url, title, label;
private SlidingMenu slidingMenu;
private DatabaseHelper db;
private ImageView nowPlaying, back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
initWidgets();
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent intent = new Intent(Playlist.this, PlayerFinal.class);
intent.putExtra("songs", songs);
if (getIntent().getIntExtra("duaa", -1) == 115)
intent.putExtra("lang", 115);
intent.putExtra("position", position);
intent.putExtra("fromClass", this.getClass() + "");
// intent.putExtra("mp3link", mp3link);
startActivity(intent);
}
});
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
XmlMapParser m = new XmlMapParser(Playlist.this, RECITER_ID);
HashMap<String, ArrayList<String>> map = m.convert();
map.keySet();
label = map.get("RecitorLabel").get(0);
title = map.get("Title").get(0);
url = map.get("Link").get(0);
back = (ImageView) findViewById(R.id.playlist_back);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
}
});
db.openDB();
for (int i = 1; i < map.get("Link").size(); i++) {
if (db.isDownloaded(i, title, RECITER_ID)) {
songs.add(new Songs(i, map.get("Title").get(i),
Environment.getExternalStorageDirectory()
.getPath()
+ "/"
+ getString(R.string.app_name)
+ "/"
+ title
+ "/"
+ map.get("Title").get(i)
+ ".mp3", title, true, RECITER_ID,
false));
} else
songs.add(new Songs(i, map.get("Title").get(i), url
+ label + "/"
+ new DecimalFormat("000").format(i) + ".mp3",
title, false, RECITER_ID, false));
}
db.closeDB();
// Log.v("--",m.convert().get("Link").get(1));
// [RecitorLabel, Title, Link] THIS ARE THE KEYS m
// Log.v("--", map.get("RecitorLabel").get(0));
// Log.v("--", map.get("Link").get(1));
return null;
}
protected void onPostExecute(Void result) {
adapter = new PlaylistAdapter(Playlist.this, songs);
list.setAdapter(adapter);
reciter.setText(songs.get(0).getRecitorName());
};
}.execute();
}
#Override
public void onBackPressed() {
if (slidingMenu.isMenuShowing()) {
slidingMenu.toggle();
} else {
super.onBackPressed();
}
}
#Override
protected void onResume() {
super.onResume();
try {
if (Tplayer.getInstance().isPlaying()) {
adapter = new PlaylistAdapter(this, songs);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
this.slidingMenu.toggle();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.slidingMenu.toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void initWidgets() {
db = new DatabaseHelper(this);
manager = (Button) findViewById(R.id.playlist_download_manager);
manager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Playlist.this, DownloadManager.class);
startActivity(intent);
}
});
reciter = (TextView) findViewById(R.id.playlist_reciter_name_top);
list = (ListView) findViewById(R.id.playlist_list);
downloadAll = (Button) findViewById(R.id.playlist_download_all);
manager = (Button) findViewById(R.id.playlist_download_manager);
songs = new ArrayList<Songs>();
RECITER_ID = getIntent().getIntExtra("filename", -1);
// downloadAll.setOnClickListener(new OnClickListener() {
//
// #Override
// public void onClick(View v) {
// new DownloadAll(Playlist.this, songs);
// db.openDB();
// for (int i = 0; i < songs.size(); i++) {
// db.addDownloaded(songs.get(i).getNumber(), songs.get(i)
// .getLink(), 0, songs.get(i).getRecitorID(), "",
// songs.get(i).getTitle());
// }
// db.closeDB();
// }
// });
nowPlaying = (ImageView) findViewById(R.id.playlist_now_playing);
nowPlaying.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Tplayer tplayer = Tplayer.getInstance();
if (tplayer.isPlaying()) {
Intent intent = new Intent(Playlist.this, PlayerFinal.class);
if (tplayer.isPlaying())
intent.putExtra("songs", tplayer.getSongs());
else
intent.putExtra("songs", songs);
if (tplayer.getSongs().size() == 14)
intent.putExtra("lang", 115);
intent.putExtra("position", tplayer.getPosition());
startActivity(intent);
}
}
});
// Jeremy Feinstein slidinglistadapter line 94
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slidingMenu.setMenu(R.layout.slidingmenu);
}
}
and my playlist adapter class:
public class PlaylistAdapter extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater = null;
private ArrayList<Songs> data;
private DatabaseHelper db;
private SharedPreferences prefs;
int playpos;
int recitorID;
public PlaylistAdapter(Activity a, ArrayList<Songs> songs) {
activity = a;
data = songs;
db = new DatabaseHelper(a);
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
prefs = activity.getSharedPreferences("quantic.Quran",
Context.MODE_PRIVATE);
recitorID = prefs.getInt("recID", -1);
playpos = prefs.getInt("posPlaying", -1);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.song_item, parent, false);
ImageView download = (ImageView) vi
.findViewById(R.id.playlist_item_download);
db.openDB();
if (db.isDownloaded(data.get(position).getNumber(), data.get(position)
.getRecitorName(), data.get(position).getRecitorID()))
download.setImageResource(R.drawable.download_yes);
else {
download.setImageResource(R.drawable.download_no);
download.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new DownloadFileFromURL(activity, data.get(position)
.getRecitorName(), data.get(position).getTitle(),
data.get(position).getLink(), data.get(position)
.getNumber(), data.get(position)
.getRecitorID()).execute();
if (!db.isDBOpen())
db.openDB();
db.addDownloaded(data.get(position).getNumber(),
data.get(position).getLink(), 0, data.get(position)
.getRecitorID(), "", data.get(position)
.getTitle());
Toast.makeText(activity,
"Downloading " + data.get(position).getTitle(),
Toast.LENGTH_SHORT).show();
}
});
}
db.closeDB();
TextView number = (TextView) vi.findViewById(R.id.playlist_item_num);
TextView reciterName = (TextView) vi
.findViewById(R.id.playlist_item_reciterName);
reciterName.setText(data.get(position).getRecitorName());
if (activity.getClass() == Playlist.class) {
reciterName.setVisibility(View.GONE);
}
TextView title = (TextView) vi.findViewById(R.id.playlist_item_reciter);
title.setText(data.get(position).getTitle());
number.setText((position + 1) + "");
ImageView eq = (ImageView) vi.findViewById(R.id.playlist_item_equlizer);
if (Tplayer.getInstance().isPlaying())
if (Tplayer.getInstance().getPosition() == position
&& data.get(position).getRecitorID() == Tplayer
.getInstance().getSong().getRecitorID()) {
eq.setVisibility(View.VISIBLE);
Ion.with(eq).load("http://darkodev.info/quran/dots.gif");
} else {
eq.setVisibility(View.GONE);
}
return vi;
}
}
Very old question, but no answer. I'm sure you have found a fix by now, but anyway.
You're changing the songs list object in background, and if Android decides to redraw your list (user scrolls your list), effectively accessing the songs list object, it may have changed and cause this exception to be thrown.
You need to use a temporary songs list and create a new adapter with it to update your list, thus not changing the current adapter list until you set a new adapter from the main UI thread.

Categories

Resources