how to show delivered messge in chatadapter - android

This is my Mainactivity class Code using this code am able to display message sent and failure.
#Override
public void executionResult(int result) {
// TODO Auto-generated method stub
final int delivered=result;
runOnUiThread(new Runnable() {
public void run() {
if(delivered==1)
{
Toast.makeText(getApplicationContext(), "sent", 1000).show();
}
else
{
Toast.makeText(getApplicationContext(), "failure", 1000).show();
}
}
});
}
While I have to display it in adapter I have created field for delivered in text view, but unable to display in adapter when I sent message successfully. Below is code for adapter and xml for that adapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/even_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.lociiapp.utils.RoundedImageView
android:id="#+id/odd_bubble"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_margin="5dip"
android:src="#drawable/index"
android:visibility="visible" />
<com.lociiapp.utils.RoundedImageView
android:id="#+id/user_img"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:layout_margin="5dip"
android:src="#drawable/index"
android:visibility="visible" />
<LinearLayout
android:id="#+id/shareRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_toLeftOf="#+id/user_img"
android:layout_toRightOf="#+id/odd_bubble"
android:background="#drawable/rectangle"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:textColor="#636363"
android:textSize="20sp" />
<TextView
android:id="#+id/delivered"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="#+id/leftimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/shareRow"
android:layout_alignTop="#+id/user_img"
android:layout_marginRight="-11.5dip"
android:src="#drawable/callout_right" />
<ImageView
android:id="#+id/ rightangale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/odd_bubble"
android:layout_marginLeft="19dip"
android:layout_toRightOf="#+id/odd_bubble"
android:src="#drawable/callout_left" />
</RelativeLayout>
delivered textview is the id of adepter where I have to display sent and failure message if 1 is got:
public class ChatAdapter extends ArrayAdapter<Chat> {
private final Context context;
private final ArrayList<Chat> values;
ImageLoader imageloader;
Datamodel dm;
public ChatAdapter(Context context, ArrayList<Chat> values) {
super(context, R.layout.list_row_layout_even, values);
this.context = context;
this.values = values;
imageloader = new ImageLoader(context);
}
public void addMessage(Chat chat) {
values.add(chat);
this.notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row_layout_odd,
parent, false);
RelativeLayout root = (RelativeLayout) convertView
.findViewById(R.id.even_container);
TextView tv = (TextView) convertView.findViewById(R.id.text);
TextView delivred = (TextView) convertView.findViewById(R.id.delivered);
RoundedImageView userImg = (RoundedImageView) convertView
.findViewById(R.id.user_img);
RoundedImageView oddImg = (RoundedImageView) convertView
.findViewById(R.id.odd_bubble);
ImageView leftimage = (ImageView) convertView
.findViewById(R.id.leftimage);
ImageView rightimage = (ImageView) convertView
.findViewById(R.id.rightangale);
Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
"fonts/ARIAL.TTF");
SharedPreferences prefs = context.getSharedPreferences(
AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
String K = prefs.getString("Member_id", "");
Chat chat = values.get(position);
String t = prefs.getString("PREFS_NAME_reccvierChatadpter", "");
tv.setText(chat.getMessage());
tv.setTypeface(fontArial);
AQuery aq = new AQuery(context);
if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
root.setBackgroundColor(Color.parseColor("#ffffff"));
tv.setTextColor(Color.parseColor("#636363"));
userImg.setVisibility(View.VISIBLE);
rightimage.setVisibility(View.GONE);
leftimage.setVisibility(View.VISIBLE);
aq.id(userImg).image(
"http://api.lociiapp.com/TransientStorage/" + K + ".jpg");
oddImg.setVisibility(View.GONE);
} else {
root.setBackgroundColor(Color.parseColor("#f5f6f1"));
tv.setTextColor(Color.parseColor("#bdbdbd"));
aq.id(oddImg).image(
"http://api.lociiapp.com/TransientStorage/" + t + ".jpg");
userImg.setVisibility(View.GONE);
oddImg.setVisibility(View.VISIBLE);
leftimage.setVisibility(View.GONE);
rightimage.setVisibility(View.VISIBLE);
}
return convertView;
}
}
Here is the code of adapter. Please help and suggest me where we have to change I am not able to fix this issue.

Try to use Handler instead of runOnUiThread. Have you Tested that you are getting the Result in "executionResult(int result)"?

Related

Item missing from android grid view on scroll

I have grid view , on which each item consists of few images and text . When it loads for the first time , everything is fine but if I scroll to the bottom and the go back to top some two images are gone from a item , and it goes randomly .
here is the code for Grid view
<GridView
android:id="#+id/testR_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:clickable="true"
android:columnWidth="#dimen/gridview_column_width"
android:horizontalSpacing="#dimen/grid_horizontal_spacing"
android:listSelector="#drawable/gridview_background"
android:numColumns="auto_fit"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:verticalScrollbarPosition="right"
android:verticalSpacing="#dimen/grid_vertical_spacing"
android:fastScrollEnabled="false"/>
Here the code for single item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_marginTop="5dp"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blurry_shadow_rect">
<com.joooonho.SelectableRoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/gridview_column_width"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="-5dp"
app:sriv_left_bottom_corner_radius="0dip"
app:sriv_left_top_corner_radius="4dip"
app:sriv_right_bottom_corner_radius="0dip"
app:sriv_right_top_corner_radius="4dip" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/item_image"
android:layout_marginBottom="5dp"
android:layout_marginLeft="2dp"
android:orientation="horizontal"
android:weightSum="100">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="35">
<TextView
android:id="#+id/item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="start"
android:layout_marginBottom="5dp"
android:ellipsize="end"
android:fontFamily="sans-serif-light"
android:maxLines="1"
android:text=""
android:textColor="#color/bodyText"
android:textSize="#dimen/GridHeader" />
<TextView
android:id="#+id/item_TestA_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/item_text"
android:layout_marginBottom="10dp"
android:alpha=".5"
android:ellipsize="end"
android:maxLines="1"
android:text=""
android:textColor="#color/bodyText"
android:textSize="#dimen/GridHeader2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight="65">
<TextView
android:id="#+id/item_testR_free_text"
style="#style/textview_grid_free_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:fontFamily="sans-serif-light"
android:maxLines="1"
android:padding="2dp"
android:singleLine="true"
android:text=" FREE " />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/already_owned"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="5dp"
android:visibility="gone"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_testR_bought_36dp" />
<ImageButton
android:id="#+id/item_testR_more_options"
android:layout_width="#dimen/image_button_more"
android:layout_height="#dimen/image_button_more"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/item_testR_free_text"
android:alpha=".5"
android:background="#drawable/imagebutton_background"
android:clickable="true"
android:focusable="false"
android:src="#drawable/ic_more_vert_black_24dp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
The elements are randomly missing are text view which id is item_testR_free_text and imageview which id is already_owned . One thing also to be noted the visibility of these two items are conditional .
Here is my code for adapter
public class TestRGridViewAdapter extends ArrayAdapter<TestR> {
Context context;
int layoutResourceId;
List<TestR> data = new ArrayList<TestR>();
RecordHolder holder = null;
//DiskCache imgCache;
TestA TestA;
TestR item;
FragmentActivity mFragmentActivity;
boolean is_local = false;
List<Track> trackList;
public TestR getItem() {
return item;
}
public void setItem(TestR item) {
this.item = item;
}
public TestRGridViewAdapter(Context context, int layoutResourceId,
List<TestR> data, FragmentActivity mFragmentActivity, boolean is_local) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.mFragmentActivity = mFragmentActivity;
// imgCache = Parameters.imgCache;
this.is_local = is_local;
}
public TestRGridViewAdapter(Context context, int layoutResourceId,
List<TestR> data, FragmentActivity mFragmentActivity) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.mFragmentActivity = mFragmentActivity;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
//LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = LayoutInflater.from(context).inflate(layoutResourceId, null);
//row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.item_testR_free_text = (TextView) row.findViewById(R.id.item_testR_free_text);
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.txtTestAName = (TextView) row.findViewById(R.id.item_TestA_name);
holder.imageItem = (SelectableRoundedImageView) row.findViewById(R.id.item_image);
holder.item_testR_more_options = (ImageButton) row.findViewById(R.id.item_testR_more_options);
holder.already_owned = (ImageView) row.findViewById(R.id.already_owned);
// holder.grid_swipe_refresh_layout = (SwipeRefreshLayout) row.findViewById(R.id.grid_swipe_refresh_layout);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
item = data.get(position);
holder.txtTitle.setText(item.getName());
// else
// holder.txtArtitName.setText("");
//holder.imageItem.setImageBitmap(item.getImage());
try {
if (item.getTestA_id() == null)
item = new SaveData(context).get_online_testR(item.getTestR_id());
TestA = Helper.getDaoSession(context).getTestADao().load(item.getTestA_id());
if (TestA == null) {
TestA = new SaveData(context).get_save_TestA(item.getTestA_id());
}
// if (TestA != null)
holder.txtTestAName.setText((TestA != null) ? TestA.getName() : "");
// else
// TestA = new SaveData(context).get_save_TestA(item.getTestA_id());
if (!is_local) {
if(!DBQuery.is_owner(context,item.getTestR_id())) {
Log.d("test","not owner - "+item.getName());
holder.item_testR_free_text.setText(
UXHelper.getPriceFromString(Parameters.price_type.equals("local_price") ?
item.getLocal_price().toString() : item.getPrice().toString()));
holder.already_owned.setVisibility(View.INVISIBLE);
holder.item_testR_more_options.setOnClickListener(new testR_popup_onClick(context, item, holder.item_testR_more_options, mFragmentActivity, TestA));
}else {
Log.d("test","owner - "+item.getName());
holder.item_testR_free_text.setVisibility(View.INVISIBLE);
holder.already_owned.setVisibility(View.VISIBLE);
holder.item_testR_more_options.setOnClickListener(new testR_popup_onClick(context, item, holder.item_testR_more_options, mFragmentActivity, TestA));
}
} else {
Log.d("test","local - "+item.getName());
holder.already_owned.setVisibility(View.INVISIBLE);
boolean synced = true;
trackList = new ArrayList<Track>();
List<Track> trackListTmp = new SaveData(context).get_tracks_local(item.getTestR_id());
boolean testR_owner = DBQuery.is_owner(context, trackListTmp.get(0).getTestR_id());
for (Track t : trackListTmp) {
boolean track_owner = DBQuery.is_owner(context, t.getTrack_id(), t.getTestR_id());
if (track_owner)
trackList.add(t);
if (testR_owner || track_owner) {
if (t.getSynced_dir() == null) {
synced = false;
// break;
} else if (!new File(t.getSynced_dir()).exists()) {
synced = false;
// break;
}
}
}
if (synced) {
Log.d("test","synched - "+item.getName());
holder.item_testR_free_text.setVisibility(View.VISIBLE);
holder.item_testR_free_text.setText("Synced");
} else {
Log.d("test","not synched - "+item.getName());
holder.item_testR_free_text.setVisibility(View.INVISIBLE);
holder.item_testR_more_options.setOnClickListener(
new testR_popup_downloaded_onCLick(context, item, holder.item_testR_more_options, mFragmentActivity, TestA,
trackList.toArray(new Track[trackList.size()]), synced));
}
}
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
holder.imageItem.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
holder.imageItem.setImageResource(R.drawable.ic_example_36);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
holder.imageItem.setImageResource(R.drawable.ic_example_36);
}
};
if (Parameters.mPicasso == null)
Parameters.mPicasso = new Picasso.Builder(context)
.build();
Parameters.mPicasso.load("http://" + context.getString(R.string.ip) + "/" +
context.getString(R.string.TestRsController) + "/" + context.getString(R.string.TESTR_THUMB_URL)
+ "/" + item.getTestR_id()+"/"+Parameters.dpi)
.placeholder(R.drawable.ic_example_36)
.error(R.drawable.ic_example_36)
.into(holder.imageItem);
} catch (Exception ex) {
new Logger(context).appendLog("Error in TestR grid view item " + ex.getMessage());
}
return row;
}
static class RecordHolder {
// SwipeRefreshLayout grid_swipe_refresh_layout;
TextView txtTitle;
TextView txtTestAName;
SelectableRoundedImageView imageItem;
TextView item_testR_free_text;
ImageButton item_testR_more_options;
ImageView already_owned;
}
}

How do I click individual controls within a list view item without affecting the controls in other list view item?

I'm developing an android application for our music school. I have designed a layout with a list view containing 6 items, the controls in each item are sourced from different layout file.
Layout file containing the list view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F8AE9F"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/aboutComposer"
android:layout_width="wrap_content"
android:layout_height="45sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|center_horizontal"
android:text="#string/msv_songs"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvCartTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/cart_total"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCartTotal"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:src="#drawable/icon_pay_now" />
<ListView
android:id="#+id/lstMSVSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:focusable="false" >
</ListView>
</RelativeLayout>
Layout file that is the source of controls for each list view item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tvViewSample"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imgFilmPoster"
android:layout_width="75sp"
android:layout_height="75sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/abc_ab_bottom_solid_dark_holo" />
<TextView
android:id="#+id/tvSongName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvFilmName"
android:layout_below="#+id/tvFilmName"
android:text="#string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvYearReleased"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvSongName"
android:layout_below="#+id/tvSongName"
android:text="#string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvYearReleased"
android:layout_below="#+id/tvYearReleased"
android:text="#string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/imgFilmPoster"
android:focusable="true"
android:text="#string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="#+id/imgAddCart"
android:layout_width="35sp"
android:layout_height="35sp"
android:contentDescription="#string/add_to_cart"
android:layout_alignTop="#+id/imgView"
android:layout_toRightOf="#+id/imgView"
android:src="#drawable/add_to_cart"
android:tag = "#string/add_to_cart" />
<ImageView
android:id="#+id/imgView"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_below="#+id/imgFilmPoster"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/imgPlay"
android:src="#drawable/view_icon" />
<ImageView
android:id="#+id/imgPlay"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_alignLeft="#+id/tvPrice"
android:layout_alignTop="#+id/imgView"
android:contentDescription="#string/play_mp3"
android:src="#drawable/play" />
</RelativeLayout>
Java code for loading data for individual list items and handling image view click events
private List<Song> msvSongs = new ArrayList<Song>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.msv_music_sheets);
populateSongsList();
populateSongsListView();
}
private void populateSongsList() {
msvSongs.add(new Song(R.drawable.msv_ayirathil_oruvan,"Ayirathil Oruvan","Adho Andha Paravai Pola","1965",150,R.raw.msv_adho_andha_paravai_pola_ao));
msvSongs.add(new Song(R.drawable.msv_karuppu_panam,"Karuppu Panam","Aadavaralaam","1964",150,R.raw.msv_aadavaralaam_kp));
msvSongs.add(new Song(R.drawable.msv_paalum_pazhamum,"Paalum Paazhamum","Aalayamaniyin Oosai","1961",150,R.raw.msv_aalayamaniyin_oosai_pp));
msvSongs.add(new Song(R.drawable.msv_paava_mannippu,"Paava Mannipu","Athaan Ennathan","1961",150,R.raw.msv_athaan_ennathaan_pm));
msvSongs.add(new Song(R.drawable.msv_periya_idhuthu_pen,"Periya Idhuthu Pen","Andru Vandhadhadhum Adhey Nila","1963",175,R.raw.msv_andru_vandhadhum_adhey_nila_pip));
msvSongs.add(new Song(R.drawable.msv_puthiya_paravai,"Pudhiya Paravai","Enge Nimmadhi","1964",250,R.raw.msv_enge_nimmadhi_pparavai));
//msvSongs.add(new Song(R.drawable.msv_server_sundaram,"Server Sundaram","Avalukku Enna Azhagiya Mugam","1964",150,R.raw.msv_avalukkenna_azhagiamugham_ss));
}
private void populateSongsListView() {
ArrayAdapter<Song> msvSongs = new MSVSongsAdapter();
ListView songsList = (ListView) findViewById(R.id.lstMSVSongs);
songsList.setAdapter(msvSongs);
}
private class MSVSongsAdapter extends ArrayAdapter<Song>{
public MSVSongsAdapter(){
super(MsvSongs.this, R.layout.songs_view, msvSongs);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.songs_view, parent, false);
}
for(int i=0;i<msvSongs.size();i++){
//Find a song to display
final Song currentSong = msvSongs.get(position);
//Fill the FilmPoster View
ImageView filmPoster = (ImageView)itemView.findViewById(R.id.imgFilmPoster);
filmPoster.setImageResource(currentSong.getFilmImageID());
//Fill the FilmName View
TextView filmName = (TextView)itemView.findViewById(R.id.tvFilmName);
filmName.setText(currentSong.getMovieName());
//Fill the SongName View
TextView songName = (TextView)itemView.findViewById(R.id.tvSongName);
songName.setText(currentSong.getSongName());
//Fill the YearReleased View
TextView yearReleased = (TextView)itemView.findViewById(R.id.tvYearReleased);
yearReleased.setText(currentSong.getYearReleased());
//Fill the Price View
TextView songPrice = (TextView)itemView.findViewById(R.id.tvPrice);
songPrice.setText("Rs. "+currentSong.getSongPrice());
//Fill the Play MP3 View
final ImageView viewMP3 = (ImageView)itemView.findViewById(R.id.imgPlay);
viewMP3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String contentDesc = (String) viewMP3.getContentDescription();
if(contentDesc.contains("Play")){
if (mp3Player != null && mp3Player.isPlaying()){
Toast.makeText(MsvSongs.this, "Stop the previous song", Toast.LENGTH_SHORT).show();
}else{
mp3Player=MediaPlayer.create(MsvSongs.this, currentSong.getmp3File());
mp3Player.start();
viewMP3.setImageResource(drawable.stop);
viewMP3.setContentDescription("Stop MP3");
}
}else{
mp3Player.pause();
mp3Player.stop();
mp3Player.release();
mp3Player=null;
viewMP3.setImageResource(drawable.play);
viewMP3.setContentDescription("Play MP3");
}
}
});
When I click the Play button of a song inside a list view, it plays the song and changes the Play image to Stop image. But it also changes the Play image to Stop image of other list view item.
Could you please advice what is wrong in the above code?
Try this way,hope this will help you to solve your problem.
private class MSVSongsAdapter extends BaseAdapter {
private Context context;
private ArrayList<Song> songList;
private MediaPlayer mp3Player;
public MSVSongsAdapter(Context context, ArrayList<Song> songList) {
this.context = context;
this.songList = songList;
}
#Override
public int getCount() {
return songList.size();
}
#Override
public Object getItem(int position) {
return songList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.songs_view,null);
holder.filmPoster = (ImageView) convertView.findViewById(R.id.imgFilmPoster);
holder.viewMP3 = (ImageView) convertView.findViewById(R.id.viewMP3);
holder.filmName = (TextView) convertView.findViewById(R.id.filmName);
holder.songName = (TextView) convertView.findViewById(R.id.songName);
holder.yearReleased = (TextView) convertView.findViewById(R.id.yearReleased);
holder.songPrice = (TextView) convertView.findViewById(R.id.songPrice);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.filmPoster.setImageResource(songList.get(position).getFilmImageID());
holder.viewMP3.setImageResource(songList.get(position).getFilmImageID());
holder.filmName.setText(songList.get(position).getMovieName());
holder.songName.setText(songList.get(position).getSongName());
holder.yearReleased.setText(songList.get(position).getYearReleased());
holder.songPrice.setText("Rs. " + songList.get(position).getSongPrice());
holder.viewMP3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String contentDesc = (String) songList.get(position).getContentDescription();
if (contentDesc.contains("Play")) {
if (mp3Player != null && mp3Player.isPlaying()) {
Toast.makeText(context, "Stop the previous song", Toast.LENGTH_SHORT).show();
} else {
mp3Player = MediaPlayer.create(context, songList.get(position).getmp3File());
mp3Player.start();
((ImageView)v).setImageResource(R.drawable.stop);
((ImageView)v).setContentDescription("Stop MP3");
}
} else {
mp3Player.pause();
mp3Player.stop();
mp3Player.release();
mp3Player = null;
((ImageView)v).setImageResource(R.drawable.play);
((ImageView)v).setContentDescription("Play MP3");
}
}
});
return null;
}
static class ViewHolder{
ImageView filmPoster;
ImageView viewMP3;
TextView filmName;
TextView songName;
TextView yearReleased;
TextView songPrice;
}
}

change imagebutton resource in listview

Im working on simple feedreader app. in my main activity i have listview that show all items . in each row i have imagebutton for save that item and when user click on that the image of imagebutton must be change(like fav icon on twitter) but it does not change ! After scrolling changes are observed !
this is my row layout .
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="#+id/tvListViewItemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:text="#string/listview_item_title"
android:textSize="25dp" />
<TextView
android:id="#+id/tvListViewItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvListViewItemTitle"
android:layout_below="#+id/tvListViewItemTitle"
android:layout_marginTop="20dp"
android:text="#string/listview_item_text"
android:textSize="18dp" />
<TextView
android:id="#+id/tvListViewItemTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvListViewItemText"
android:layout_below="#+id/tvListViewItemText"
android:layout_marginTop="22dp"
android:paddingTop="5dp"
android:text="#string/listview_item_time" />
<ImageButton
android:id="#+id/ibListViewItemSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/tvListViewItemTime"
android:layout_marginLeft="20dp"
android:paddingTop="11dp"
android:src="#drawable/icon_save_story_2x"
android:background="#null" />
and this is my custom adapter class.
public class FeedListAdapter extends ArrayAdapter<FeedItem> {
private final Activity context;
private final List<FeedItem> list;
public FeedListAdapter(Activity context, List<FeedItem> list) {
super(context, R.layout.feed_listview_item_type1, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
TextView tvTitle;
TextView tvText;
TextView tvTime;
ImageButton ibSave;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.feed_listview_item_type1, parent,false);
// configure view holder
ViewHolder holder = new ViewHolder();
holder.tvTitle = (TextView)rowView.findViewById(R.id.tvListViewItemTitle);
holder.tvText = (TextView)rowView.findViewById(R.id.tvListViewItemText);
holder.tvTime = (TextView)rowView.findViewById(R.id.tvListViewItemTime);
holder.ibSave = (ImageButton)rowView.findViewById(R.id.ibListViewItemSave);
rowView.setTag(holder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
final FeedItem feedItem = list.get(position);
holder.tvTitle.setText(feedItem.getTitle());
holder.tvText.setText(feedItem.getText());
holder.tvTime.setText(feedItem.getTime());
if(feedItem.isSave()) holder.ibSave.setImageResource(R.drawable.icon_saved_story_2x);
else holder.ibSave.setImageResource(R.drawable.icon_save_story_2x);
final String msg = " "+(position+1);
holder.ibSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(feedItem.isSave()){
feedItem.setSave(false);
Toast.makeText(context,
msg+"deleted", Toast.LENGTH_SHORT).show();
//holder.ibSave.setImageResource(R.drawable.icon_save_story_2x);
}else{
feedItem.setSave(true);
Toast.makeText(context,
msg+"saved", Toast.LENGTH_SHORT).show();
//holder.ibSave.setImageResource(R.drawable.icon_saved_story_2x);
}
}
});
return rowView;
}
}
Call notifyDataSetChanged in your onClick after the if else.
At present the FeedListAdapter does not realize that you have made some changes to the list

ListView displaying nothing with custom adapter

As the title states, I'm having some troubles getting my custom listview to work properly. The app displays nothing on the list, and gives just a blank white screen. I tested my data with a simple list I already have setup, and that worked just fine. I'm hoping someone can see something I haven't. Thanks.
History.java
public class History {
public String score;
public String gametype;
public int icon;
public History() {
super();
}
public History(String score, String gametype, int icon) {
super();
this.score = score;
this.gametype = gametype;
this.icon = icon;
}
}
HistoryAdapter.java
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
Implementation
History[] historyData = new History[games.length()];
for(int i = 0; i < games.length(); i++) {
JSONObject c = games.getJSONObject(i);
JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
type[i] = c.getString(TAG_TYPE);
champId[i] = c.getString("championId");
cs[i] = gameStats.getString("minionsKilled");
kills[i] = gameStats.getString("championsKilled");
deaths[i] = gameStats.getString("numDeaths");
assists[i] = gameStats.getString("assists");
win[i] = gameStats.getString("win");
if(win[i].equals("true"))
win[i] = "Victory";
else
win[i] = "Defeat";
if(type[i].equals("RANKED_SOLO_5x5"))
type[i] = "Ranked (Solo)";
if(type[i].equals("CAP_5x5"))
type[i] = "TeamBuilder";
if(type[i].equals("NORMAL"))
type[i] = "Unranked";
score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
}
if(historyData == null) {
historyData[0] = new History("No game found", "N/A", R.drawable.ic_launcher);
Log.i("Data", "" + historyData);
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:background="#111111">
</ListView>
list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#111111"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/score"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="0/0/0 KDA"
android:textSize="12sp" />
<TextView
android:id="#+id/gameType"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/score"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
The code you've included seems fine, so the only point where it can fail is in the loop where you create the historyData array.
For some reason you might not be processing it correctly, for instance, some JSON attributes not being defined sometimes.
You can use a try block and catch a JSONException to see what's wrong, and also add several Log.d() sentences to know where's the culprit.
For instance:
try {
cs[i] = gameStats.getString("minionsKilled");
}
catch (JSONException) { e.printStackTrace(); }
I guess you are using wrong XML
Change
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,historyData);
to
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_item,historyData);
and make sure that your historyData is not null

Having an issue with a custom listview adapter

As the title states, I'm having some troubles getting my custom listView adapter to work. The app doesn't crash, but it also displays nothing on the list. I tested my data with a simple list I already have setup, and that worked just fine.
History.java
public class History {
public String score;
public String gametype;
public int icon;
public History() {
super();
}
public History(String score, String gametype, int icon) {
super();
this.score = score;
this.gametype = gametype;
this.icon = icon;
}
}
HistoryAdapter.java
public class HistoryAdapter extends ArrayAdapter<History> {
Context context;
int layoutResId;
History data[] = null;
public HistoryAdapter(Context context, int layoutResId, History[] data) {
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HistoryHolder holder = null;
if(convertView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResId, parent, false);
holder = new HistoryHolder();
holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon);
holder.textTitle = (TextView)convertView.findViewById(R.id.gameType);
holder.textScore = (TextView)convertView.findViewById(R.id.score);
convertView.setTag(holder);
}
else
{
holder = (HistoryHolder)convertView.getTag();
}
History history = data[position];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
return convertView;
}
static class HistoryHolder
{
ImageView imageIcon;
TextView textTitle;
TextView textScore;
}
}
Implementation
for(int i = 0; i < games.length(); i++) {
JSONObject c = games.getJSONObject(i);
JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS);
type[i] = c.getString(TAG_TYPE);
champId[i] = c.getString("championId");
cs[i] = gameStats.getString("minionsKilled");
kills[i] = gameStats.getString("championsKilled");
deaths[i] = gameStats.getString("numDeaths");
assists[i] = gameStats.getString("assists");
win[i] = gameStats.getString("win");
if(win[i].equals("true"))
win[i] = "Victory";
else
win[i] = "Defeat";
if(type[i].equals("RANKED_SOLO_5x5"))
type[i] = "Ranked (Solo)";
if(type[i].equals("CAP_5x5"))
type[i] = "TeamBuilder";
if(type[i].equals("NORMAL"))
type[i] = "Unranked";
score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i];
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:background="#111111">
</ListView>
list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#111111"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/score"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="0/0/0 KDA"
android:textSize="12sp" />
<TextView
android:id="#+id/gameType"
android:textColor="#C49246"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/score"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
You are creating a History object with this line:
new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image
This just calls the constructor and creates an object for the garbage collector to take away again.
You are not actually adding this History object to the historyData array.
Before the loop, you should declare the historyData array as:
History[] historyData = new History[games.length();
And in the loop, you should assign the created object to the array
historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image

Categories

Resources