Every time I use to run the application.It crashes.What could be the issue??can anyone help me out.I have called 2nd activity using current activity's onClickItem method but it's not working
What I have tried:
MainActivity
package com.example.brandonmain.listviewexample1;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import static android.R.attr.name;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.results_listview);
HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("Chairman Seceretery", "7th floor");
nameAddresses.put("Corporate Affairs Division", "6th floor");
nameAddresses.put("Director(Technical) Secretariat", "5th Floor");
nameAddresses.put("Operations", "5th Floor");
nameAddresses.put("Emergency Control room", "5th Floor");
nameAddresses.put("Enterprise risk management", "5th Floor");
nameAddresses.put("Business Planning", "5th Floor");
nameAddresses.put("Director(Finance) Secretariat", "4rth Floor");
nameAddresses.put("Finance", "4rth Floor");
nameAddresses.put("DIRECTOR (PERSONNEL) SECRETARIAT", "3rd Floor");
nameAddresses.put("Personnel", "3rd Floor");
nameAddresses.put("ED(LAW)& PLO", "3rd Floor");
nameAddresses.put("Business Planning", "3rd Floor");
nameAddresses.put("Transformation monitoring office", "3rd Floor");
nameAddresses.put("Vigilance", "2nd Floor");
nameAddresses.put("Administration", "2nd Floor");
nameAddresses.put("Parliament Cell", "2nd Floor");
nameAddresses.put("Corporate Planning", "2nd Floor");
nameAddresses.put("Coal Import Group", "2nd Floor");
nameAddresses.put("Director(P&SP) Secretariat", "1st Floor");
nameAddresses.put("Business Planning", "1st Floor");
nameAddresses.put("C & IT", "1st Floor");
nameAddresses.put("Medical & Health Service", "1st Floor");
nameAddresses.put("Director(RM&L) Secretariat", "1st Floor");
nameAddresses.put("CRMG", "1st Floor");
nameAddresses.put("ICVL", "1st Floor");
nameAddresses.put("O/o ED(Forest)", "1st Floor");
nameAddresses.put("Reception", "Ground Floor");
nameAddresses.put("State Bank Of India", "Ground Floor");
nameAddresses.put("Library", "Ground Floor");
nameAddresses.put("Basement", "Ground Floor");
nameAddresses.put("Administration-Kaushambi ", "Ground Floor");
Map<String, String> treeMap = new TreeMap<String, String>(nameAddresses);
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = treeMap.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id)
{
super.onListItemClick(l, view, position, id);
if (position == 0) {
Intent intent = new Intent(this, Adminstrative.class);
startActivity(intent);
}
}
}
Mainxml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.brandonmain.listviewexample1.MainActivity">
<ListView
android:id="@+id/results_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
/>
I want my current listview to open a new listview when an item is pressed in current ListView.
MainActivity
package com.example.brandonmain.listviewexample1;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import static android.R.attr.name;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.results_listview);
HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("Chairman Seceretery", "7th floor");
nameAddresses.put("Corporate Affairs Division", "6th floor");
nameAddresses.put("Director(Technical) Secretariat", "5th Floor");
nameAddresses.put("Operations", "5th Floor");
nameAddresses.put("Emergency Control room", "5th Floor");
nameAddresses.put("Enterprise risk management", "5th Floor");
nameAddresses.put("Business Planning", "5th Floor");
nameAddresses.put("Director(Finance) Secretariat", "4rth Floor");
nameAddresses.put("Finance", "4rth Floor");
nameAddresses.put("DIRECTOR (PERSONNEL) SECRETARIAT", "3rd Floor");
nameAddresses.put("Personnel", "3rd Floor");
nameAddresses.put("ED(LAW)& PLO", "3rd Floor");
nameAddresses.put("Business Planning", "3rd Floor");
nameAddresses.put("Transformation monitoring office", "3rd Floor");
nameAddresses.put("Vigilance", "2nd Floor");
nameAddresses.put("Administration", "2nd Floor");
nameAddresses.put("Parliament Cell", "2nd Floor");
nameAddresses.put("Corporate Planning", "2nd Floor");
nameAddresses.put("Coal Import Group", "2nd Floor");
nameAddresses.put("Director(P&SP) Secretariat", "1st Floor");
nameAddresses.put("Business Planning", "1st Floor");
nameAddresses.put("C & IT", "1st Floor");
nameAddresses.put("Medical & Health Service", "1st Floor");
nameAddresses.put("Director(RM&L) Secretariat", "1st Floor");
nameAddresses.put("CRMG", "1st Floor");
nameAddresses.put("ICVL", "1st Floor");
nameAddresses.put("O/o ED(Forest)", "1st Floor");
nameAddresses.put("Reception", "Ground Floor");
nameAddresses.put("State Bank Of India", "Ground Floor");
nameAddresses.put("Library", "Ground Floor");
nameAddresses.put("Basement", "Ground Floor");
nameAddresses.put("Administration-Kaushambi ", "Ground Floor");
Map<String, String> treeMap = new TreeMap<String, String>(nameAddresses);
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = treeMap.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id)
{
super.onListItemClick(l, view, position, id);
if (position == 0) {
Intent intent = new Intent(this, Adminstrative.class);
startActivity(intent);
}
}
}
Mainxml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.brandonmain.listviewexample1.MainActivity">
<ListView
android:id="@+id/results_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
/>
2nd Activity: package com.example.brandonmain.listviewexample1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Adminstrative extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adminstrative);
ListView resultsListView1 = (ListView) findViewById(R.id.administrative);
HashMap<String, String> nameAddresses1 = new HashMap<>();
nameAddresses1.put("xyz", "7th floor");
Map<String, String> treeMap1 = new TreeMap<String, String>(nameAddresses1);
List<HashMap<String, String>> listItems1 = new ArrayList<>();
SimpleAdapter adapter1 = new SimpleAdapter(this, listItems1, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = treeMap1.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap1 = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap1.put("First Line", pair.getKey().toString());
resultsMap1.put("Second Line", pair.getValue().toString());
listItems1.add(resultsMap1);
}
resultsListView1.setAdapter(adapter1);
}
}
administrative.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"
tools:context="com.example.brandonmain.listviewexample1.Adminstrative">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/adminstrative"
android:fastScrollEnabled="true"/>
</LinearLayout