Multiple List Views in Android - android

In my android app I have two List Views and two buttons.On clicking first button, first List View will be visible and on second button second List View will be visible.In one List View I will have list of items and in second List View there will be ADD button. Now clicking on ADD button my first List View will be visible and user can select items to add in second List View. I have done it successfully. But my problem is, by clicking on second button List View is appearing but there are no data which are added from First List View. Below is my code.
What am I missing?
final ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
final View header = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(header, null, false);
btnCollege.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItems[] = {};
final ArrayAdapter<String> string = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1);
lv.setAdapter(string);
Button btnAdd = (Button) header.findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (j == 0)
{
lv.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
j = 1;
}
else if (j == 1)
{
lv.setVisibility(View.INVISIBLE);
header.setVisibility(View.INVISIBLE);
j = 0;
}
else {}
lv1.setVisibility(View.VISIBLE);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String selected = lv1.getItemAtPosition(arg2).toString();
string.add(selected);
string.notifyDataSetChanged();
setListAdapter(string);
}
});
}
});
}
});

The view is not getting updated. use handlers for updating listview. That would help you solve the issue

Related

spinner item selection inside button onclick not working

Below inside my adapter class as i will get data inside button click i want to populate spinner inside button click and set item selected listener in it.
tvMediaCategory.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
for(int i=0; i<mediaList.get(position).getMediaCatList().size(); i++)
{
catArr[i] = mediaList.get(position).getMediaCatList().get(i).getCategoryName();
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for(int i=0; i<catArr.length; i++)
{adapter.add(catArr[i]);}
adapter.add("HINT_TEXT_HERE"); //This is the text that will be displayed as hint.
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
Toast.makeText(context, "position clicked "+position, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
tvMediaCategory.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
spinner.performClick();
//showCategoryDlg(catArr, position, selectedPos, tvMediaCategory);
}
}
});
here my spineer not getting open but selecting default item. how to make it work. this my code is inside a adapter. how can i show my spinner items on click of button?
Try this example... i am sure you will be able to get what u r looking for....
you need to implement the onItemSelectedListener
https://www.mkyong.com/android/android-spinner-drop-down-list-example

Error in making call on Button click inside listview

I have a listview with number and an image buton with each number. I want to make call on button click to the number in the row. my getview method is
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView = convertView;
ContactStockView sv = null;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(
R.layout.activity_row, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sv = new ContactStockView();
sv.name = (TextView) rowView.findViewById(R.id.textacti_row1);
sv.number = (TextView) rowView.findViewById(R.id.textacti_row2);
sv.btncall=(ImageButton)rowView.findViewById(R.id.imgbtn_call);
//sv.btncall.setOnClickListener((OnClickListener) activity);
rowView.setTag(sv);
//ImageButton btn=(ImageButton)convertView.findViewById(R.id.btn_call);
/* */
} else {
sv = (ContactStockView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
ContactStock currentStock = (ContactStock) stocks.get(position);
sv.name.setText(currentStock.getName());
number=currentStock.getNumber();
sv.number.setText(number);
//ImageButton btn=(ImageButton)rowView.findViewById(R.id.imgbtn_call);
sv.btncall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText( activity, number, Toast.LENGTH_SHORT).show();
String phoneCallUri = "tel:"+number;
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
activity.startActivity(phoneCallIntent);
}
});
/* btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText( activity, "abc", Toast.LENGTH_SHORT).show();
/*}
});*/
// TODO Auto-generated method stub
return rowView;
}
protected static class ContactStockView {
protected TextView name;
protected TextView number;
protected ImageButton btncall;
}
My log cat on call is
you should declare the string number in get view method not outside it. change
number=currentStock.getNumber();
to
final String number=currentStock.getNumber();
and delete the string(ie,number) from outside getview method
You can't findout the problem that way. You have to debug your getView method.
Do that by
1) double click on the first line of the method on the left of the outer frame to see a small point thay represents a breakpoint.
2) Right click on the project and click Debug as Android application.
3) Using the button F6 (in Eclipse) start moving steps untill you find the specific line you code produce error.
4) Now when you know the line it's easier to find out what happened.
attach the number to the view with setTag, then retrieve it in the click listener. you also don't need a new listener every time, define that once outside the function...
sv.btncall.setTag(number);
then in the onclick listener...
new View.OnClickListener() {
#Override
public void onClick(View v) {
String number = (String) v.getTag();
String phoneCallUri = "tel:"+number;
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
activity.startActivity(phoneCallIntent);
}
}

Delete ListView row in Custom Adapter

I want to delete a row from my list view on click of "delete" button. My listview item has following things placed horizontally: TextView-1,TextView-2,TextView-3,ImageButton-delete button. Now when I click delete button, the row should be deleted from the view. Below is the adapter code;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_name);
String item = tv.getText().toString();
String tableno = mListItems.get(0).getTableNumber();
orderDetailsDB = new OrderDetailsDBAdapter(
getApplicationContext());
orderDetailsDB.deleteItem(item,tableno);
I tried by setting Individual textviews to blank but its not working.
holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");
I read couple of posts and they suggest to remove item from my list(mListItems) and then do adapter.notifyDataSetChanged();. Problem is I am not using array adapter for populating list view but using Custom adapter, so unable to get the position for item to be deleted. Please advise. thanks.
First write below line in your adapter's getView method.
button.setTag(position)
in onClick method
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
yourarraylistObject.remove(position);
// your remaining code
notifyDataSetChanged();
}
Just use remove() to remove list item from the adapter
for your reference
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
What you can do in order to get the position that you want to delete is to pass that into your listener:
// inside custom adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// do your delete code here
notifyDataSetChanged();
}

Using Checkbox array in android

I am trying to make a listview with checkbox using listview's built in checkbox method. I have gone through a stackoverflow post and i found it is running properly except one problem.
If there are four items in list and assume, i checked the second and third item, onclicking, it is displaying the second and third item as needed..but if i am selecting first and then third and then second item, and then i m unchecking the first, so i must be left with second and third as desired output. But it is providing first second and third item as output.
can anybody guide me on that..?
This is the java code:
public class TailoredtwoActivity extends Activity implements OnItemClickListener, OnClickListener{
Button btn1;
ListView mListView;
String[] array = new String[] {"Ham", "Turkey", "Bread"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tailoredtwo);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);
mListView = (ListView) findViewById(R.id.listViewcity);
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.btn_tailortwo_submit);
button.setOnClickListener(this);
}
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
int size = positions.size();
for(int index = 0; index < size; index++) {
Toast.makeText(getApplicationContext(), array[positions.keyAt(index)].toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
Change your onClick to
Delcare the below as a class variable
StringBuilder builder;
Then
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
builder = new StringBuilder();
for(int index = 0; index <array.length; index++) {
if(positions.get(index)==true)
{
builder.append(array[index]);
builder.append("\n");
}
}
Toast.makeText(getApplicationContext(),builder, Toast.LENGTH_LONG).show();
}

Android ListView Item is not Clickable

I want to design each list item in the ListView can be clickable, and triger out which list item be clicked. But it can not. I tried the two methods: setOnItemClickListener() and setOnItemSelectedListener() on my code. I have had googled couple of references about the article, however it still can not work(clickable).
I would like post the code below: The code can display the list items and I can see the Log.d content for the line of Log.d(" mListView01.getCount()="," "+vc); on LogCat well. But, there is no any response if I clicked on the list Item.
if you don't mind, could you help point me out where I was wrong, thanks !
Code for creating the listView using the Activity Widget:
......
setContentView(R.layout.main_open);
TextView itemText = (TextView) findViewById(R.id.itemText);
TextView codeText = (TextView) findViewById(R.id.codeText);
itemText.setText(selectedItem);
codeText.setText(selectedCode);
ListView mListView01 = (ListView)findViewById(R.id.main_open_listview1);
String[] keys = new String[] {"title","title_image", "content",
"title1","title1_image","content1","title2","title2_image","content2"};
int[] resValues = new int[] { R.id.title, R.id.title_image, R.id.content,
R.id.title1, R.id.title1_image, R.id.content1,R.id.title2, R.id.title2_image, R.id.content2};
openDocAdapter opendoc = new openDocAdapter(this,localdcoumentlist, R.layout.main_open_content, keys, resValues );
mListView01.setSelected(true);
mListView01.setClickable(true);
mListView01.setAdapter(opendoc);
int vc = mListView01.getCount();
Log.d(" mListView01.getCount()="," "+vc);
mListView01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("Selected From setOnItemSelectedListener, arg2=", " "+ arg2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
mListView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedViewPos = arg2;
Log.d("TitlesSelectionDialog(),selectedViewPos= "," "+ selectedViewPos);
Toast.makeText(getApplicationContext(), "selectedViewPos= "+ selectedViewPos, Toast.LENGTH_LONG).show();
}
});
......
Code for openDocAdapter:
private class openDocAdapter extends SimpleAdapter
{
private Context _con;
private List _List;
private int _listviewId;
private String[] _keys;
private int[] _resValues;
public openDocAdapter(Context context, ArrayList<HashMap<String,Object>> List , int listviewId, String[] keys, int[] resValues )
{
super(context, List, listviewId, keys, resValues);
_con =context;
_List = List;
_listviewId = listviewId;
_keys = keys;
_resValues = resValues;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.main_open_content, null);
}
TextView title = (TextView) v.findViewById(R.id.title);
(...Similiar codes define textView, imageViewsd.)
return v;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return super.getCount();
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return super.getItem(position);
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return super.getItemId(position);
}
}
Edit1: I found an article here About the Focus setting on the layout will cause clickable work or not work. So, I remove the lines of (I don't while it be coded here) in the xml file of layout. Then the setOnItemSelectedListener() method is worked while scrolling the list list with orange focus change. But it still not meet my expection.
Provlem Solved ! After couple hous googling/search and try_eror. And I would like share it if you are interesting.
The main cause of the problem is: I used the ScrollView as the basic layout for the row.xml(containing the content for each listview row). Then, I used the LinearLayout(Vertial) instead of it. The setOnItemClickedListener() method works fine now. I do not have any idea regarding this that will cause the ListView to be not clickable. If somebody know it, please tell us,

Categories

Resources