StartActivityForResult to update a row in a ListView - android

I've the following Activity that uses a ListView to display a list of subjects. Each row has a textview which is set to red to indicate the question for that subject has not yet been answered. When the user clicks on a row it should launch an Activity that asks a question on that subject.
I want to launch the Activity by calling startActivityForResult, this will update the row with the fact the question has been asked and turn the textview green.
My question is how to update a particular row in the listview from onActivityResult?
public class QuestionListForInTransaction extends ActionBarActivity {
ListView listView;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.questionlistforintxlayout);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
setTitle("Call Question(s)");
listView = (ListView)findViewById(R.id.txinquestionlist);
String[] values = new String[] { "Are you the driver?", "Question 2", "Question 3", };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(listener);
}//end of onCreate
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.questionlisttxinrowlayout, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.questionlisttxinrowlayout, parent, false);
TextView question = (TextView) rowView.findViewById(R.id.tvquestionforintx);
question.setText(values[position]);
TextView answered = (TextView) rowView.findViewById(R.id.rowstatusbox);
answered.setBackgroundColor(Color.RED);
String questionStr = values[position];
rowView.setTag(questionStr);
return rowView;
}
} //end of adapter class
OnItemClickListener listener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(QuestionListForInTransaction.this, "pos = " + position, Toast.LENGTH_LONG).show();
String question = (String)view.getTag();
if(question.equalsIgnoreCase("Are you the driver?")){
//Toast.makeText(QuestionListForInTransaction.this, "Are you the driver?" + position, Toast.LENGTH_LONG).show();
final int QUESTION_REQUEST = 1;
Intent questionIntent = new Intent(QuestionListForInTransaction.this, Question.class);
startActivityForResult(questionIntent, QUESTION_REQUEST);
//TextView answered = (TextView) view.findViewById(R.id.rowstatusbox);
//answered.setBackgroundColor(Color.GREEN);
}
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
if(extras != null)
int result = extras.getInt("result");
//*********************update a row green**********************
}
} //end of class

Just Create some custom wrapper class like this:
public class QuestionWrapper {
private String mQuestionText;
private boolean mIsAsked;
public QuestionWrapper(String defaultQuestionText, boolean isAsked) {
mQuestionText = defaultQuestionText;
mIsAsked = isAsked;
}
public boolean isAsked() {
return mIsAsked;
}
public void setIsAswked(boolean isAsked) {
mIsAsked = isAsked;
}
public String getQuestionText() {
return mQuestionText;
}
}
then you need to set List of this wrappers to Adapter:
public class QuestionListForInTransaction extends ActionBarActivity {
List<QuestionWrapper> mWrappers = new ArrayList<>();
MySimpleArrayAdapter mAdapter;
....
public void onCreate(Bundle icicle) {
....
wrappers.add(new QuestionWrapper("Are you the driver?",false));
wrappers.add(new QuestionWrapper("Question 2",false));
wrappers.add(new QuestionWrapper("Question 3",false));
mAdapter = new MySimpleArrayAdapter(this, wrappers);
....
and Update Your Adapter like this:
public class MySimpleArrayAdapter extends ArrayAdapter<QuestionWrapper> {
private final Context context;
private final List<QuestionWrapper> mWrappers;
public MySimpleArrayAdapter(Context context, List<QuestionWrapper> wrappers) {
super(context, R.layout.questionlisttxinrowlayout, wrappers);
this.context = context;
this.mWrappers = wrappers;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.questionlisttxinrowlayout, parent, false);
QuestionWrapper wrapper = mWrappers.get(position);
TextView question = (TextView) rowView.findViewById(R.id.tvquestionforintx);
question.setText(wrapper.getQuestionText());
TextView answered = (TextView) rowView.findViewById(R.id.rowstatusbox);
if (wrapper.isAsked()) {
answered.setBackgroundColor(Color.GREEN);
} else {
answered.setBackgroundColor(Color.RED);
}
rowView.setTag(wrapper.getQuestionText());
return rowView;
}
onActivityResult - Need to update your wrapper class and adpater:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
....
mWrappersList.get(yourPosition).setAsked(true/false);
if (mAdapter!=null){
mAdapter.notifyDataSetChanged();
}
...
}
I think for RequestCode in startActivityForResult you can use Adapter click position.

Related

How to Switch Activities using a Custom ListView Adapter?

I'm trying to use a Custom ListView Adapter to switch to different activities, but am having trouble doing so, as there is an error that I can't seem to get rid of.
Relevant code
Category Table:
public class CategoryTable extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_table);
populateTable();
goToPreviousActivity();
}
private void populateTable() {
final ListView mListView = findViewById(R.id.listView);
Category 1 = new Category(" One");
Category 2 = new Category(" Two");
Category 3 = new Category(" Three");
Category 4 = new Category(" Four");
final ArrayList<Category> categoryList = new ArrayList<>();
categoryList.add(1);
categoryList.add(2);
categoryList.add(3);
categoryList.add(4);
CategoryListAdapter adapter = new CategoryListAdapter(this, R.layout.adapter_view_layout, categoryList);
mListView.setAdapter(adapter);
}
private void goToPreviousActivity() {
ImageButton btn = findViewById(R.id.backButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
public static Intent makeIntent(Context context) {
return new Intent(context, CategoryTable.class);
}
}
Category List Adapter:
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
final LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
catName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (position == 0) {
//error -> makeIntent(android.content.Context in AllToolsTable cannot be applied to com.test.test.CategoryListAdapter
Intent intent = AllToolsTable.makeIntent(CategoryListAdapter.this);
mContext.startActivity(intent);
} else {
Toast toast = Toast.makeText(getContext(), "Clicked2", Toast.LENGTH_SHORT);
toast.show();
}
}
});
return convertView;
}
}
(I commented above the erroneous piece of code)
All Tools Table:
public class AllToolsTable extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_tools_table);
}
public static Intent makeIntent(Context context) {
return new Intent(context, AllToolsTable.class);
}
}
I will provide any other relevant code/details if needed.
Any help will be greatly appreciated.
//error -> makeIntent(android.content.Context in AllToolsTable cannot be applied to com.test.test.CategoryListAdapter
Intent intent = AllToolsTable.makeIntent(CategoryListAdapter.this);
Your AllToolsTable.makeIntent() method requires a Context argument. Normally you'd be fine with something like AllToolsTable.makeIntent(MainActivity.this) because Activity is a subclass of Context. However, CategoryListAdapter is not.
So you need to get your hands on a Context object somehow.
Luckily, the getView() method passes you a non-null ViewGroup parent object, and all views have a context. So you can replace your intent creation with this code:
Context context = parent.getContext();
Intent intent = AllToolsTable.makeIntent(context);

Android Listview does not refresh/redraw

Problem - I have list of in app purchases loaded from app store in a Listview.
When user purchases (taps on a button in listview) the item is purchased and the button should be now hidden and the list item purchased should show an image of tick.
What I have tried -
Refresh the listview after item is purchased by calling notifyDataSetChanged
Set the visibility of the button and image using View.GONE and View.VISIBLE
None of the above seems to work.
I am using this in app billing library
public class ListViewAdapter extends ArrayAdapter<Product> {
private ArrayList<Product> iapProducts= new ArrayList<>();
private final LayoutInflater inflater;
private final Activity activity;
public ArrayList<Product> getIapProducts() {
return iapProducts;
}
public void setIapProducts(ArrayList<Product> iapProducts) {
this.iapProducts = iapProducts;
}
public ListViewAdapter(final Activity context) {
super(context, 0);
inflater = LayoutInflater.from(context);
this.activity = context;
}
#Override
public int getCount() {
return this.iapProducts.size();
}
public String getItem(String position) {
return position;
}
#Override
public boolean hasStableIds(){
return true;
}
public void addAll(ArrayList<Product> iapProducts){
this.iapProducts.addAll(iapProducts);
notifyDataSetChanged();
}
public static class ViewHolder {
public TextView name;
public ImageView imageView;
public Button purchaseButton;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.names_filter_row, null);
holder = new ViewHolder();
holder.name = (TextView) vi.findViewById(R.id.languageName);
holder.imageView = (ImageView) vi.findViewById(R.id.imgTick);
holder.purchaseButton = (Button) vi.findViewById(R.id.iapProductPurchaseBtn);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
final Product product = iapProducts.get(position);
holder.name.setText(product.getTitle());
holder.purchaseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemPurchased(v);
}
});
holder.iapProductId.setText(product.getProductId());
holder.purchaseButton.setText(product.getPriceText());
holder.imageView.setVisibility(View.GONE);
if(product.isPurchased()){
holder.purchaseButton.setVisibility(View.GONE);
holder.imageView.setVisibility(View.VISIBLE);
}else{
holder.purchaseButton.setVisibility(View.VISIBLE);
holder.imageView.setVisibility(View.GONE);
}
return vi;
}
private void onItemPurchased(View v){
final RelativeLayout parentLayout = (RelativeLayout)v.getParent();
final TextView productId = (TextView)parentLayout.findViewById(R.id.iapProductId);
final ImageView languageCheck = (ImageView)parentLayout.findViewById(R.id.imgTick);
PurchaseActivity purchaseActivity = (PurchaseActivity) this.activity;
purchaseActivity.purchaseItem(productId.getText().toString(), v);
}
}
public class PurchaseActivity extends AppCompatActivity implements BillingProcessor.IBillingHandler {
private BillingProcessor bp;
boolean isOneTimePurchaseSupported;
private ArrayList<Product> productsToPurchase = new ArrayList<>();
private final static String TAG = PurchaseActivity.class.getName();
private ListViewAdapter adapter;
private ImageView imageTick;
private Button buttonPurchase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase);
bp = BillingProcessor.newBillingProcessor(this, "billingKey", this); // doesn't bind
bp.initialize(); // binds
adapter = new ListViewAdapter(this);
nameFilterListView.setAdapter(adapter);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
#Override
public void onBillingInitialized() {
isOneTimePurchaseSupported = bp.isOneTimePurchaseSupported();
if(isOneTimePurchaseSupported){
loadIAPData(false);
}else{
Toast.makeText(this,"Billing not supported",Toast.LENGTH_LONG).show();
}
}
private void loadIAPData(boolean showProgressDialog){
bp.loadOwnedPurchasesFromGoogle();
final List<SkuDetails> productDetails = bp.getPurchaseListingDetails(productList);
if(productDetails!= null && !productDetails.isEmpty()){
//fetch products and add to the list
productsToPurchase.add(product);
}
//add all the products to adapter
adapter.addAll(productsToPurchase);
}
}
public void purchaseItem(#NonNull final String productId, final View imageTick, final View button){
this.imageTick = (ImageView) imageTick;
this.buttonPurchase = (Button) button;
bp.purchase(this,productId);
}
#Override
public void onProductPurchased(String productId, TransactionDetails details) {
//hide button show image
this.imageTick.setVisibility(View.VISIBLE);
this.buttonPurchase.setVisibility(View.GONE);
adapter.setIapProducts(new ArrayList<>(productsToPurchase));
adapter.notifyDataSetChanged();
}
}
(Moved comment into a post)
After a user purchases something, is that Product within productsToPurchase updated? The adapter looks okie so maybe the list you're using isn't being updated after a purchase.
I'm assuming you need to flip the isPurchased flag of the Product in onProductPurchased() using the provided productId.

Passing data in from list to detail view in Android

I have a list view populated threw an SQlitedatabase but I need to pass to a detail activity from the list view. The problem is in passing the details from the listview activity to the detail activity because when I click the detail activity it gives me blank edit texts
Here is my listview activity:
public class consulter_note extends Activity implements AdapterView.OnItemClickListener{
ListView list;
SQLiteDatabase sqLiteDatabase;
DataBaseOperationstwo dataBaseOperationstwo;
Cursor cursor;
ListDataAdapter listDataAdapter;
String titre,objet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consulter_note);
list = (ListView) findViewById(R.id.listView);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.notelist_row);
list.setAdapter(listDataAdapter);
list.setOnItemClickListener(this);
dataBaseOperationstwo = new DataBaseOperationstwo(getApplicationContext());
sqLiteDatabase = dataBaseOperationstwo.getReadableDatabase();
cursor = dataBaseOperationstwo.getInformations(sqLiteDatabase);
if (cursor.moveToFirst())
{
do
{
titre = cursor.getString(0);
objet = cursor.getString(1);
DataProvider dataProvider = new DataProvider(titre,objet);
listDataAdapter.add(dataProvider);
}while (cursor.moveToNext());
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, note_details.class);
startActivity(intent);
intent.putExtra("titre", titre);
intent.putExtra("objet", objet);
}
}
And here is my array adapter:
public class ListDataAdapter extends ArrayAdapter{
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler
{
TextView TITRE,OBJET;
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.notelist_row,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.TITRE = (TextView) row.findViewById(R.id.titredemo);
layoutHandler.OBJET = (TextView) row.findViewById(R.id.objetdemo);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.TITRE.setText(dataProvider.getTitre());
layoutHandler.OBJET.setText(dataProvider.getObjet());
return row;
}
}
The data provider class used in the array adapter:
public class DataProvider {
private String titre,objet;
public DataProvider(String titre,String objet)
{
this.titre = titre;
this.objet = objet;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getObjet() {
return objet;
}
public void setObjet(String objet) {
this.objet = objet;
}
}
And finally my details activity. I'm only interested in the intent part; the rest has nothing to do with my problem:
public class note_details extends Activity {
ImageButton Del;
EditText PASSTITRE,USEROBJET;
String Passtitre,Userobjet;
DataBaseOperationstwo DOP;
Context CTX = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent =getIntent();
if(intent != null)
{
String objet = intent.getStringExtra("objet");
String titre= intent.getStringExtra("titre");
PASSTITRE.setText(objet);
USEROBJET.setText(objet);
}
setContentView(R.layout.activity_note_details);
Del = (ImageButton) findViewById(R.id.suppnote);
PASSTITRE = (EditText) findViewById(R.id.titree);
USEROBJET = (EditText) findViewById(R.id.objett);
Del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Passtitre = PASSTITRE.getText().toString();
Userobjet = USEROBJET.getText().toString();
DOP = new DataBaseOperationstwo(CTX);
DOP.deleteNote(DOP,Passtitre,Userobjet);
Toast.makeText(getBaseContext(),"note supprimé",Toast.LENGTH_LONG).show();
finish();
}
});
}
public void liste(View v)
{
Intent i = new Intent(this, consulter_note.class);
startActivity(i);
}
public void supprimer(View v)
{
}
}
My logcat doesn’t show any errors but the details activity shows with empty edittexts.
You should first add extras to the Intent and then fire it:
Intent intent = new Intent(this, note_details.class);
intent.putExtra("titre", titre);
intent.putExtra("objet", objet);
startActivity(intent);
Another thing worth mentioning is that you should avoid doing DB queries on the main thread, as it will slow down your app. Use Loaders, or just run queries on worker threads.

Adding dynamic items to listview

Hi I want to add an item to a listview.
This is my New message activity in which I wan to pass an item to my Main Activity. I'm not quite sure how to pass this data through an intent any help would be greatly appreciated.
public class NewMessage extends ActionBarActivity {
EditText new_message;
Button post_new_message_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_message);
new_message = (EditText) findViewById(R.id.message_content);
post_new_message_button = (Button) findViewById(R.id.message_send);
post_new_message_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView username = (TextView) findViewById(R.id.conversation_username2);
itemAdapter.add(new MessageItem(1656,"Bill Smith", "image", DateTime.now(), new_message.getText().toString()));
itemAdapter.notifyDataSetChanged();
if (v.getId() == R.id.message_send);
new_message.setText("");
}
});
}
}
This is my main activity I want to pass in the data
public class MainActivity extends ActionBarActivity {
TextView threadId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button newMessage = (Button) findViewById(R.id.new_message_button);
newMessage.setOnClickListener (new View.OnClickListener(){
#Override
public void onClick(View v){
Intent newMessage = new Intent(MainActivity.this, NewMessage.class);
startActivity(newMessage);
}
});
final ListView listView = (ListView) this.findViewById(R.id.messagingListView);
final ActivityAdapter itemAdapter = new ActivityAdapter(getApplicationContext(), this.MessageFeedData());
listView.setAdapter(itemAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
listView.getAdapter().getItem(position);
}
});
}
// Get dummy data for Activity Feed
public ArrayList<MessageItem> MessageFeedData() {
ArrayList<MessageItem> items = new ArrayList<MessageItem>();
items.add(new MessageItem(1, "Bob Doe", "image", DateTime.now(), "Hello how are you?"));
items.add(new MessageItem(200, "John Smith", "image", DateTime.now(), "Hello what are you doing"));
return items;
}
class ActivityFeedTask extends AsyncTask<Integer, Void, Void> {
ArrayList<MessageItem> recentTracks;
}
public class ActivityAdapter extends ArrayAdapter<MessageItem> {
private final Context context;
private final ArrayList<MessageItem> items;
//private int currentPage = 0;
public ActivityAdapter(Context context, ArrayList<MessageItem> recentTrackArrayList) {
super(context, 0, recentTrackArrayList);
this.context = context;
this.items = recentTrackArrayList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = getLayoutInflater().inflate
(R.layout.message_list_item, parent, false);
//final MessageItem item = items.get(position);
rowView = convertView;
TextView comment2 = (TextView) rowView
.findViewById(R.id.messaging_username);
comment2.setText(items.get(position).Username);
ImageView comment3 = (ImageView) rowView
.findViewById(R.id.messaging_photo);
if (items.get(position).Image == null) {
comment3.setImageResource(R.drawable.ic_launcher);
}
TextView comment4 = (TextView) rowView
.findViewById(R.id.messaging_date);
comment4.setText(items.get(position).DateTimeStamp.toString());
TextView comment5 = (TextView) rowView
.findViewById(R.id.messaging_string);
comment5.setText(items.get(position).MessageString);
}
return convertView;
}
}
}
if you just want to pass data back and forth between the two activities, maybe you should use:
startActivityForResult(Intent intent, int requestCode)
and
onActivityResult(int requestCode, int resultCode, Intent data)
So that when the NewMessageActivity finishes, it can send the data back to the main activity.

Android - List items do not refresh immediately

I created a class activity after which inherit two activity. It looks as follows:
public abstract class VlcMediaPlayerList extends RoboActivity {
public final static int AUDIO = 1;
public final static int VIDEO = 2;
#InjectView(R.id.audio_list_view)
ListView listView;
public static ArrayList<ImageWithTwoText> list = new ArrayList<ImageWithTwoText>();
public ImageWithTwoTextArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vlc_media_player_list);
adapter = new ImageWithTwoTextArrayAdapter(this, list);
listView.setAdapter(adapter);
addListenerOnItemList();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (intent != null) {
Bundle extras = intent.getExtras();
ImageWithTwoText file = null;
try {
file = (ImageWithTwoText) extras.getSerializable(FileChooserAcitivity.FILE_TYPE);
} catch (NullPointerException ex) {
}
Log.i("FILECHOOSER", String.valueOf(list.size()));
if (file != null) {
list.add(file);
}
adapter.notifyDataSetChanged();
}
}
/**
* Add File method called when addButton is clicked. Start activity
* FileChooserActivity for result as file
*
* #param ImageButton
* view
*/
public void addFile(View view) {
Intent intent = new Intent(this, FileChooserAcitivity.class);
intent.putExtra(FileChooserAcitivity.FILE_TYPE, getMediaType());
startActivityForResult(intent, getMediaType());
}
/**
* Clear the list when clearButton is clicked
*
* #param View
* view
*/
public void clearList(View view) {
list.clear();
adapter.notifyDataSetChanged();
Toast.makeText(this, getResources().getString(R.string.clear_list), Toast.LENGTH_SHORT).show();
}
/**
* Add listener on list
*/
public void addListenerOnItemList() {
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(view.getContext(),
PlayerActivity.class);
intent.putExtra(PlayerActivity.PLAYER_ELEMENT,
list.get(position).getTitle());
startActivity(intent);
}
});
}
protected abstract int getMediaType();
}
One class that extends the abstract class as follows:
public class VlcMediaPlayerAudio extends VlcMediaPlayerList {
#Override
protected int getMediaType() {
return VlcMediaPlayerList.AUDIO;
}
}
The problem occurs after calling onActivityResult () which is taken ImageWithTwoText object representing the element of the list. After calling adapter.notifyDataSetChanged (), this element is not added to the list.
Note! After exiting the activity (the return key) and re-entry to the same activity previously added element is already in the list
This is code for ImageWithTwoTextArrayAdapter:
public class ImageWithTwoTextArrayAdapter extends ArrayAdapter<String> {
public final static String TITLE_KEY = "title";
public final static String SUBTITLE_KEY = "subtitle";
public final static String IMAGE_RESOURCE_KEY = "imageResources";
private final List<ImageWithTwoText> imageWithTwoTextList;
private final Context context;
/**
* Class constructor
*
* #param context
*/
public ImageWithTwoTextArrayAdapter(Context context, List<ImageWithTwoText> imageWithTwoTextList) {
super(context, R.layout.row_view, getArrayListOfTitles(imageWithTwoTextList));
this.context = context;
this.imageWithTwoTextList = imageWithTwoTextList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_view, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.title);
TextView subTitleView = (TextView) rowView.findViewById(R.id.subtitle);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
ImageWithTwoText currentElement = imageWithTwoTextList.get(position);
titleView.setText(currentElement.getTitle());
subTitleView.setText(currentElement.getSubTitle());
imageView.setImageResource(currentElement.getImageResource());
return rowView;
}
private static List<String> getArrayListOfTitles(List<ImageWithTwoText> imageWithTwoTextList) {
List<String> listOfTitles = new ArrayList<String>();
for (ImageWithTwoText imageWithTwoText : imageWithTwoTextList) {
listOfTitles.add(imageWithTwoText.getTitle());
}
return listOfTitles;
}
}
I think the problem is in your constructor for the adapter, you are calling the base constructor like this:
super(context, R.layout.row_view, getArrayListOfTitles(imageWithTwoTextList));
This is passing the result of
getArrayListOfTitles()
to the underlying list stored by the adapter, which is a copy of your provided list. This means that changes you make to the list in the activity are not being made to the copy of the list held by the adapter.
Ahh, your comment beat my edit by about a minute! Glad it is solved!

Categories

Resources