Does my adapter work? - java

I get inputs from the user and then put them in a ListView, but when I click on the button, it works with no errors, opening the layout and immediately closing it. When I try with arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_list);, it works perfect.
public class HomeChat extends AppCompatActivity {
ListView listView;
EditText writeSms;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> m_list = new ArrayList<String>();
String emriUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_chat);
writeSms = (EditText) findViewById(R.id.shkrunSms);
listView = (ListView) findViewById(R.id.listView);
emriUser = getIntent().getExtras().getString("emri");
arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_chat,m_list);
listView.setAdapter(arrayAdapter);
}
public void sentSmsButton(View view) {
String mesazhet = emriUser + ": " + writeSms.getText().toString();
if (writeSms != null && writeSms.length() > 0) {
m_list.add(mesazhet);
arrayAdapter.notifyDataSetChanged();
writeSms.setText("");
}else
{
Toast.makeText(getApplicationContext(),"Something Wrong",Toast.LENGTH_LONG).show();
}
}
}
Here is the chat layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
</LinearLayout>

The problem is that ArrayAdapter is expecting a layout with only a single TextView in it. So, your layout with a LinearLayout containing a TextView won't work. Try changing R.layout.list_chat to simply:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/test123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:background="#cccccc"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>

Related

How to set text to value of listview items after time intervals

I am developing an android app where a user will input text to an EditText, then it will be added to a ListView.
But once a button with onClick value "start" is clicked, it will display each item of the ListView in a TextView with id displayText, in the order of each item and it will loop(Repeat) once it is exhausted.
Here is my Code
Math.java
public class Math extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
public void AddItem(View v) {
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
itemsAdapter.add(itemText);
etNewItem.setText("");
}
public void start(View v){
RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
lc.setVisibility(View.GONE);
RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
tc.setVisibility(View.VISIBLE);
}
}
activity_math.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Math">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvContainer"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:id="#+id/displayText"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contentContainer">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lvItems"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btnAddItem" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNewItem"
android:layout_alignTop="#+id/btnAddItem"
android:hint="Enter a new item"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btnAddItem"
android:layout_toStartOf="#+id/btnAddItem"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/btnAddItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="AddItem"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#id/btnAddItem"
android:onClick="start"/>
</RelativeLayout>
Okay, i think the issue is that you haven't given the TextView any thing to display. you have to convert the arrayList to a String array and use setText to display the Value.
add this part to the "start" method
String ls =System.getProperty("line.separator");// i used this to seperate the array items
String joint = TextUtils.join(ls, lvitems);
result.setText(joint);
That should work

Views not showing when added on button click?

Ok, I have been on this for 6 hours now and for some reason, I just don't get it.
I am trying to make an app to calculate something using a certain formula.
Now consider it as a grade point average calculating thing. I have a button which should allow the user to add two more spinners to the layout and thereby proceeding further. Now I made a method for this purpose. When I call this method for the first time the spinners are added. But it doesn't work the second time.
It also doesn't work if I put it in the button onClick() method. I have gone through multiple examples posted online and also on stackoverflow, maybe I am making some stupid mistake. Just guide me through this, please.
Here is the code:-
SGPA.class
public class SGPA extends AppCompatActivity {
View v;
Context c;
LinearLayout subjectData;
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
//int id = View.generateViewId();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sgpa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
//TODO if 2015 or 2013 regulations
//assuming 2015
fill2015();
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_sgpa, null);
subjectData=(LinearLayout)v.findViewById(R.id.subjectdata);
c = this;
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addViewForAnotherSubject();
setContentView(v);
}
});
setContentView(v);
}
void addViewForAnotherSubject()
{
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
subjectData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
void fill2013()
{
}
}
activity_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="innominatebit.srmite.SGPA">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_sgpa" />
</android.support.design.widget.CoordinatorLayout>
content_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="6dp"
android:paddingEnd="6dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingStart="6dp"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="innominatebit.srmite.SGPA"
tools:showIn="#layout/activity_sgpa">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/subjectdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
All help is appreciated. Thanks in advance!
Ok, I got it. i made another test application for debugging purposes until I got it. Seems I did not need a layout inflator at all.
Here's the code:
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
fill2015();
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
final LinearLayout myData=(LinearLayout)findViewById(R.id.myData);
addforanothersubject(myData);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addforanothersubject(myData);
}
});
}
void addforanothersubject(LinearLayout myData)
{
Context c=MainActivity.this;
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(c,
android.R.layout.simple_spinner_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
myData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
}
And the xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="aaa.test.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/myData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Hope it helps someone.

spinner not displaying when clicked

hi currently im encountering a problem where the spinner is not showing up when clicked. when i run theres not any error so i dont know whats wrong . sorry im still new to android and java pls help....
public class SpinnerInfo extends MainProduct {
Spinner spinners;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinners = (Spinner) findViewById(R.id.spinner);
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinners.setAdapter(adapter);
spinners.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String [] dataArray = getResources().getStringArray(R.array.type_arrays);
String type = dataArray[position];}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}}
my xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:orientation="vertical"
android:elevation="1dp"
android:weightSum="1">
<TextView
android:text="Product List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|italic"
android:textAlignment="center"
android:fontFamily="casual"
android:textColor="#android:color/background_dark"
android:layout_marginTop="10dp"/>
<TextView
android:text="Select Type:"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:textAlignment="textStart"
android:textStyle="normal|bold" />
<Spinner
android:layout_width="126dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
/>
<ListView
android:id="#+id/listview_product"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="8dp"
android:background="#android:color/white"
android:layout_height="match_parent">
</ListView>
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
to
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.layout.support_simple_spinner_dropdown_item, R.array.type_arrays);
By the way you are accessing to the string resources when you can do this parent.getItemAtPosition(position)

How to use ListView in CardView?

How to use ListView in CardView ? I have CardView, CardView and then in CardView I would like to add ListView that ListView data come from server.
activity_main.xml
<android.support.v7.widget.CardView...>
<android.support.v7.widget.CardView...>
<android.support.v7.widget.CardView
android:id="#+id/cv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingredients"/>
<ListView
android:id="#+id/list_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ListView>
</LinearLayout>
</android.support.v7.widget.CardView>
activity_listview.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
MainActivity.java
private void GetNames() {
NetworkEngine.getInstance().getStudent(student.getId(), new Callback<List<Student>>() {
#Override
public void success(List<Student> students, Response response) {
// Student student = students;
// ListAdapter adapter = new ListAdapter<>(getApplication(), R.layout.activity_listview, students);
// ListView listView = (ListView) findViewById(R.id.list_name);
// listView.setAdapter(adapter);
}
});
}

android listview is iterating through the footer menus too

This is my activity code:
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
"Disclaimer", "Security", "Credits"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
}
And here is the xml file for this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/LinearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="#+id/label"></TextView>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:layout_width="wrap_content" android:id="#+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="#+id/imgbtn3" android:layout_height="wrap_content" android:src="#drawable/topnews" android:visibility="visible"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgbtn5" android:layout_alignBottom="#+id/imgbtn4" android:layout_height="wrap_content" android:src="#drawable/more"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:id="#+id/imageButton1" android:layout_height="wrap_content" android:src="#drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/imgbtn1"></ImageButton>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Now, when the activity is loading it is appending the menu with each item in the list. I just want it to appear only in bottom not with each and every list item.
any suggestions?
Edit-1 :
I tried this but getting an error of nullpointer exception
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
ListView lv = new ListView(this);
lv.addFooterView(footerView);
What do you mean by menu? Do you mean your ImageButtons?
If that's the case then remove those create another layout for those ImageButtons and add them using ListView.addFooterView() instead.
You could create e.g. list_footer_layout.xml, which contains your ImageButtons. Then:
public void onCreate(Bundle bundle) {
...
ListView lv = getListView(); // If your activity is ListActivity
View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
lv.addFooterView(foot);
}

Categories

Resources