set onClickListener edit text listview - java

Hello at all the community,
i've a listview with a custom adapter that consists by one textview and one edit text. i set the text for the edit text into the adapter but in my acivity (listview) i want to set the onClickListener at this edit text. ive this code for my getView method
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.textView = (TextView)view.findViewById(R.id.textView);
holder.editText = (EditText)view.findViewById(R.id.editText);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
holder.editText.setText("My text");
holder.textView.setText("text");
return view;
}
#Override
public boolean isEnabled(int position) {
return false;
}
i disablet the click on listview with the isEnabled method because I will not be able to click on the list. in my activity i've only the listview so if i want to set onclicklistener on the edit text of the adapter how can i do this?
public MyAdapterClass(Context context, int textViewResourceId, ArrayList<String> list) {
super(context, textViewResourceId, list);
// TODO Auto-generated constructor stub
this.list = list;
this.context = context;
}

Simply use this
holder.editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
#Override
public boolean isEnabled(int position) {
return true;
}

Please do something like :
private OnClickListener cl;
public MyAdapterClass(Context context, int textViewResourceId, ArrayList<String> list, OnClickListener cl) {
super(context, textViewResourceId, list);
// TODO Auto-generated constructor stub
this.list = list;
this.context = context;
this.cl = cl;
}
then in on getView()set it to EditText :
holder.editText.setOnClickListener(cl);
finally set set an put OnClickListener() like :
OnClickListener cl = new OnClickListener() {
#Override
public void onClick(View v) {
// Logic of Click goes Here.
}
};
where you are setting Adapter to
ListView, like :
adapter = MyAdapterClass(Activity.this, android.R.layout.simple_list_item_1, list, cl);
listView.setAdapter(adapter);

Related

How to refresh ListView after deleting element?

I have created a listview but when i delete a element, it doesn't refresh. I have tried
adapter.notifyDataSetChanged();
and
getListView().invalidateViews();
in mainactivity:
case R.id.ordini:
Ordini O = new Ordini();
android.app.FragmentManager fragmentManager0 = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction0 = fragmentManager0
.beginTransaction();
fragmentTransaction0.replace(R.id.frame, O);
fragmentTransaction0.commit();
Ordini.java
public class Ordini extends ListFragment implements AdapterView.OnItemClickListener {
int ArraySize;
CustomAdapterOrdini adapter;
private List<RowItemOrdini> rowItems;
Button SendButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.ordini, null, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArraySize = MainActivity.ArrayProdotto.size();
SendButton = (Button) getActivity().findViewById(R.id.send_btn);
rowItems = new ArrayList<RowItemOrdini>();
for (int i = 0; i < ArraySize; i++) {
RowItemOrdini items = new RowItemOrdini(
MainActivity.ArrayProdotto.get(i),
R.drawable.ic_delete_black_24dp,
MainActivity.ArrayCode.get(i),
MainActivity.ArrayPrezzo.get(i),
MainActivity.ArrayColli.get(i),
MainActivity.ArrayQuantita.get(i));
rowItems.add(items);
}
adapter = new CustomAdapterOrdini(getActivity(), rowItems);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
SendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "invio ordine in corso... ", Toast.LENGTH_SHORT)
.show();
SendOrdine();
}
});
}
#Override
public void onResume(){
super.onResume();
adapter.notifyDataSetChanged();
getListView().invalidateViews();
}
CustomAdapterOrdini
public class CustomAdapterOrdini extends BaseAdapter {
Context context;
List<RowItemOrdini> rowItem;
ImageView Del;
TextView Colli, Prezzo, Quantita, Code, Product;
CustomAdapterOrdini(Context context, List<RowItemOrdini> rowItem) {
this.context = context;
this.rowItem = rowItem;
}
#Override
public int getCount() {
return rowItem.size();
}
#Override
public Object getItem(int position) {
return rowItem.get(position);
}
#Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_ordini, null);
}
Code = (TextView) convertView.findViewById(R.id.prodcode);
Product = (TextView) convertView.findViewById(R.id.PRODOTTO);
Del = (ImageView) convertView.findViewById(R.id.del);
Colli = (TextView) convertView.findViewById(R.id.T1);
Prezzo = (TextView) convertView.findViewById(R.id.T2);
Quantita = (TextView) convertView.findViewById(R.id.T3);
Del.setFocusable(true);
Del.setClickable(true);
Del.setTag(convertView);
final RowItemOrdini row_pos = rowItem.get(position);
// setting the image resource and title
Del.setImageResource(R.drawable.ic_delete_black_24dp);
Product.setText(row_pos.getTitle());
Code.setText(row_pos.getCode());
Colli.setText(row_pos.getColli());
Prezzo.setText(row_pos.getPrezzo());
Quantita.setText(row_pos.getQuantita());
Del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE I WANT REFRESH MY LISTVIEW
MainActivity.ArrayQuantita.remove(0);
MainActivity.ArrayCode.remove(0);
MainActivity.ArrayProdotto.remove(0);
MainActivity.ArrayColli.remove(0);
MainActivity.ArrayPrezzo.remove(0);
CustomAdapter.ShowDialog("Elemento eliminato con successo!", context);
}
});
return convertView;
}
Can someone help me with listview refresh?
I want refresh my listview in CustomAdapterOrdini or in onResume in Ordini.java.
public void onClick(View v) {
// HERE I WANT REFRESH MY LISTVIEW
There is a simple way?
Thank you so much!
You have declare ArrayProdotto in your MainActivity and you are display your data from rowItem in CustomAdapterOrdini. You have to remove your data from both list and call notifyDataSetChanged();
Like as below code.
Del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE I WANT REFRESH MY LISTVIEW
MainActivity.ArrayProdotto.remove(position); //Delete from main source
rowItem.remove(position) // Delete from adapter
CustomAdapter.ShowDialog("Elemento eliminato con successo!", context);
notifyDataSetChanged(); // Refresh Adapter
}
});
1). Remove your item from adapter:
adapter.remove(adapter.getItem(position));
2). Call adapter.notifyDataSetChanged();
You should call notifyDataSetChanged() every time when you deleting item from adapter. Not only in onResume.
Note, that you should remove item from adapter, to change ListView items, not from storage(ArrayList, SQLite, or else).

No Solution for NullPointerException

I want to create a BaseAdapter for a ListView. From my database I get an ArrayList that contains the entries for the ListView. The onCreate Method works fine but if I click the button to insert data to the database and display it in the ListView my app crashes. I'm looking for the Solution but can't find it. Maybe you do.
Logcat says there is a NullPointerException at the point where the text of the TextView is set (LaunchActivity$CustomAdapter.getView).
LaunchActivity.java
public class LaunchActivity extends Activity {
ImageView buttonAdd;
EditText insertText;
SubjectAdapter helper;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
helper=new SubjectAdapter(this);
listView = (ListView) findViewById(R.id.listViewLaunch);
listView.setEmptyView(findViewById(R.id.emptyView));
insertText = (EditText) findViewById(R.id.editTextLaunch);
buttonAdd = (ImageView) findViewById(R.id.imageViewLaunch);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String subject = insertText.getText().toString();
long id=helper.insertData(subject);
if(id<0){
Log.d("Personal", "insertData not successfull");
} else {
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(insertText.getWindowToken(), 0);
// loading ListView with newly added data
loadListViewData();
Log.d("Personal", "insertData successfull");
Toast.makeText(LaunchActivity.this, subject+" hinzugefĆ¼gt", Toast.LENGTH_LONG).show();
insertText.setText("");
}
}
});
loadListViewData();
}
public void loadListViewData() {
// database handler
SubjectAdapter helper = new SubjectAdapter(getApplicationContext());
// Get Data from Database and set ArrayList
ArrayList<String> list = (ArrayList<String>) helper.getData();
// Creating adapter for ListView
CustomAdapter adapter = new CustomAdapter(getApplicationContext(), list);
// attaching data adapter to ListView
listView.setAdapter(adapter);
}
class CustomAdapter extends BaseAdapter{
ArrayList<String> list = new ArrayList<String>();
Context context;
// Constructor
CustomAdapter(Context context, ArrayList<String> list){
this.list = list;
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// LayoutInflation and Recycling
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_listview, parent, false);
}
TextView myText = (TextView) findViewById(R.id.textList);
// Get String from ArrayList
String temp = list.get(position);
// Set Text for TextView inside ListView
myText.setText(temp);
return row;
}
}
}
If row is null you have to inflate and assign the result to row!
row = inflater.inflate(R.layout.item_listview, parent, false);
Also you'll want to get myText from row:
TextView myText = (TextView) row.findViewById(R.id.textList);
First create object of ArrayList<String> in getData() and check code below:
public void loadListViewData() {
// database handler
SubjectAdapter helper = new SubjectAdapter(getApplicationContext());
// Get Data from Database and set ArrayList
ArrayList<String> list = helper.getData();
// Creating adapter for ListView
CustomAdapter adapter = new CustomAdapter(getApplicationContext(), list);
// attaching data adapter to ListView
listView.setAdapter(adapter);
}
class CustomAdapter extends BaseAdapter{
ArrayList<String> list = null;
Context context;
// Constructor
CustomAdapter(Context context, ArrayList<String> list){
this.list = list;
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// LayoutInflation and Recycling
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_listview, parent, false);
}
TextView myText = (TextView) findViewById(R.id.textList);
// Get String from ArrayList
String temp = list.get(position);
// Set Text for TextView inside ListView
myText.setText(temp);
return row;
}
}
and make sure return type of getData() will be ArrayList<String>
hope it will help.

Change all ImageViews in ListView to a particular image

I'm trying to make an audio file preview page which consists of a listview of files in a folder. Each row has a play button next to it which changes to a stop button when clicked. I now need a way of setting all the other images to the play button if a row is clicked as only one audio file should be playing at once. I tried iterating through the list and getting the imageview from this but had no luck (shown below):
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
IncidentLogger logger = new IncidentLogger(this.getActivity());
final List<Audio> audioList = logger.getAllAudio();
AudioListViewAdapter adapter = new AudioListViewAdapter(audioList, getActivity());
final ListView listView = (ListView) getActivity().findViewById(R.id.listView);
listView.setAdapter(adapter);
OnItemClickListener onItemClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i = 0; i < audioList.size(); i++) {
View v = listView.getAdapter().getView(i, view, parent);
ImageView imageView = (ImageView) v.findViewById(R.id.control);
imageView.setImageResource(R.drawable.ic_action_play);
}
ImageView iv = (ImageView) view.findViewById(R.id.control);
if(iv.getTag() == "1") {
iv.setTag("0");
iv.setImageResource(R.drawable.ic_action_play);
} else {
iv.setTag("1");
iv.setImageResource(R.drawable.ic_action_stop);
}
}
};
listView.setOnItemClickListener(onItemClickListener);
}
This can be done so easily, you need to just look at on the position of list item that was clicked by user. A well described complete demo code is as below ...
public class ChangeImageInListDynamically extends Activity {
/**
* Adapter for your data.
*/
ImageAdapter adpAR;
ArrayList<String> itemList;
ListView mListView;
ImageAdapter mAdapter;
/**
* Indicate which item is currently selected.
* Its default selected item is '0' e.g., 1st item.
*/
private int selectedPosition=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemList = new ArrayList<String>();
for (int i = 0; i < 11; i++) {
itemList.add("Item - "+i);
}
mListView = (ListView)findViewById(R.id.data_list);
//Initialize your adapter
mAdapter = new ImageAdapter(nChangeImageInListDynamically.this, itemList);
//set adapter to your list.
mListView.setAdapter(mAdapter);
//set list click listener.
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
//your image that need to change dynamically.
ImageView imageView = (ImageView) view.findViewById(R.id.select);
//position of clicked item.
selectedPosition=position;
//change image of respective image.
imageView.setImageResource(R.drawable.selected);
//notify your adapter to update your list.
adpAR.notifyDataSetChanged();
}
});
}
static class ViewHolder {
TextView title;
ImageView img;
}
/**
* Your ImageAdapter.
*/
public class ImageAdapter extends BaseAdapter {
ArrayList<String> dataList;
Context context;
public ImageAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.dataList = arrayList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.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) {
View row = convertView;
final ViewHolder holder;
Log.d("posiiton ImageAdapater : ",""+position);
if (row == null) {
LayoutInflater inflater = ChangeImageInListDynamically.this.getLayoutInflater();
row = inflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.title = (TextView) row.findViewById(R.id.text);
holder.img = (ImageView) row.findViewById(R.id.select);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
//set titel text.
holder.title.setText(dataList.get(position));
//Change Image of selected item dynamically.
if(selectedPosition == position) {
//Item is selected.
holder.img.setImageResource(R.drawable.selected);
}else{
//Other non-selected items.
holder.img.setImageResource(R.drawable.dis_selected);
}
return row;
}
}
}

How to retrieve all the values from a custom arraydapter

i have implemented a custom array adapter for my project,so far i am able to add and delete items from it and it displays them in the ListView too
dynamically..however i have a button in the MainActivity(that holds the ListView), that when clicked i want to display all the row items.
so far what i am getting instead of the data in the adapter after appending all values to a StringBuilder is:
com.baseadapter.C#4052b9b8com.baseadapter.C#4052f490com.baseadapter.C#40531378com.baseadapter.C#4053415
the arrayadapter and arraylist are set as below:
ArrayList<C> arrayvalues= new ArrayList<C>();
MyAdapter adapter;
adapter= new MyAdapter(MainActivity.this, R.layout.list_item,
arrayvalues);
the code for my collection class is this:
public class C {
String the_text;
public String getText() {
return the_text;
}
public void setText(String the_text) {
this.the_text = the_text;
}
public C(String the_text) {
super();
this.the_text = the_text;
}
}
the code i use to iterate over the adapter is this:
btn_done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
StringBuilder b = new StringBuilder();
for(int i=0 ; i<adapter.getCount() ; i++){
b.append(adapter.getItem(i));
}
Toast.makeText(getApplicationContext(), b.toString(), Toast.LENGTH_SHORT).show();
}
});
and the custom adapter code(just in case,though i don,t think the problem lies here):
public class MyAdapter extends ArrayAdapter<C> {
Context context;
int layoutResourceId;
ArrayList<C> data = new ArrayList<C>();
public MyAdapter(Context context, int layoutResourceId,
ArrayList<C> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
myholder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new myholder();
holder.tv = (TextView) row.findViewById(R.id.tv_num);
holder.btnDelete = (ImageButton) row.findViewById(R.id.btn_delete);
row.setTag(holder);
} else {
holder = (myholder) row.getTag();
}
C c=data.get(position);
holder.tv.setText(c.getText());
//String c=data.get(position);
//holder.tv.setText(c.g.getName());
holder.btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
data.remove(position);
notifyDataSetChanged();
}
});
return row;
}
static class myholder {
TextView tv;
ImageButton btnDelete;
}
}
what should i do to get the actual values in the adapter?ps:kindly attach code,thanks
Change your below loop code. it shall work now.
for(int i=0 ; i<adapter.getCount() ; i++)
{
C itemObject=adapter.getItem(i);
if(itelObject!=null)
b.append(itemObject.getText());
}
You should override the getItem() method in the Adapter:
#Override
public C getItem(int position) {
return data.get(position);
}
And update your loop code to get the text for "C" or override "C" toString() method as was suggested.

update listview from an Array adapter

I'm trying to refresh the list view when I click on a single item of the list view.
For example I'm trying to do something like this:
public class RssItemsAdapter extends ArrayAdapter<Item> {
private TextView txtTitle;
private ImageView imgFavourite;
private int resourceId;
private Context context;
private List<Item> items = new ArrayList<Item>();
public RssItemsAdapter(Context context, int resourceId, List<Item> items)
{
super(context, resourceId, items);
this.context = context;
this.items = items;
this.resourceId = resourceId;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Item getItem(int index) {
return items.get(index);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
Item item = getItem(position);
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(resourceId, parent, false);
}
txtTitle = (TextView) row.findViewById(R.id.title);
txtTitle.setText(item.getTitle());
imgFavourite = (ImageView) row.findViewById(R.id.favourite);
imgFavourite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (item.isFavourite() == true) {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_off);
}
else {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_on);
}
}
});
// add a "star" icon on favourite items
if (item.isFavourite() == true)
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_on);
else
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_off);
return row;
}
Every time I click on the imgFavourite icon it should change. Where is the problem?
It doesn't look like you are ever changing the value of item.isFavourite(). Assuming you want to toggle when that item is clicked...
imgFavourite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Toggle the current state
item.setIsFavourite(!item.isFavourite());
if (item.isFavourite() == true) {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_off);
}
else {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_on);
}
}
});
Also, you should be doing this kind of work in the ListView's onItemClickListsner. As is, this will create a new onClickListener each time a new row is brought into view. Beyond that, check into the "view holder pattern".

Categories