I have created a custom ListView using ArrayAdapter. ListView's row has two views, one is the main view & other is hidden & it appears when a TextView on main view is clicked.
For showing data on rows I have a created an Object. I am facing some issues in my ListView.
This issue occurs some time. Whenever I delete an item from ListView, I can see the item is deleted from my ArrayList & then I call notifyDataSetChanged. The issue is this the ListView still shows the deleted row but its not clickable(only view is there). Only other rows are clickable which are not deleted yet. Check image 1.
I have used a flag in my Object to hide/show the hidden view. But sometimes it doesn't appear well. Check image 2
Sometimes after hiding the hidden view it still shows below the main view. Check image 3
Please help me out in this.
class ShippingAdapter extends ArrayAdapter<ShippingOption>
{
ShippingAdapter(Context context, ArrayList<ShippingOption> list)
{
super(context, R.layout.row_shipping, R.id.row_shipping_tv_product_name, list);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
ShippingViewHolder holder = (ShippingViewHolder) row.getTag();
if (holder == null)
{
holder = new ShippingViewHolder(row);
row.setTag(holder);
}
final ShippingOption shippingOption = getShippingOption(position);
holder.tvRemove.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
showRemoveDialog(shippingOption.getProductId(), position);
}
});
holder.tvProductName.setText(shippingOption.getProductName());
imageLoader.DisplayImage(shippingOption.getProductImageUrl(), holder.ivProduct);
ArrayAdapter<String> adapterShippingOption = createAdapter(holder.spinnerShippingOption, shippingOption.getListShipping(), R.layout.spinner_textview_blue);
holder.spinnerShippingOption.setSelection(adapterShippingOption.getPosition(shippingOption.getSelectedOption()));
if (shippingOption.getSelectedOption().equals(Constants.PICK_UP_AT_STORE))
{
HashMap<String, Integer> mapPickupAddress = shippingOption.getMapPickupAddress();
Collection<String> collection = mapPickupAddress.keySet();
ArrayList<String> listAddress = new ArrayList<String>();
listAddress.addAll(collection);
ArrayAdapter<String> adapterAddress = createAdapter(holder.spinnerAddress, listAddress, R.layout.spinner_textview_blue_small);
holder.spinnerAddress.setSelection(adapterAddress.getPosition(shippingOption.getSelectedAddress()));
holder.relativeAddress.setVisibility(View.VISIBLE);
holder.spinnerAddress.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedAddress = parent.getItemAtPosition(position).toString();
String oldAddress = shippingOption.getSelectedAddress();
shippingOption.setSelectedAddress(selectedAddress);
if (oldAddress == null || !oldAddress.equals(selectedAddress))
notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
} else
{
holder.relativeAddress.setVisibility(View.GONE);
}
holder.spinnerShippingOption.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedOption = parent.getItemAtPosition(position).toString();
// if (!selectedOption.equals(Constants.FREE_SHIPPING))
// {
String oldOption = shippingOption.getSelectedOption();
shippingOption.setSelectedOption(selectedOption);
if (!oldOption.equals(selectedOption))
notifyDataSetChanged();
// }
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
holder.relativeLayoutGift.setVisibility(shippingOption.isGiftDetailsVisible() ? View.VISIBLE : View.GONE);
if (shippingOption.isGiftWrapAvailable())
{
imageLoader.DisplayImage(shippingOption.getGiftImageUrl(), holder.ivGift);
holder.layoutGift.setVisibility(View.VISIBLE);
ArrayList<String> listGift = new ArrayList<String>();
listGift.add("Not a Gift");
listGift.add("Yes it's a Gift $" + shippingOption.getGiftServiceCharge());
ArrayAdapter<String> adapterGift = createAdapter(holder.spinnerGift, listGift, R.layout.spinner_textview_blue);
holder.tvGiftDetails.setVisibility(shippingOption.getSelectedGiftOption().equals("Not a Gift") ? View.GONE : View.VISIBLE);
holder.spinnerGift.setSelection(adapterGift.getPosition(shippingOption.getSelectedGiftOption()));
holder.spinnerGift.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedOption = parent.getItemAtPosition(position).toString();
String oldOption = shippingOption.getSelectedGiftOption();
shippingOption.setSelectedGiftOption(selectedOption);
if (!oldOption.equals(selectedOption))
notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
holder.tvGiftDetails.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(true);
notifyDataSetChanged();
}
});
holder.relativeGiftDetails.setVisibility(shippingOption.isGiftDetailsVisible() ? View.VISIBLE : View.GONE);
holder.relativeLayoutGift.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
holder.btnCancel.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});
holder.btnClose.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});
final EditText editLocal = holder.editGiftMessage;
holder.btnApply.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
String message = editLocal.getText().toString();
shippingOption.setGiftMessageApplied(true);
shippingOption.setGiftMessage(message);
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});
holder.editGiftMessage.setText(shippingOption.getGiftMessage());
holder.editGiftMessage.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
} else
{
holder.layoutGift.setVisibility(View.GONE);
holder.relativeGiftDetails.setVisibility(View.GONE);
}
return row;
}
}
Here i am going to demonstrate for one view tagging.
ShippingViewHolder holder;
if (holder == null)
{
holder = new ShippingViewHolder(row);
row.setTag(holder);
}
holder = (ShippingViewHolder) row.getTag();
ShippingOption shippingOption = getShippingOption(position);
holder.tvRemove.setTag(shippingOption);
holder.tvRemove.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ShippingOption shippingOption1 =(ShippingOption) v.getTag();
int pos = getPosition(shippingOption1); // this is the function that will return you the postion of your object from your passed list of object; write it your self.
showRemoveDialog(shippingOption1.getProductId(), pos);
}
});
Related
I want to take the value of correct answers "qCorrrect" from my spinner that is in my Adapter, to activity and make it counter from that.I have getUser_input method to take that value and pass in ButtonListener.But when i go thru debbuger"user_input" in my button Activity not showing any values
#Override
public void onBindViewHolder(#NonNull final ItemAdapter.MyHolder holder, int position) {
holder.question.setText(mList.get(position).getQuestion());
holder.category.setText(mList.get(position).getCategory());
final ArrayList<String> questions=new ArrayList<>();
qCorrect=mList.get(position).getCorrectAnswer();
String qWrong= String.valueOf(mList.get(position).getIncorrectAnswers().get(0));
String qWrong1= String.valueOf(mList.get(position).getIncorrectAnswers().get(1));
String qWrong2= String.valueOf(mList.get(position).getIncorrectAnswers().get(2));
questions.add(qCorrect);
questions.add(qWrong);
questions.add(qWrong1);
questions.add(qWrong2);
Collections.shuffle(questions);
user_input=new String[mList.size()];
final ArrayAdapter<String> arrayAdapter=
new ArrayAdapter<String>(context,android.R.layout.simple_spinner_dropdown_item,questions);
holder.spinner.setAdapter(arrayAdapter);
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(holder.spinner.getSelectedItem().equals(qCorrect)){
user_input[position]=qCorrect;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Button finshButton=findViewById(R.id.finishBtn);
finshButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int couter=0;
String[] user_input=itemAdapter.getUser_input();
for(int i=0;i<mQuestionList.size();i++){
if(mQuestionList.get(i).getCorrectAnswer().equals(user_input[i])){
couter++;
}
}
Toast.makeText
(QuestionsActivity.this, couter + "/" + mQuestionList.size() , Toast.LENGTH_SHORT).show();
}
});
Hey new in android just create an app in that change the text color after change position of the in list view, when I am changing the position of the ABC at the same time change the new position ABC "Assigning" to "Assigned" text with change Assigned color red.
please help me.
private class CurrentEmployeeAdapter extends BaseAdapter {
Context context;
int layoutId;
LayoutInflater liCurrentEmp;
public CurrentEmployeeAdapter(CurrentEmployee currentEmployee, int activity_current_employee) {
this.context=currentEmployee;
this.layoutId=activity_current_employee;
liCurrentEmp=LayoutInflater.from(context);
}
#Override
public int getCount() {
return SalesManNameArrayList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View v;
if (convertView==null){
convertView=liCurrentEmp.inflate(R.layout.current_employee_items,parent,false);
v=convertView;
}else {
v=convertView;
}
TextView tvEmpName=(TextView)v.findViewById(R.id.tv_salesman_name);
final TextView tvassingwork=(TextView)v.findViewById(R.id.tv_assign_word);
Button btnAssign=(Button)v.findViewById(R.id.btn_assign);
btnAssign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tvassingwork.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (assignNo==1) {
tvassingwork.setText("Assigning");
tvassingwork.setTextColor(getResources().getColor(R.color.green));
assignNo=0;
}else {
int i=position+1;
if(i >0) {
String moveName = SalesManNameArrayList.get(position);
SalesManNameArrayList.set(position, SalesManNameArrayList.get(position));
SalesManNameArrayList.remove(position);
SalesManNameArrayList.add(SalesManNameArrayList.size(), moveName);
tvassingwork.setText("Assigned");
tvassingwork.setTag(position-1);
tvassingwork.setTextColor(getResources().getColor(R.color.red));
assignNo=1;
notifyDataSetChanged();
}
}
}
});
tvEmpName.setText(SalesManNameArrayList.get(position));
return convertView;
}
}
Try this
tvassingwork.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if (assignNo==1) {
tvassingwork.setText("Assigning");
tvassingwork.setTextColor(getResources().getColor(R.color.green));
assignNo=0;
}else {
int i=position+1;
if(i >0) {
String moveName = SalesManNameArrayList.get(position);
SalesManNameArrayList.set(position, SalesManNameArrayList.get(position));
SalesManNameArrayList.remove(position);
SalesManNameArrayList.add(SalesManNameArrayList.size(), moveName);
tvassingwork.setText("Assigned");
tvassingwork.setTag(position-1);
tvassingwork.setTextColor(getResources().getColor(R.color.red));
assignNo=1;
notifyDataSetChanged();
}
}
});
}
});
runOnUIThread() may helps you.
I am have a small problem in my custom dialog.
When the user search for an item in the listview, the list shows the right items, but if the user for example wants to search again, the listview shows the results from the previous search.
The problem:
How do I restore the listview after the first search?
My activity with the custom dialog
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog();
}
});
}
}
private void showDialog() {
dialog = new Dialog(this);
View view = getLayoutInflater().inflate(R.layout.material_list, null);
searchList = (ListView) view.findViewById(R.id.searchList);
dialog.setTitle("Välj ny artikel");
final MaterialAdapter adapter = new MaterialAdapter(
InformationActivity.this, materialList);
dialog.setContentView(view);
searchList.setAdapter(adapter);
searchList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Materials itemMat = materialList.get(position);
resultProduct = itemMat.materialName;
resultProductNo = itemMat.materialNo;
result = resultProduct + " " + resultProductNo;
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_SHORT).show();
}
});
search = (EditText) dialog.findViewById(R.id.search);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
resultText = search.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(resultText);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
cancel = (Button) dialog.findViewById(R.id.btnCancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
ok = (Button) dialog.findViewById(R.id.btnOK);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
product.setText(resultProduct);
productNo.setText(resultProductNo);
dialog.dismiss();
}
});
dialog.show();
}
}
My adapter
public class MaterialAdapter extends BaseAdapter {
LayoutInflater inflater;
Context context;
List<Materials> searchItemList = null;
ArrayList<Materials> materialList = new ArrayList<Materials>();
public MaterialAdapter(Context context, List<Materials> searchItemList) {
this.context = context;
inflater = LayoutInflater.from(context);
this.searchItemList = searchItemList;
this.materialList = new ArrayList<Materials>();
this.materialList.addAll(searchItemList);
}
#Override
public int getCount() {
return searchItemList.size();
}
#Override
public Object getItem(int position) {
return searchItemList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.d, null);
viewHolder = new ViewHolder();
viewHolder.tvMaterialName = (TextView) convertView
.findViewById(R.id.tvMaterialName);
viewHolder.tvMaterialNo = (TextView) convertView
.findViewById(R.id.tvMaterialNo);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvMaterialName.setText((searchItemList.get(position))
.getMaterialName());
viewHolder.tvMaterialNo.setText((searchItemList.get(position))
.getMaterialNo());
return convertView;
}
public class ViewHolder {
TextView tvMaterialName;
TextView tvMaterialNo;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
searchItemList.clear();
if (charText.length() == 0) {
searchItemList.addAll(materialList);
} else {
for (Materials wp : materialList) {
if (wp.getMaterialName().toLowerCase(Locale.getDefault())
.startsWith(charText)) {
searchItemList.add(wp);
}
}
}
notifyDataSetChanged();
}
}
Thanks for the help :)
Try to change search.getText() to s.toString()
public void onTextChanged(CharSequence s, int start, int before,
int count) {
resultText = search.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(resultText);
}
to
public void onTextChanged(CharSequence s, int start, int before, int count) {
resultText = s.toString().toLowerCase(Locale.getDefault());
adapter.filter(resultText);
}
A secondary thing, make materialList = searchItemList; And searchItemList empty which will be filled with search items (first run will be same elements of materialList.)
P.S Your adapter should implement Filterable in this case (implements Filterable)
#Override
public void afterTextChanged(Editable s) {
resultText = search.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(resultText);
}
Try this and check whether it helps
I'm working with 2 spinner in my app, and I want to do this operation in my button click for example like this in visual studio
if ((spin1.text = "bla bla bla") && (spin2. text = "ho ho ho"))
{
text1.text = result;
}
Do you have any idea to perform this operation in android? thanks.
SOLVED WITH THIS CODE!!
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value1 = item.toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value2 = item.toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void count(View v)
{
if(value1.equals("Depok") && value2.equals("Jakarta"))
{
hasil.setText("SUCCESS");
}
}
First you have to retrieve the text from the spinner correctly, did you do that?
If not, this is how to do it:
Assuming you called your spinner spinner.
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String txtFromSpinner = mySpinner.getSelectedItem().toString();
When comparing, take the string from the spinner and compare it with some string.
if (txtFromSpinner.equals("bla bla bla") && txtFromSpinner2.equals("ho ho ho")
{
Log.E("Well done sir.","Comparison complete!");
//Code
}
Good luck.
Try this code once.
String item1,item2;
Textview hasil;
hasil = (TextView) findViewById(R.id.textView1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
{
String item1 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
String item2 = (String) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
public void count(View v)
{
if(item1.equals("Depok") && item2.equals("Jakarta"))
{
hasil.setText("berhasil");
}
}
I'm trying to delete an item from ListView by Clicking on a picture (imageView). I must do it this way, I've managed to do it by click on the item itself but for this project I'm not allowed to do that.
My Adapter extends BaseAdapter and my TaskListItem extends Relative Layout
I'm using a custom layout xml.
protected void onFinishInflate() {
super.onFinishInflate();
textV = (TextView)findViewById(R.id.textViewTask);
textP = ((TextView)findViewById(R.id.textPriority));
textR = ((TextView)findViewById(R.id.textResponsible));
imageD = ((ImageView)findViewById(R.id.imageDeleteTask));
imageD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deleteTask();
}
});
}
and the deleteTask() method:
protected void deleteTask() {
//MainActivity.adapter.clear(position)
Toast.makeText(getContext(), "Task Deleted", Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged();
}
my problem is the variable position. I cannot find a way to get the position of the item selected (by clicking on the imageView widget).
Finally, here's my adapter code. Any help will be much appreciated:
public class TaskListAdapter extends BaseAdapter {
private ArrayList<Task> tasks;
private Context context;
public TaskListAdapter(ArrayList<Task> tasks, Context context) {
super();
this.tasks = tasks;
this.context = context;
}
#Override
public int getCount() {
return tasks.size();
}
#Override
public Task getItem(int position) {
return (null==tasks)? null: tasks.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TaskListItem tli;
Log.v("test","dasfsd");
if (null == convertView) {
tli = (TaskListItem)View.inflate(context, R.layout.task_list_item, null);
}
else {
tli = (TaskListItem)convertView;
}
tli.setTask(tasks.get(position));
return tli;
}
public void forceReload() {
notifyDataSetChanged();
}
public void clear(int position) {
tasks.remove(position);
}
Update Code for OnClickListener on ImageView
public void setTask(final Task task) {
this.task = task;
//textV.set(task.isComplete());
textV.setText(task.getName());
textP.setText(task.getPriority());
textR.setText(task.getResponsible());
imageD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// probably get position
// deleteTask();
MainActivity.adapter.notifyDataSetChanged();
}
});
}
protected void deleteTask() {
int position=0;
MainActivity.adapter.clear(position);
Toast.makeText(getContext(), "Task Deleted", Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged();
//testing - problem cannot delete item.
}
To get position , Add position as Tag of view
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TaskListItem tli;
Log.v("test","dasfsd");
if (null == convertView) {
tli = (TaskListItem)View.inflate(context, R.layout.task_list_item, null);
}else {
tli = (TaskListItem)convertView;
}
tli.setTask(tasks.get(position) , position);
return tli;
}
public void setTask(final Task task , int position) {
this.task = task;
//textV.set(task.isComplete());
textV.setText(task.getName());
textP.setText(task.getPriority());
textR.setText(task.getResponsible());
imageD.setTag(new Integer(position));
imageD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer) v.getTag();
deleteTask(position);
MainActivity.adapter.notifyDataSetChanged();
}
});
}
protected void deleteTask(int position) {
MainActivity.adapter.clear(position);
Toast.makeText(getContext(), "Task Deleted", Toast.LENGTH_SHORT).show();
MainActivity.adapter.notifyDataSetChanged();
//testing - problem cannot delete item.
}