如何将个人/相关列表添加到字母顺序gridview?

时间:2016-01-29 11:54:05

标签: android listview android-gridview

任何人都可以告诉我如何将列表项添加到网格视图...例如..我有像A..Z字母的网格视图。 。如果我点击字母A应该转到另一个布局并显示显示A内容[即相关列表]  如果我点击B字母的网格视图,应该在另一个布局中显示b相关列表,很快按钮点击.. 我希望你有我的要求。请你告诉我我该怎么做.. 希望你重播我。

例如:: 我有一个像这样的列表网格视图 [A B C D  E F G H ........ ....... Z] 我要求的答案是: 如果我们点击应该显示的字母A. A [按下]然后 阿沛 阿贾伊 阿伦 如果B字母按下那么 B [按下] Brahmaiah bharth bhargav。 像上面我想显示网格视图。请帮助我

GridViewActivity.java

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;
            intent = new Intent(getApplicationContext(), ListActivity.class);

            startActivity(intent);
        }
    });

}

} .... 这是我的活动类..我想将相关内容添加到列表中的每个字母。 ...

1 个答案:

答案 0 :(得分:0)

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.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 OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                   Intent intent = null;
                   intent = new Intent(getApplicationContext(), ListActivity.class);
                   // Send the position to the next activity
                   // i.e. position 0 -> numbers[0] -> "A"
                   intent.putExtra("PositionToList", position);
                   startActivity(intent);
                }
            });

        }

    }

ListActivity.java

public class ListActivity extends Activity {

    // Dummy array, Only for example purposes!!! 
    // In real life you will fetch data from somewhere else (i.e. DB, file, resources, network)
    static final String[][] list = new String[][] { 
                {"Abhay", "Ajay", "Arun"},
                {"Brahmaiah", "bharth", "bhargav"},
                ....,
                ....,
                ....};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the position in order to know the data to be displayed
        Intent intent = getIntent();
        int position = intent.getIntExtra("PositionToList", 0); // here 0 is the default value

        // Set the content to display in your adapter
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list[position]);
        ListView myList = (ListView)findViewById(R.id.listView);
        myList.setAdapter(myAdapter);
    }
}

重要 这里显示的真正要点不是代码本身(我没有检查它是否编译顺便说一下),而是如何与其他活动进行通信的想法。

  1. 首先,您要在GridActivity中选择一些内容,然后根据该选择在下一个Activity中显示正确的内容。这是使用意图

  2. 完成的
  3. 其次,根据上一个活动传递的信息,您可以在ListActivity中决定显示内容的内容和方式。