Android - package manager disable/enable - android

I have a problem with disable application through package manager function disable/enable.
In android terminal is all corect, but in this application not. However " Runtime.getRuntime().exec("su"); " functioning normally and carrying out his activity.
public class AppInfoAdapter extends BaseAdapter {
private Context aContext;
private List<ApplicationInfo> aListAppInfo;
private PackageManager aPackManager;
private CheckBox chAppStat ;
public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
aContext = c;
aListAppInfo = list;
aPackManager = pm;
}
#Override
public int getCount() {
return aListAppInfo.size();
}
#Override
public Object getItem(int position) {
return aListAppInfo.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ApplicationInfo entry = aListAppInfo.get(position);
//positon = position;
View v = convertView; // convertView
// recyklovace
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(aContext);
v = inflater.inflate(R.layout.layout_appinfo, null);
}
// nahrat layout
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
chAppStat = (CheckBox)v.findViewById(R.id.chBox);
//text = entry.packageName;
chAppStat.setTag(entry.packageName);
chAppStat.setOnCheckedChangeListener(aListener);
// privest na vystup
ivAppIcon.setImageDrawable(entry.loadIcon(aPackManager));
tvAppName.setText(entry.loadLabel(aPackManager));
tvPkgName.setText(entry.packageName);
// navrat view
return v;
}
OnCheckedChangeListener aListener = new CompoundButton.OnCheckedChangeListener () {
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked) {
if(isChecked = true){
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("pm disable "+ buttonView.getTag());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
try {
Runtime.getRuntime().exec("pm enable "+ buttonView.getTag());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
}
recapitulation: problem is package manager function disable/enable in the application. BUT in terminal is correct "pm disable some.name.package"
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("pm disable "+ buttonView.getTag());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Related

handling click of buttons in list view item android

I have a custom list view where each list item has two buttons, one text view and one seek bar where the seek bar visibility is set to gone in list item's XML.
Now what I want to do is on click of button one in the list item I want to hide the text view and display the seek bar of that list item whose button is clicked and show hide the seek bar and display the text view of all other list items.
The code that I have written works well if the total items are less than the views that can be displayed at once in a list view but for a lot of views this does not works.
What I am doing is load a list of files from a a folder into the list view and play the in the list view only
Please tell me where I am going wrong.
Any help would be appreciated.
My Adapter Code:
public class AudioFileListAdapter extends BaseAdapter {
Context context;
ArrayList<String> fileList;
LayoutInflater inflater;
String folder;
ListView mListView;
public MediaPlayer mPlayer;
public Handler seekHandler;
SeekBar mSeekBar;
int clickedPos = -1;
protected static class RowViewHolder {
public TextView fileName;
public Button filePlay;
public Button rate;
public SeekBar fileSeek;
}
public AudioFileListAdapter(Context context, ArrayList<String> fileList,
String folder) {
this.fileList = fileList;
this.context = context;
this.folder = folder;
mListView = (ListView) ((Activity) context)
.findViewById(R.id.Filelistview);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
seekHandler = new Handler();
}
#Override
public int getCount() {
return fileList.size();
}
#Override
public Object getItem(int position) {
return fileList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RowViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.audio_file_list_item, null);
viewHolder = new RowViewHolder();
viewHolder.fileName = (TextView) convertView
.findViewById(R.id.file_name);
viewHolder.filePlay = (Button) convertView
.findViewById(R.id.file_play);
viewHolder.filePlay.setTag(position);
viewHolder.rate = (Button) convertView.findViewById(R.id.rate);
viewHolder.fileSeek = (SeekBar) convertView
.findViewById(R.id.file_seek);
viewHolder.filePlay.setOnClickListener(mListener);
viewHolder.rate.setOnClickListener(mListener);
convertView.setTag(viewHolder);
} else {
viewHolder = (RowViewHolder) convertView.getTag();
}
viewHolder.fileName.setText(fileList.get(position));
return convertView;
}
OnClickListener mListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.file_play)
{
final int position = mListView.getPositionForView((View) v
.getParent());
Log.d("position"+position, "count"+mListView.getCount());
for (int i = 0 ; i < mListView.getChildCount(); i++) {
RowViewHolder viewholder = (RowViewHolder) ((View) mListView
.getChildAt(i)).getTag();
if (i == position) {
String path = Environment.getExternalStorageDirectory()
.toString()
+ "/analyser/"
+ folder
+ "/"
+ fileList.get(i);
mSeekBar = viewholder.fileSeek;
Log.d("IN the click "+i,path);
// mSeekBar.setEnabled(false);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(mPlayer != null && fromUser){
mPlayer.seekTo(progress);
}
}
});
viewholder.fileSeek.setVisibility(View.VISIBLE);
viewholder.fileName.setVisibility(View.GONE);
play(path);
} else {
viewholder.fileSeek.setVisibility(View.GONE);
viewholder.fileName.setVisibility(View.VISIBLE);
}
}
}
if (v.getId() == R.id.rate) {
}
}
};
public void play(String path) {
if (mPlayer == null) {
mPlayer = new MediaPlayer();
mPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mSeekBar.setMax(mPlayer.getDuration());
mp.start();
seekUpdation();
}
});
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mPlayer.release();
mPlayer = null;
seekHandler.removeCallbacks(run);
}
});
}
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
seekHandler.removeCallbacks(run);
}
try {
mPlayer.setDataSource(path);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Runnable run = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if (mPlayer != null) {
seekUpdation();
}
}
};
public void seekUpdation() {
if (mPlayer != null) {
mSeekBar.setProgress(mPlayer.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
}
}
You have to set the listeners for the button in the Adapter class getView method.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RowViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.audio_file_list_item, null);
viewHolder = new RowViewHolder();
viewHolder.fileName = (TextView) convertView.findViewById(R.id.file_name);
viewHolder.filePlay = (Button) convertView.findViewById(R.id.file_play);
viewHolder.filePlay.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
// Do Your Work Here
}
});
viewHolder.filePlay.setTag(position);
viewHolder.rate = (Button) convertView.findViewById(R.id.rate);
viewHolder.fileSeek = (SeekBar) convertView.findViewById(R.id.file_seek);
viewHolder.filePlay.setOnClickListener(mListener);
viewHolder.rate.setOnClickListener(mListener);
convertView.setTag(viewHolder);
} else {
viewHolder = (RowViewHolder) convertView.getTag();
}
viewHolder.fileName.setText(fileList.get(position));
return convertView;
}

Select all listview checkbox when we select the select all checkbox in android

I m using fragment where i m having the ListView and the select all check box.
Fragment class..
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fb_custom_listview = (ListView) selectfriend_login
.findViewById(R.id.friend_listview);
select_all=(CheckBox) selectfriend_login.findViewById(R.id.select_all_checkbox);
select_all.setOnCheckedChangeListener(this);
}
now having the custom adapter class extends Baseadapter
where i m having one ImageView ,one TextView and one checkbox in the layout and inflating the layout in the getview method of the baseadapter class.everything is ok it display image,display text and display the checkbox also.so now when i click the select_all(fragment class) checkbox all the checkbox of custom adapter class should be checked and on unchecked all are unchecked.Here is the class
for custom adapter
public class CustomListAdapter extends BaseAdapter
{
private Context contxt;
private ArrayList<String> user_friend_name;
private ArrayList<String> user_friend_pic_url;
private LayoutInflater inflater;
private int friend_counter=0;
private ChangeUIListner mlistner;
public static ArrayList<Integer> selected_friend_pos;
public static ArrayList<String> selected_friend_uid;
public static ArrayList<String> selected_friend_name;
private List<Model> list;
private ViewHolder viewholder=null;
public CustomListAdapter(Context context,ArrayList<String> user_friend_picurl, List<Model> model) {
// TODO Auto-generated constructor stub
this.contxt=context;
this.list=model;
user_friend_pic_url=user_friend_picurl;
selected_friend_uid=new ArrayList<String>();
selected_friend_name=new ArrayList<String>();
selected_friend_pos=new ArrayList<Integer>();
user_friend_name=GetFriendDetails.user_fb_friend_name;
inflater=(LayoutInflater) contxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.gc();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return user_friend_pic_url.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;
}
#Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
viewholder=new ViewHolder();
convertView=inflater.inflate(R.layout.custom_listview_friendlist, null);
viewholder.fb_friend_name=(TextView) convertView.findViewById(R.id.friend_nametextview);
viewholder.fb_friend_pic=(ImageView) convertView.findViewById(R.id.friend_picview);
viewholder.fb_checkbox=(CheckBox) convertView.findViewById(R.id.friendselectcheckBox);
viewholder.fb_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getposition=(Integer) buttonView.getTag();
Log.e("ITEM POSITION", "."+getposition);
list.get(getposition).setSelected(buttonView.isChecked());
if(isChecked==true)
{
if(selected_friend_pos.contains(getposition))
{
//nothing
}
else
{
friend_counter++;
mlistner.UpdateUi(friend_counter);
selected_friend_pos.add(getposition);
selected_friend_uid.add(GetFriendDetails.user_fb_friend_id.get(getposition));
selected_friend_name.add(GetFriendDetails.user_fb_friend_name.get(getposition));
Log.e("ARRAY SIZE", "."+selected_friend_uid.size());
}
}
else
{
if(selected_friend_pos.contains(getposition)){
friend_counter--;
mlistner.UpdateUi(friend_counter);
int del_posi=selected_friend_pos.indexOf(getposition);
selected_friend_pos.remove(del_posi);
selected_friend_name.remove(del_posi);
selected_friend_uid.remove(del_posi);
Log.e("ARRAYLIST", "REM"+del_posi);
Log.e("ARRAY SIZE", "."+selected_friend_uid.size());
}
}
}
});
convertView.setTag(viewholder);
}
else
{
viewholder=(ViewHolder) convertView.getTag();
new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position));
}
viewholder.fb_checkbox.setTag(position);
viewholder.fb_checkbox.setChecked(list.get(position).isSelected());
viewholder.fb_friend_name.setText(user_friend_name.get(position));
viewholder.fb_friend_name.setTag(user_friend_name.get(position));
viewholder.fb_friend_pic.setTag(user_friend_pic_url.get(position));
//new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position));
return convertView;
}
class ViewHolder
{
int id;
ImageView fb_friend_pic;
TextView fb_friend_name;
CheckBox fb_checkbox;
}
public class AsyncDownloadImage extends AsyncTask<String, Object, Bitmap>
{
private HttpURLConnection connection;
private InputStream is;
private Bitmap bitmap;
private URL url;
ViewHolder holder;
public AsyncDownloadImage(ViewHolder viewholder1) {
// TODO Auto-generated constructor stub
holder=viewholder1;
}
#Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
try {
url=new URL(params[0]);
try {
connection=(HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
is=connection.getInputStream();
bitmap=BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(is!=null)
{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(connection!=null)
{
connection.disconnect();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
if(result!=null)
{
holder.fb_friend_pic.setImageBitmap(result);
}
}
}
public void registerUIupdateListener(ChangeUIListner lisnter)
{
mlistner=lisnter;
}
}
as u can see i had used the single selection thing for the listview checkbox so its working fine and adding into arraylist for further operation.so how can we achieve the select all when the check box is click or select.Thanks
When u click selectAll check button then call selectAllFromList(value) function.
like
selectAllButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked())
{
selectAllFromList(true);
}else
{
selectAllFromList(false);
}
}
Following function will select all check boxes from the list:
private void selectAllFromList(boolean b) {
int cList = list.getCount();
for(int i=0;i<cList;i++){
View sView = yourMainList.getChildAt(i);
if(sView != null){
CheckBox childCheckBox=(CheckBox)sView.findViewById(R.id.urCheckButton);
childCheckBox.setChecked(b);
childCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked())
{
selectAllButton.setChecked(false);
}
}
});
}
}
}
});
Hope this one helps you

Unchecking Checkboxes in Custom Listview

I want to unchecked my all check boxes inside listview on button click.
This my adapter class. It unchecked all check boxes but for that I have to scroll through listview.
When I scroll through it, it unchecked according to the rows that are visible.
public class ExpenseCalculatorAdapter extends ArrayAdapter<String>{
private Activity context;
private static LayoutInflater inflator = null;
private ArrayList<String> data;
private ArrayList<String> values;
DataBaseUtil dbUtils;
public ExpenseCalculatorAdapter(Activity context, ArrayList<String> data){
super(context,R.layout.expenserow, data);
this.context = context;
this.data = data;
dbUtils=new DataBaseUtil(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String row=data.get(position);
String date;
String type;
String cost;
final String id;
/* result.add(c.getString(iRowid)+"&"+c.getString(iDate)+"~"+c.getString(iCost)+"#"+c.getString(iType)+"*");*/
id=row.substring(0, row.indexOf("&"));
date=row.substring(row.indexOf("&")+1,row.indexOf("~"));
cost=row.substring(row.indexOf("~")+1,row.indexOf("#"));
type=row.substring(row.indexOf("#")+1,row.indexOf("*"));
if(convertView==null)
{
ViewHolder holder = new ViewHolder();
inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.expenserow, null);
holder.togCheck=(CheckBox)convertView.findViewById(R.id.check);
holder.textDate = (TextView) convertView.findViewById(R.id.txtrowDate);
holder.textType = (TextView) convertView.findViewById(R.id.txtrowType);
holder.textCost = (TextView) convertView.findViewById(R.id.txtrowCost);
holder.togCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction("clicked");
if(buttonView.isChecked())
{
try
{
dbUtils.open();
dbUtils.setVisibility(id);
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","yes");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
else
{
try
{
dbUtils.open();
dbUtils.setInVisibility(id);
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","no");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
}
});
convertView.setTag(holder);
}
ViewHolder hold = (ViewHolder) convertView.getTag();
//setting Data to List
hold.textDate.setText(date);
hold.textType.setText(type);
hold.textCost.setText(cost);
if(CheckReceiver.checkAll)
{
hold.togCheck.setChecked(true);
}
if(!CheckReceiver.checkAll)
{
hold.togCheck.setChecked(false);
}
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
static class ViewHolder{
public CheckBox togCheck;
public TextView textDate;
public TextView textType;
public TextView textCost;
}
}
As Listview recycles view its not possible to do this with only BroadCastReciver. It was really a bad idea.
I done this using Database and BroadCastReciver.
holder.togCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Intent intent=new Intent();
intent.setAction("clicked")*/;
if(holder.togCheck.isChecked())
{
//holder.togCheck.setBackgroundResource(R.drawable.check);
try
{
dbUtils.open();
dbUtils.setVisibility(id);
// Toast.makeText(context, "Checked ", Toast.LENGTH_SHORT).show();
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","yes");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
else
{
// holder.togCheck.setBackgroundResource(R.drawable.uncheck);
try
{
dbUtils.open();
dbUtils.setInVisibility(id);
Toast.makeText(context, "UnChecked ", Toast.LENGTH_SHORT).show();
}catch(SQLException sqx)
{
sqx.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
dbUtils.close();
}
intent.putExtra("isClicked","no");
intent.putExtra("ID",""+id);
context.sendBroadcast(intent);
}
}
});
what i done is i set the checked or unchecked state of checkbox according to id in database.
Then by fetching state of that id i done calculation!.

android Use own Image in ListView

I have 10 graphic needs to display in my ListView
and I create a class like this
public class LayoutAdapterObj {
public String addString;
public ImageView leftImage;
public String backgroundImageName;
public LayoutAdapterObj(String addString,ImageView leftImageName,String backgroundImageName){
this.addString = addString;
this.leftImage = leftImageName;
this.backgroundImageName = backgroundImageName;
}
and when I create a LayoutAdaptor object
I do this
aArray.add(new LayoutAdapterObj("1",new ImageView(null, null, R.drawable.frame1),"R.drawable.layout"));
aArray.add(new LayoutAdapterObj("2",new ImageView(null, null, R.drawable.frame2),"R.drawable.layout"));
LayoutListAdapter m_adapter;
m_adapter = new LayoutListAdapter(this, R.layout.topbarlayout, aArray);
after that.... in my adapter class, i do this
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.layout1, null);
}
LayoutAdapterObj o = items.get(position);
if (o != null) {
TextView addText = (TextView) v.findViewById(R.id.addtext);
ImageView leftImage=(ImageView)v.findViewById(R.id.layoutframe);
if(!o.backgroundImageName.equals(""))
{
}
if(addText!=null)
{
addText.setText(o.addString);
}
the wrong message is like this
java.lang.IllegalStateException: Could not execute method of the activity
if(leftImage != null)
{
leftImage= o.leftImage;
}
see this...example...`
class UserListAdapter extends ArrayAdapter<String>
{
#Override
public int getCount() {
// TODO Auto-generated method stub
int temp;
if(SharedVariables.is_traffic_search_ON_flag){
temp=search_cases_trafficTicketsList.size();
}else{
temp=search_cases_criminalTicketsList.size();
}
return temp;
}
Activity context;
public UserListAdapter(Activity context) {
super(context, 0);
Log.d("hh","sgbdfjfdgfk");
this.context = context;
}
class ViewHolder {
RelativeLayout rl2;
ImageView genderimage;
TextView ticketcounttt;
TextView name;
TextView city;
TextView added_date;
}
#SuppressWarnings("unused")
public View getView(final int position, View convertView, ViewGroup parent){//here we inflating the layout "R.layout.cars_row"
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
if(SharedVariables.is_traffic_search_ON_flag)rowView = inflater.inflate(R.layout.traffic_row, null, true);
else rowView = inflater.inflate(R.layout.criminal_row, null, true);
holder = new ViewHolder();
holder.rl2=(RelativeLayout) rowView.findViewById(R.id.rl2);
try{
holder.genderimage=(ImageView) rowView.findViewById(R.id.genderimage);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
holder.ticketcounttt=(TextView) rowView.findViewById(R.id.ticketcounttt);
holder.name=(TextView) rowView.findViewById(R.id.textView1);
holder.city=(TextView) rowView.findViewById(R.id.textView2);
holder.added_date=(TextView) rowView.findViewById(R.id.textView333);
//Traffic cases...
if(SharedVariables.is_traffic_search_ON_flag)
{
if(search_cases_trafficTicketsList.get(position).CaseID!=0)holder.rl2.setBackgroundResource(R.drawable.green_color1); //Pink color...
else holder.rl2.setBackgroundResource(R.drawable.pink_color); //Green color...
if(search_cases_trafficTicketsList.get(position).Sex.equals("M"))holder.genderimage.setBackgroundResource(R.drawable.man);
else holder.genderimage.setBackgroundResource(R.drawable.woman);
holder.ticketcounttt.setTypeface(SharedVariables.font);
holder.name.setTypeface(SharedVariables.font);
holder.city.setTypeface(SharedVariables.font);
holder.added_date.setTypeface(SharedVariables.font);
holder.name.setText(search_cases_trafficTicketsList.get(position).FirstName+" "+search_cases_trafficTicketsList.get(position).LastName);
holder.city.setText(search_cases_trafficTicketsList.get(position).City+" City"+"("+search_cases_trafficTicketsList.get(position).ZIP+")");
try{
String s[]=search_cases_trafficTicketsList.get(position).ViolationDate.split("/");
String month=SharedVariables.convertFromNumberToMonth(Integer.parseInt(s[0]));
String day=s[1];
String year=s[2].substring(0,4);
holder.added_date.setText("Added: "+Integer.parseInt(day)+" "+month+" "+year);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try{
holder.ticketcounttt.setText(""+search_cases_trafficTicketsList.get(position).Violations.split(",").length);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
//Criminal casess...
else{
if(search_cases_criminalTicketsList.get(position).CaseID!=0)holder.rl2.setBackgroundResource(R.drawable.green_color1); //Pink color...
else holder.rl2.setBackgroundResource(R.drawable.pink_color); //Green color...
holder.ticketcounttt.setTypeface(SharedVariables.font);
holder.name.setTypeface(SharedVariables.font);
holder.city.setTypeface(SharedVariables.font);
holder.added_date.setTypeface(SharedVariables.font);
holder.name.setText(search_cases_criminalTicketsList.get(position).FirstName+" "+search_cases_criminalTicketsList.get(position).LastName);
holder.city.setText(search_cases_criminalTicketsList.get(position).City+" City");
try{
String s[]=search_cases_criminalTicketsList.get(position).ViolationDate.split("/");
String month=SharedVariables.convertFromNumberToMonth(Integer.parseInt(s[0]));
String day=s[1];
String year=s[2].substring(0,4);
holder.added_date.setText("Added: "+Integer.parseInt(day)+" "+month+" "+year);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try{
holder.ticketcounttt.setText(""+search_cases_criminalTicketsList.get(position).Violations.split(",").length);
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
return rowView;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}`
call your adapter like this
UserListAdapter a=new UserListAdapter();
yurlistview.setAdapter(a);

Issue with Record holder custom adapter

I have listAdapter of listview and I have set record button in custom adapter. I have put code inside click event of holder.record like
holder.record.setBackgroundResource(R.drawable.record_green);
for set background of that button while user touch.but my problem is when I touch particular list item button this images are apply in another list item
Here are code:
holder.record.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isplaying2 == true) {
} else
{
if (b_Flag_Record_or_not == true) {
} else {
try {
holder.record.setBackgroundResource(R.drawable.record_green);
b_Flag_Record_or_not = true;
b_play_or_not = true;
flag_stop_position = position;
AuthHandler dataHandler = new AuthHandler();
AuthDataset dataset = dataHandler
.getParsednewJobdtl_DataSet();
System.out.println("dataset.getint1();"
+ dataset.getint1());
// Login l = new Login();
String str_useid = RequestTo[position];
recorder = new AudioRecorder("/audiometer/shanesh"
+ RequestId[position] + "-" + str_useid);
start_or_not = true;
recorder.start();
Toast.makeText(context, "Start Recording", Toast.LENGTH_LONG).show();
CountDownTimer countDowntimer = new CountDownTimer(
120000000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
try {
Toast.makeText(
context,
"Stop recording Automatically ",
Toast.LENGTH_LONG).show();
recorder.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
countDowntimer.start();
} catch (IOException e) {
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
String s = writer.toString();
}catch (Exception e) {
}
}
}
}
});
Update:
public class ListViewAdapter extends BaseAdapter {
public static Activity context;
String title[];
String description[];
String RequestId[];
String Folderpath[];
String RequestTo[];
boolean b_Flag_Record_or_not = false;
boolean b_upload_or_not = false;
boolean start_or_not = false;
boolean start_play = false;
boolean upload_or_not = false;
boolean b_play_or_not = false;
boolean isplaying2 = false;
Thread welcomeThread;
int glob_position;
MediaPlayer mPlayer2;
int flag_stop_position;
AudioRecorder recorder;
AnimationDrawable frameAnimation, frameAnimation_play;
private static String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
Recording login = new Recording();
ViewHolder holder;
MediaPlayer MP_completeRequest = new MediaPlayer();
public ListViewAdapter(Activity context, String[] title,
String[] description, String[] req_id, String[] FolderPath,
String[] Arr_RequestTo) {
super();
this.context = context;
this.title = title;
this.description = description;
this.RequestId = req_id;
this.Folderpath = FolderPath;
this.RequestTo = Arr_RequestTo;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
Button record, stop, play, upload;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
glob_position = position;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView
.findViewById(R.id.textView1);
holder.txtViewDescription = (TextView) convertView
.findViewById(R.id.textView2);
holder.record = (Button) convertView.findViewById(R.id.record);
holder.stop = (Button) convertView.findViewById(R.id.stop);
holder.play = (Button) convertView.findViewById(R.id.play1);
holder.upload = (Button) convertView
.findViewById(R.id.audio_upload);
/* set button image */
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
Typeface face = Typeface.createFromAsset(context.getAssets(),
"fonts/tahoma.ttf");
holder.txtViewTitle.setTypeface(face);
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
Please see Holder is used to reuse views in adapterview, so if a Screen can displays 5 items once, 6th item will reuse item no 1's View, hence holder.
Use ListView.onItemClickListener instead of holder.record.setOnClickListener(), or set background of view into getView method, I am editing your code in getVIew method:
public class ListViewAdapter extends BaseAdapter {
public static Activity context;
String title[];
String description[];
String RequestId[];
String Folderpath[];
String RequestTo[];
boolean b_Flag_Record_or_not = false;
boolean b_upload_or_not = false;
boolean start_or_not = false;
boolean start_play = false;
boolean upload_or_not = false;
boolean b_play_or_not = false;
boolean isplaying2 = false;
Thread welcomeThread;
int glob_position;
MediaPlayer mPlayer2;
int flag_stop_position;
AudioRecorder recorder;
AnimationDrawable frameAnimation, frameAnimation_play;
private static String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
Recording login = new Recording();
ViewHolder holder;
MediaPlayer MP_completeRequest = new MediaPlayer();
public ListViewAdapter(Activity context, String[] title,
String[] description, String[] req_id, String[] FolderPath,
String[] Arr_RequestTo) {
super();
this.context = context;
this.title = title;
this.description = description;
this.RequestId = req_id;
this.Folderpath = FolderPath;
this.RequestTo = Arr_RequestTo;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
glob_position = position;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView
.findViewById(R.id.textView1);
holder.txtViewDescription = (TextView) convertView
.findViewById(R.id.textView2);
/* set button image */
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
Typeface face = Typeface.createFromAsset(context.getAssets(),
"fonts/tahoma.ttf");
holder.txtViewTitle.setTypeface(face);
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
Button record = (Button) convertView.findViewById(R.id.record);
Button stop = (Button) convertView.findViewById(R.id.stop);
Button play = (Button) convertView.findViewById(R.id.play1);
Button upload = (Button) convertView
.findViewById(R.id.audio_upload);
record.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isplaying2 == true) {
} else
{
if (b_Flag_Record_or_not == true) {
} else {
try {
record.setBackgroundResource(R.drawable.record_green);
b_Flag_Record_or_not = true;
b_play_or_not = true;
flag_stop_position = position;
AuthHandler dataHandler = new AuthHandler();
AuthDataset dataset = dataHandler
.getParsednewJobdtl_DataSet();
System.out.println("dataset.getint1();"
+ dataset.getint1());
// Login l = new Login();
String str_useid = RequestTo[position];
recorder = new AudioRecorder("/audiometer/shanesh"
+ RequestId[position] + "-" + str_useid);
start_or_not = true;
recorder.start();
Toast.makeText(context, "Start Recording", Toast.LENGTH_LONG).show();
CountDownTimer countDowntimer = new CountDownTimer(
120000000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
try {
Toast.makeText(
context,
"Stop recording Automatically ",
Toast.LENGTH_LONG).show();
recorder.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
countDowntimer.start();
} catch (IOException e) {
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
String s = writer.toString();
}catch (Exception e) {
}
}
}
}
});

Categories

Resources