I'm trying to achieve a search activity where when the search button is clicked from the action bar, a list is shown with possible products. I'm trying to connect my search button with my list view but have been unsuccessful.
MainActivity.java
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
// Listview Data
String products[] = {"A", "B", "C", "D",
"fdsafds", "fdsavds",
"fdsafdsafsd", "greadgfd", "F", "thrgefargra"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
// adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
// lv.setAdapter(adapter);
return true;
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/inputSearch"
android:title="Search places"
android:hint="Search places..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="29dp"/>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Single ListItem -->
<ListView android:id="#+id/list_item"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
<!-- Product Name -->
<TextView android:id="#+id/product_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
You shoild reggister 'ListView' for the Menu
registerForContextMenu(listview);
I've done something like what you want. try and see if it can help you.
public class YourActivity extends Activity {
private ListView l;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
l = (ListView) findViewById(R.id.your_list_view_id);
List<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
l.setAdapter(arrayAdapter);
}
}
Related
I am getting an error when trying to set my view to display the ListView for the file I want to display(text file). I am pretty sure it has something to do with the XML. I just want to display the information from this.file = fileop.ReadFileAsList("Installed_packages.txt");. My code:
public class Main extends Activity {
private TextView tv;
private FileOperations fileop;
private String[] file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fileop = new FileOperations();
this.file = fileop.ReadFileAsList("Installed_packages.txt");
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
ListView lv = new ListView(this);
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
setContentView(lv);
}
}
list_item.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:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</LinearLayout>
main.xml :
<?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"
android:weightSum="1">
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5sp"
android:id="#+id/TextView01"
android:text="#string/hello"/>
</ScrollView>
</LinearLayout>
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
// other attributes of the TextView
/>
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
Soution is here
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
Java code :
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:
//Pass in the resource id: R.id.text_view
SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
R.id.text_view,
new ArrayList<>());
Adapter:
public class SpinnerAdapter extends ArrayAdapter<MyEntity> {
private Context context;
private List<MyEntity> values;
public SpinnerAdapter(Context context, int textViewResourceId,
List<MyEntity> values) {
//Pass in the resource id: R.id.text_view
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
The problem is in the line:
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
Because when i change it to:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.file));
It works!
I am trying to display a spinner in FrameLayout but it's not showing the drop down menu. i am unable to find the issue.
XML
<FrameLayout
//design continue here
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b89ff"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</FrameLayout>`
Code
final List<String> list=new ArrayList<>();
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, list);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
My onCreate method
TabHost host;
host = findViewById(R.id.tabHost);
spinner= findViewById(R.id.spinner);
progressDialog=new ProgressDialog(this);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("News Feed");
spec.setContent(R.id.tab1);
spec.setIndicator("News Feed");
host.addTab(spec);`
Update 1
Using RelativeLayout instead FrameLayout throws
android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout
Remove this line and check,
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //Comment this line
Try this change also :
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item, list); //Changed layout resource id
Try this
final List<String> list=new ArrayList<>();
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
list.add("jamshaid");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, list);
spinner.setAdapter(arrayAdapter);
Please try this:
code:
public class MainClass extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOnSpinner();
}
private void addOnSpinner() {
Spinner spinner = findViewById(R.id.spinner2);
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
ArrayAdapter adapter = new ArrayAdapter<String>( this, R.layout.support_simple_spinner_dropdown_item, list);
spinner.setAdapter(adapter);
}
}
XML:
<FrameLayout
//layout
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner2"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm experiencing the problem "Unfortunately, your listview has stopped working" when i was using the android emulator to run my codes.
Please take a look at my codes thanks!
For activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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.example.it3783.listview.MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose a location..."/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:id="#+id/spLocations"></ListView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/superHerosTV"
/>
</LinearLayout>
For MainActivity.java:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<CharSequence>adapter;
TextView tvSelection;
ListView lvLocation;
String []strArray;
String []superHeros = {"1. Batman\n 2. SpiderMan", "1. Peter Pan\n2. Superman",
"1. IronMan\n2. Peter Pan", "1. SnowWhite \n2. Gingerbreadman"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, strArray);
tvSelection = (TextView) findViewById(R.id.superHerosTV);
lvLocation = (ListView) findViewById(R.id.spLocations);
Resources myRes = this.getResources();
strArray = myRes.getStringArray(R.array.locationArea);
lvLocation.setAdapter(adapter);
lvLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = strArray[position];
tvSelection.setText(text);
}
});
adapter = ArrayAdapter.createFromResource(this, R.array.locationArea, android.R.layout.simple_list_item_1);
}
}
For strings.xml:
<resources>
<string name="app_name">ListView</string>
<string-array name="locationArea">
<item>North</item>
<item>South</item>
<item>East</item>
<item>West</item>
</string-array>
</resources>
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_1, strArray);
This line in your onCreate methods takes the strArray, which is not initialized so its value is null.
Now when you set the adapter with null data to the listview, it crashes.
Right now I am trying to create an activity with an EditText and a button, and below that a listview were I can Dynamically add strings to the list. I'm getting errors trying to mock up my activity.
Here is my main activity.
public class MainActivity extends Activity {
private Connection serverConnection;
private ArrayList<String> listItems = new ArrayList<String>();
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(MainActivity.this, 0, listItems);
listview.setAdapter(adapter);
serverConnection = new Connection(MainActivity.this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void displayMessage(String string) {
listItems.add(string);
adapter.notifyDataSetChanged();
}
}
Here is the xml for the main activity.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/message_entry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/list"
android:layout_toLeftOf="#+id/send_button"
android:layout_weight="1"
android:hint="#string/enter_message" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/message_entry"
android:paddingTop="10dp" />
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/message_entry"
android:layout_alignRight="#+id/list"
android:layout_alignTop="#+id/message_entry" />
</RelativeLayout>
Here is my other class that I'm going to use to for connecting to a socket.
public class Connection extends Thread {
private Socket client;
private ObjectOutputStream output;
private ObjectInputStream input;
public Connection(MainActivity mainActivity) {
mainActivity.displayMessage("Test1");
mainActivity.displayMessage("Test2");
mainActivity.displayMessage("Test3");
mainActivity.displayMessage("Test4");
mainActivity.displayMessage("Test5");
}
}
This line gives you an error
adapter = new ArrayAdapter<String>(MainActivity.this, 0, listItems);
The second parameter accepts layout resourceID where you can populate the value (String) to the layout.
You can use the layout of android framework which is android.R.layout.simple_list_item_1
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listItems);
Here is the layout I am trying to get. As long as I don't actually try to populate my spinners, everything loads just fine. Based on what I have found in my searches and the log is that I need to use a TextView with the ArrayAdapter. Exactly how I do that while preserving the current layout - Spinners and a submit button at the top with a ListView for the returned results below the spinners is what I am having trouble achieving.
Here is my onCreate method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.master);
DBHelper db = new DBHelper(this);
List<Stations> st = db.getAllStations();
List<CharSequence> stations = new ArrayList<CharSequence>();
setList(4, 9);
for (int i = 0; i < st.size(); i++)
{
stations.add(st.get(i).getStation());
}
ListView list = (ListView) findViewById(R.id.list);
Spinner s1 = (Spinner) findViewById(R.id.spinnerStart);
Spinner s2 = (Spinner) findViewById(R.id.spinnerEnd);
ArrayAdapter<CharSequence> SimpleSpinner1 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
ArrayAdapter<CharSequence> SimpleSpinner2 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
SimpleAdapter nSchedule = new SimpleAdapter(this, departures, R.layout.row,
new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
for (CharSequence c : stations)
{
SimpleSpinner1.add(c);
SimpleSpinner2.add(c);
}
list.setAdapter(nSchedule);
s2.setAdapter(SimpleSpinner2);
s1.setAdapter(SimpleSpinner1);
}
master.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="#+id/spinnerStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Spinner
android:id="#+id/spinnerEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinnerStart" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinnerEnd"
android:text="Button" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
layout="#layout/listview" />
</RelativeLayout>
spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Instead of R.id.spinnerStart / R.id.spinnerEnd you need to pass a textView
EDIT:
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_item, R.id.textView1);
spinner1.setAdapter(adapter);
textView1 is present in spinner_item layout. Hope this is clear
Try this
// set id for spinner
spinner2 = (Spinner) findViewById(R.id.spinner2);
// prepare a list
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
// set adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter to spinner
spinner2.setAdapter(dataAdapter);