I am creating a ListView in which contents of an ArrayList(Declared Globally) is to be displayed. I am using a custom layout here. Everything looks good but the ListView is just not showing on the output screen. While the same coding used on some other project is giving the desired output. Please tell me if there is any problem with the code or some other thing.
Globalvar.java //where the ArrayList is declared
package com.example.tale;
import java.util.ArrayList;
import java.util.List;
public class Globalvar
{
public static int value=0;
public static List<String> list = new ArrayList<String>();
}
activity_cart2.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"
tools:context=".cart2">
<ListView
android:id="#+id/lvl"
android:layout_width="match_parent"
android:layout_height="404dp">
</ListView>
cart2.java
package com.example.tale;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class cart2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart2);
Globalvar.list.add("Example 1");
ListView listView = (ListView)findViewById(R.id.lvl);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.forcart2,Globalvar.list);
listView.setAdapter(adapter);
}
}
forcart2.xml //custom layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp">
</TextView>
Related
So, I installed AnyChart library and tried to do the simplest pie chart and it does not show up. I searced for proper samples of code and all of them don't work for me
So, here is my code:
layout:
<LinearLayout
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:orientation="vertical"
tools:context="com.anychart.charts.Overall">
<com.anychart.AnyChartView
android:layout_weight="1"
android:id="#+id/any_chart_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My java class:
package com.anychart.charts;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.anychart.AnyChart;
import com.anychart.AnyChartView;
import com.anychart.chart.common.dataentry.DataEntry;
import com.anychart.chart.common.dataentry.ValueDataEntry;
import com.example.project.R;
import java.util.ArrayList;
import java.util.List;
public class Overall extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overall);
Pie pie = AnyChart.pie();
List<DataEntry> data = new ArrayList<>();
data.add(new ValueDataEntry("John", 10000));
data.add(new ValueDataEntry("Jake", 12000));
data.add(new ValueDataEntry("Peter", 18000));
pie.data(data);
AnyChartView anyChartView = findViewById(R.id.any_chart_view);
anyChartView.setChart(pie);
}
}
I'm new to Android Studio.For me there is a problem with listview. I've set all the ids properly and yet the problem occurs.
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"
tools:context="com.example.prashanthwagle.myapplication.MainActivity">
<ListView
android:id="#+id/sample"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>
Java:
package com.example.prashanthwagle.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] s={"asfasaf","bafaf","cbfbd"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.sample);
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s);
listView.setAdapter(a);
}
}
I Think that you should change
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s)
;
to
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R. android.R.layout.simple_list_item_1,s);
You have set the wrong layout for ArrayAdapter. Use this:
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,s);
The layout reference in the adapter initialization and assignment should not be the layout reference of your activity. Basically, this parameter tells the ListView what type of layout will each row of the list have. Read more about this from here: https://developer.android.com/guide/topics/ui/layout/listview.html
I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.
I got a fragment that should only display a list but every time I try to display the list the previous activity is displayed in the background.
So my question is, how do I display the list on a empty page? What can I put instead of getActivity() in the ArrayAdapter.
My XML 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="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/fundraiser"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
And my code looks like this
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FundraiserFragment extends ListFragment{
String[] values = new String[] { "Item 1", "Item 2", "Item 3"};
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(),android.R.layout.simple_list_item_1,values);
setListAdapter(adapter);
}
Don't forget that layouts are transparent by default, unless you assign them a background color.
Assign a background color to the LinearLayout of your fragment in the XML file and the parent activity should disappear.
android:background="#android:color/white"
In my application I want to change the list view text color. In this case I am using XML file for list view. Is it possible? If yes then give the example.
gender.xml
<?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"
android:background="#drawable/topelg"
>
<ListView
android:id="#+id/android:list"
android:layout_marginTop="60dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
/>
</LinearLayout>
Egender.java
package com.Elgifto;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
public class Egender extends ListActivity{
Button b1;
ListView lv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gender);
// b1=(Button) findViewById(R.id.butt1);
b1=new Button(this);
b1.setText("Done");
//b1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv=(ListView) findViewById(android.R.id.list);
lv.addHeaderView(b1);
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
// To send a result, simply call setResult() before your
// activity is finished.
finish();
}
});
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, GENDER));
lv.setBackgroundColor(Color.BLACK);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setTextFilterEnabled(true);
}
private static final String[] GENDER = new String[] {
"Male","Female"
};
}
If you would like to change color of all ListView items, instead of passing default android.R.layout.simple_list_item_single_choice to ArrayAdapter you should pass custom list item XML, which has different TextColor attribute.
For example, created custom_list_item.xml under folder Layout:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
Then passed it to adapter:
new ArrayAdapter<String>(this, R.layout.custom_list_item, stringList)
I've got list, were all items are red.