过去2天我无法纠正以下代码问题。我不知道如何将自定义列表项添加到现有网格视图。 我已经完成并编译了这段代码而没有任何错误。 但遗憾的是,点击网格视图字母后,可点击不会出现此问题。 我的要求就像这样:如果按下网格视图字母[“A”],应转到另一个布局,并应显示项目列表[任何列表/图标菜单/带图标的列表]。 所以请尽快让我摆脱这个问题。 我使用的代码:
GridViewActivity.java
public class GridViewActivity extends Activity {
GridView gridView;
static final String[] numbers = new String[]{
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
""+position, Toast.LENGTH_SHORT).show();
Intent intent = null;
if(position == 0){
intent = new Intent(v.getContext(), ListActivity.class);
// intent.putExtra("title", list.add(list));
}
if(position == 1){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 2){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 3){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position ==4){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 5){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 6){
intent = new Intent(v.get Context(), List Activity.class);}
if(position == 7){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 8){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 9){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 10){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 11){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 12) {
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 13){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 14){
intent = new Intent(v.get Context(), List Activity.class);
}
if(position == 15){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 16){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 17){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 18){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 19){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 20){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 21){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 22){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 23){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 23){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 24){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 25){
intent = new Intent(v.getContext(), ListActivity.class);
}
if(position == 26){
intent = new Intent(v.getContext(), ListActivity.class);
}
startActivity(intent);
}
});
}
}
main.xml中:
<?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:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:entries="@array/list"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
ListActivity.java:
package com.example.brahmaiah.grid_view;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
/**
* Created by brahmaiah on 1/29/2016.
*/
public class ListActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_grid_view, menu);
return true;
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
我认为这些内容足以理解此应用代码的问题。 o / p就像...网格视图一直在显示但是在点击网格视图之后,像{A,B ...}这样的项目正在显示这样的消息。不幸的是,列表活动关闭了
答案 0 :(得分:0)
首先清理你的代码,消除所有那些不必要的“if”块。然后在代码中更改以下行:
intent = new Intent(v.getContext(), ListActivity.class);
到
intent = new Intent(getApplicationContext(), ListActivity.class);
修改强> 另外,检查ListActivity是否在您的清单中:
<activity android:name=".ListActivity"></activity>