ListView not getting updated on calling notifyDataSetChanged() - android

i want to add data into the listview when pressing the button.
but it is not getting updated pressing the button dont know how it is not getting worked.
Please help me.
here is the code below
#SuppressLint("NewApi")
public class Messanger extends Activity {
ArrayAdapter<String> adapter;
ListView output;
EditText box;
String msg[];
Button btn;
int counter=0;
ArrayList<String> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messanger);
output=(ListView)findViewById(R.id.textOutput);
box=(EditText)findViewById(R.id.textInput);
listItems = new ArrayList<String>();
adapter = new ArrayAdapter<String> (Messanger.this,android.R.layout.simple_list_item_1, android.R.id.text1, listItems);
output.setAdapter(adapter);
btn=(Button)findViewById(R.id.btnSend);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(Messanger.this, "in event", Toast.LENGTH_LONG).show();
sendMessage(box.getText().toString());
//stItems.notifyAll();
}
});
}
public void sendMessage(String message)
{
listItems.add("me: "+message);
adapter.addAll(listItems);
adapter.notifyDataSetChanged();
}
}
And my Layout looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10" >
<ListView
android:id="#+id/textOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" />
</ScrollView>
<LinearLayout
android:id="#+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:baselineAligned="true">
<EditText android:layout_weight="1" android:id="#+id/textInput"
android:layout_height="45dp" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_weight="1" android:text="Send"
android:layout_height="45dp" android:layout_width="125dp"
android:id="#+id/btnSend"></Button>
</LinearLayout>
</LinearLayout>
thanks in advance

Not sure why is isn't updating. Code looks OK to me. You might try clearing the array before calling addAll() because if you don't you will end up with duplicates in your list (which probably isn't what you want. Should look like this:
listItems.add("me: "+message);
adapter.clear();
adapter.addAll(listItems);
adapter.notifyDataSetChanged();
The reason it isn't updating is probably somewhere else though. Post your layout, maybe the problem is in there.
EDIT: After seeing the layout:
Yep, the problem is in your layout. You don't put a ListView inside a ScrollView. A ListView already knows how to scroll. Get rid of the surrounding ScrollView and you should be good.

Notice that you always add the same items by calling
adapter.addAll(listItems);
Regardless,
Try debugging you getView() function in the adapter.

Related

Android Studio bottom button is not showing when run the program but it shows in design view

I'm writing an Android lab and somehow my bottom button and text enter part are not showing. I'm using Linear Layout. it shows in the XML file design mode but doesn't show when running the program. Can someone help me find out what the problem is?
I tried to add a pic of what my design page and program run pic but it's not letting me, when I run the program I can see the two strings that I added to an array, but just not see the button and plain text at the bottom
here is my code, I have my java file code list below the xml code:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="684dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send" />
<EditText
android:id="#+id/typehere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Type here"
android:textColor="#9C9B9B" />
<Button
android:id="#+id/receive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="receive" />
</LinearLayout>
</LinearLayout>
</ListView>
my activity
public class ChatRoomActivity extends AppCompatActivity {
ListView listView;
EditText input;
Button receive;
Button send;
ArrayList<String> items;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
listView = findViewById(R.id.listView);
input = findViewById(R.id.typehere);
receive = findViewById(R.id.receive);
send = findViewById(R.id.send);
items = new ArrayList<>();
items.add("I'm sending");
items.add("I'm receiving");
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter); // see the list of result of adaptor
receive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = input.getText().toString();
addMessage(text);
input.setText("");
}
});
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = input.getText().toString();
addMessage(text);
input.setText("");
}
});
}
public void addMessage(String message){
items.add(message);
listView.setAdapter(adapter);
}
}
after straggling in the display, I found the height for ListView is longer than my screenview, so I edit the xml to
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginTop="-50dp"
android:orientation="horizontal"
tools:visibility="visible">
and the rest of the code didn't change
I guess for others that has same issue try to shorter the listview's height
You can use RelativeLayout and can add android:layout_above attribute to your ListView
Example code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llBottomLayout"/>
<LinearLayout
android:id="#+id/llBottomLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
tools:visibility="visible">
</LinearLayout>
</RelativeLayout>

The ListView does not scroll. How do I fix it?

I have a ListView inside a Linearlayout.
The listview has a custom cursoradapter.
Everything works fine, except the ListView does not scroll.
Any suggestion more than welcome!! Thanks. maurizio
Here is the XML of the MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is the relevant piece of java code.
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor tabellaCursor=null;
private ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] to = new int[] {R.id.name_entry};
db = new DatabaseHelper(this);
tabellaCursor=db.getWritableDatabase().rawQuery("SELECT _id, colonna1, colo nna2, colonna3 FROM tabella ORDER BY _id", null);
ListAdapter adapter=new MyAdapter(this, R.layout.list_example_entry, tabe llaCursor, new String[]{"colonna1"},to);
ListView lt = (ListView)findViewById(R.id.list);
lt.setAdapter(adapter);
Button addbtn=(Button)findViewById(R.id.buttonAdd);
addbtn.setOnClickListener(new OnClickListener()
{public void onClick(View v){add(); }
});
}
Your ListViews Layout param is "wrap_content". it will expand as you add new items to litview. therefore it won't scroll. If you set it to "match_parent" it will sure start scrolling.And dont forget that a view does scroll only if its contents(childs view what ever)
size is bigger than it.
Add more list items that exceeds height. then you can scroll.
Please use below code, it will solve your problem.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Aggiungi" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/buttonAdd"/>
</RelativeLayout>

android listview or scrollview: for a form with dynamic number of fields

Let's say I need to take names of movies the person likes. I don't know how many he likes so I want to present him with just one text field. He can click a button and add more fields to fill. I can use ScrollView of course but should I think about using a ListView? If so, I can't seem to understand how. Please help
Better, add edittext dynamically inside scrollview, onclick of a button.
try this:
public class TestingActivity extends ListActivity {
/** Called when the activity is first created. */
EditText value;
Button insert;
ArrayList<String> data=new ArrayList<String>();
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
insert = (Button) findViewById(R.id.button2);
value=(EditText)findViewById(R.id.value);
insert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
data.add(value.getText().toString());
getListView().setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, data));
}
});
getListView().setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, data));
}
}
xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollingCache="false" >
</ListView>
</LinearLayout>
You could implement an own BaseAdapter that uses EditText-Views to let people enter data.
Here's how to implement a good Adapter.
If the Button is getting clicked you can just add another entry in a List that is used to add another View to your Adapter.

how to fill the empty space with ListView after hiding a button

I have a simple vertical LinearLayout in which there is a button on the top and a Listview below it. What I want is that when the button is pressed, it hides (using View.GONE) and the empty space generated by it is filled by ListView. But after all the efforts, I have not been able to implement it.
I tried invalidate() and onDraw(), I tried giving layout_weight=1 to the ListView, I tried forceLayout(), requestLayout() methods as well but none of them worked.
The strange thing is that if there is EditText or TextView or any other component instead of ListView, its working fine i.e. Edittext etc. go and grab the empty space generated by hiding the Button. But in case of ListView , its not happening.
This thing is perfectly working in my emulator
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new ProgressTask().execute();
btn1.setVisibility(8);
}
})
XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="#+id/itemlist"
android:layout_below="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
check this......
public class TestingActivity extends ListActivity {
/** Called when the activity is first created. */
Button checkBtn;
String[] temp={"item 1","item 2","item 3"};
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getListView().setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, temp));
checkBtn = (Button) findViewById(R.id.button1);
checkBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
checkBtn.setVisibility(View.GONE);
}});
} }
xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check connetion" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollingCache="false" >
</ListView>
</LinearLayout>

Button onClick not detected with a ListView and ListActivity

I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Height"
android:layout_height="wrap_content"
android:id="#+id/buttonHeight"
android:layout_width="wrap_content"
android:layout_weight="15"
android:onClick="OnClickHeight">
</Button>
<Button
android:text="Width"
android:layout_height="wrap_content"
android:id="#+id/buttonWidth"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/buttonHeight"
android:layout_weight="1"
android:onClick="onClickWidth">
</Button>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:
public void onClickWidth(View v)
{
// does stuff
}
However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!
I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?
Can you tell us what are you try to do? And why do you need ListActivity?
Sorry for edditing:P
You could take a look here: How to handle ListView click in Android
put OnClicklistener within adapter itself .
Rather then using the onClick stuff in the layout, have you tried something like this:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
Then do the same for buttonHeight.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_land);
Button btn = (Button) findViewbyid(R.id.buttonWidth);
btn.setOnClickListener(this);
}
public void onClickWidth(View v)
{
if(v == btn){
//your code...
}
}
set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>

Categories

Resources