我希望当前列表视图在当前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"
/>
</RelativeLayout>
第二项活动:
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>
每次我用来运行应用程序。崩溃。可能是什么问题?任何人都可以帮助我。我已经使用当前活动的onClickItem
方法调用了第二个活动但是它没有工作
答案 0 :(得分:0)
您使用ListActivity
扩展了活动,因此对于带有 @android:的ListView,您的ID应为android:id="@android:id/list"
。
如果您想要自定义列表视图,只需使用extends AppCompatActivity
作为您的活动,那么您可以使用与上述相同的ID。我希望这会对你有所帮助。
试试这个:
<?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="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
/>
</RelativeLayout>