我怎样才能创建一个只有某些项目的列表扩展? [英] How can I create a list with only certain items expandable?

查看:160
本文介绍了我怎样才能创建一个只有某些项目的列表扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图组成一个清单,一些项目扩展以及一些单品。我希望有它,这样被点击无论是单个项目或扩展列表儿童在,我可以根据项目的文本调用的意图。

我想扩展列表会的工作,但有没有办法设置项可扩展列表,使他们不具备扩展列表图标?我应该使用比字符串数组以外的东西?

什么是做到这一点的最好方法是什么?

感谢

下面是我的不完整的code,我想插入项目之间的项目区域1,2区,3区,亚特兰大和波士顿或亚特兰大的一个子集:

 包test.lists.special;进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;公共类SpecialList扩展ListActivity {
TextView中的TopText;
的String []项目= {亚特兰大,波士顿,芝加哥,小牛};@覆盖 公共无效的onCreate(捆绑冰柱){
  super.onCreate(冰柱);
  的setContentView(R.layout.main);
  setListAdapter(新ArrayAdapter<串GT;(这一点,
  android.R.layout.simple_list_item_1,
  项));
  的TopText =(的TextView)findViewById(R.id.toptext);
 }公共无效onListItemClick(ListView的父母,视图V,INT位置,
 长ID){
     意向意图=新的Intent();     如果(parent.getItemAtPosition(位置)==亚特兰大)
     {
           //这是我想插入ZONE 1,2区,ZONE 3
                  亚特兰大和波士顿在列表之间//
     }
     否则,如果(parent.getItemAtPosition(位置)==波士顿)
     {
          intent.setClass(这一点,test.lists.special.Boston.class);
           startActivity(意向);
     }
     否则,如果(parent.getItemAtPosition(位置)==芝加哥)
     {
             intent.setClass(这一点,test.lists.special.Chicago.class);
              startActivity(意向);
     }
     否则,如果(parent.getItemAtPosition(位置)==达拉斯)
     {
            intent.setClass(这一点,test.lists.special.Dallas.class);
             startActivity(意向);
     }
     否则,如果(parent.getItemAtPosition(位置)==1区)
     {
            intent.setClass(这一点,test.lists.special.Atlanta.Zone1.class);
             startActivity(意向);
     }
     否则,如果(parent.getItemAtPosition(位置)==2区)
     {
            intent.setClass(这一点,test.lists.special.Atlanta.Zone2.class);
             startActivity(意向);
     }
     否则,如果(parent.getItemAtPosition(位置)==3区)
     {
            intent.setClass(这一点,test.lists.special.Atlanta.Zone3.class);
             startActivity(意向);
     }
    }}


解决方案

工作和测试code如下:

 进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;公共类SpecialList扩展ListActivity {    TextView中的TopText;
    的String []项目= {亚特兰大,波士顿,芝加哥,小牛};
    私人CityListAdapter mListOfCities;    布尔mAtlantaListExpanded;    @覆盖
    公共无效的onCreate(捆绑冰柱){
        super.onCreate(冰柱);
        的setContentView(R.layout.main);        mAtlantaListExpanded = FALSE;
        mListOfCities =新CityListAdapter();
        setListAdapter(mListOfCities);        对于(INT N = 0; N< items.length; N ++)
        {
            mListOfCities.add(项目[N]);
        }
    }    公共无效onListItemClick(ListView的父母,视图V,INT位置,长的ID)
    {
         意向意图=新的Intent();         如果(parent.getItemAtPosition(位置)==亚特兰大)
         {
             !mAtlantaListExpanded = mAtlantaListExpanded;
             mListOfCities.clear();
             对于(INT N = 0; N< items.length; N ++)
             {
                mListOfCities.add(项目[N]);
             }
             Log.i(SpecialList,亚特兰大);
         }
         否则,如果(parent.getItemAtPosition(位置)==波士顿)
         {
             Log.i(SpecialList,波士顿);
         }
         否则,如果(parent.getItemAtPosition(位置)==芝加哥)
         {
             Log.i(SpecialList,芝加哥);
         }
         否则,如果(parent.getItemAtPosition(位置)==达拉斯)
         {
             Log.i(SpecialList,达拉斯);
         }
    }    类CityListAdapter扩展ArrayAdapter<串GT; {        CityListAdapter(){
            超(SpecialList.this,R.layout.one_city_row);
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            查看排= NULL;
            LayoutInflater吹气= getLayoutInflater();            如果((位置== 0)及和放大器;!(mAtlantaListExpanded))
            {
                行= inflater.inflate(R.layout.expandable_city_row,父母,假);
                TextView的城市=(TextView中)row.findViewById(R.id.Expandable_TextView01);
                city​​.setText(项目[位置]);                TextView的cityZone1 =(TextView中)row.findViewById(R.id.Zone_TextView01);
                city​​Zone1.setText(1区);                TextView的cityZone2 =(TextView中)row.findViewById(R.id.Zone_TextView02);
                city​​Zone2.setText(2区);                TextView的cityZone3 =(TextView中)row.findViewById(R.id.Zone_TextView03);
                city​​Zone3.setText(3区);                city​​Zone1.setOnClickListener(
                    新Button.OnClickListener(){
                        @覆盖
                        公共无效的onClick(视图v){
                             Log.i(SpecialList,区域1);
                        }
                    }
                );                city​​Zone2.setOnClickListener(
                    新Button.OnClickListener(){
                        @覆盖
                        公共无效的onClick(视图v){
                             Log.i(SpecialList,2区);
                        }
                    }
                );                city​​Zone3.setOnClickListener(
                    新Button.OnClickListener(){
                        @覆盖
                        公共无效的onClick(视图v){
                             Log.i(SpecialList,3区);
                        }
                    }
                );
            }
            其他
            {
                行= inflater.inflate(R.layout.one_city_row,父母,假);
                TextView的城市=(TextView中)row.findViewById(R.id.C​​ity_TextView01);
                city​​.setText(项目[位置]);
            }            返回(行);
        }
    }
}

点击列表中的亚特兰大的原因重绘,只显示亚特兰大或者扩展列表之间切换。 R.layout.one_city_row.xml 是简单的布局一个TextView的, R.layout.Expandable_city_row.xml 有额外的子选项作为TextView的元素,可从所附code段结束。

使用这种方法,你完全可以根据自己的需要自定义列表。在这里我把地方 Log.i(),你应该把你的意图。

I am trying to compose a list with some items expandable and some single items. I wish to have it so that when either a single item or expandable list child is clicked, I can call an intent based on the text of the item.

I suppose expandable lists would work, but is there a way to set items in an expandable list so that they don't have the expandable list icon? Should I use something other than a string array?

What is the best way to do this?

Thanks

Below is my incomplete code, I would like to insert the items "Zone 1", "Zone 2", "Zone 3" between the items "Atlanta" and "Boston" or as a subset of Atlanta:

package test.lists.special;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SpecialList extends ListActivity{
TextView toptext;
String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};

@Override

 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  setListAdapter(new ArrayAdapter<String>(this,
  android.R.layout.simple_list_item_1,
  items));
  toptext=(TextView)findViewById(R.id.toptext);
 }

public void onListItemClick(ListView parent, View v, int position,
 long id) {
     Intent intent = new Intent();

     if (parent.getItemAtPosition(position)=="Atlanta")
     {
           //THIS IS WHERE I WISH TO INSERT "ZONE 1", "ZONE 2", "ZONE 3" 
                  //between Atlanta and Boston in the list
     }
     else if (parent.getItemAtPosition(position)=="Boston")
     {
          intent.setClass(this, test.lists.special.Boston.class);
           startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Chicago")
     {
             intent.setClass(this, test.lists.special.Chicago.class);
              startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Dallas")
     {
            intent.setClass(this, test.lists.special.Dallas.class);
             startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Zone 1")
     {
            intent.setClass(this, test.lists.special.Atlanta.Zone1.class);
             startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Zone 2")
     {
            intent.setClass(this, test.lists.special.Atlanta.Zone2.class);
             startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Zone 3")
     {
            intent.setClass(this, test.lists.special.Atlanta.Zone3.class);
             startActivity(intent); 
     }
    }

}

解决方案

Working and tested code below:

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.ArrayAdapter; 
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SpecialList extends ListActivity{ 

    TextView toptext; 
    String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};
    private CityListAdapter mListOfCities;

    boolean mAtlantaListExpanded;

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

        mAtlantaListExpanded = false;
        mListOfCities = new CityListAdapter();
        setListAdapter(mListOfCities);

        for (int n=0; n < items.length; n++)
        {
            mListOfCities.add(items[n]);
        }
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) 
    { 
         Intent intent = new Intent(); 

         if (parent.getItemAtPosition(position)=="Atlanta") 
         { 
             mAtlantaListExpanded = !mAtlantaListExpanded;
             mListOfCities.clear();
             for (int n=0; n < items.length; n++)
             {
                mListOfCities.add(items[n]);
             }           
             Log.i("SpecialList", "Atlanta");            
         } 
         else if (parent.getItemAtPosition(position)=="Boston") 
         { 
             Log.i("SpecialList", "Boston");
         } 
         else if (parent.getItemAtPosition(position)=="Chicago") 
         { 
             Log.i("SpecialList", "Chicago");
         } 
         else if (parent.getItemAtPosition(position)=="Dallas") 
         { 
             Log.i("SpecialList", "Dallas");
         } 
    }

    class CityListAdapter extends ArrayAdapter<String> {    

        CityListAdapter() {         
            super(SpecialList.this, R.layout.one_city_row);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            View row = null;
            LayoutInflater inflater = getLayoutInflater();

            if ((position == 0) && (!mAtlantaListExpanded))
            {
                row = inflater.inflate(R.layout.expandable_city_row, parent, false);
                TextView city = (TextView) row.findViewById(R.id.Expandable_TextView01);
                city.setText(items[position]);

                TextView cityZone1 = (TextView) row.findViewById(R.id.Zone_TextView01);
                cityZone1.setText("Zone 1");

                TextView cityZone2 = (TextView) row.findViewById(R.id.Zone_TextView02);
                cityZone2.setText("Zone 2");

                TextView cityZone3 = (TextView) row.findViewById(R.id.Zone_TextView03);
                cityZone3.setText("Zone 3");

                cityZone1.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             Log.i("SpecialList", "Zone 1");
                        }
                    }
                );

                cityZone2.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             Log.i("SpecialList", "Zone 2");
                        }
                    }
                );

                cityZone3.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             Log.i("SpecialList", "Zone 3");
                        }
                    }
                );              
            }
            else
            {
                row = inflater.inflate(R.layout.one_city_row, parent, false);
                TextView city = (TextView) row.findViewById(R.id.City_TextView01);
                city.setText(items[position]);
            }                   

            return(row);
        }
    }
} 

Click on "Atlanta" cause redraw of the list, toggling between showing only "Atlanta" or expanded list. R.layout.one_city_row.xml is simple layout with one TextView, R.layout.Expandable_city_row.xml has additional suboptions as TextView elements, as may be concluded from the attached code snippet.

Using this approach you may customize list completely according to your needs. On the places where I put Log.i(), you should put your intents.

这篇关于我怎样才能创建一个只有某些项目的列表扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆