gpt4 book ai didi

android - 如何创建仅包含某些可扩展项目的列表?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:13 26 4
gpt4 key购买 nike

我正在尝试编写一个列表,其中包含一些可扩展的项目和一些单个项目。我希望拥有它,以便在单击单个项目或可扩展列表子项时,我可以根据项目的文本调用 Intent 。

我想可扩展列表会起作用,但有没有办法在可扩展列表中设置项目,使它们没有可扩展列表图标?我应该使用字符串数组以外的东西吗?

执行此操作的最佳方法是什么?

谢谢

下面是我的不完整代码,我想在项目“Atlanta”和“Boston”之间或作为亚特兰大的子集插入项目“Zone 1”、“Zone 2”、“Zone 3”:

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);
}
}

}

最佳答案

下面的工作和测试代码:

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);
}
}
}

点击“亚特兰大”会导致列表重绘,在仅显示“亚特兰大”或展开列表之间切换。 R.layout.one_city_row.xml 是一个 TextView 的简单布局,R.layout.Expandable_city_row.xml 有额外的子选项作为 TextView 元素,可以从附加的代码中得出结论 fragment 。

使用这种方法,您可以根据需要完全自定义列表。在我放置 Log.i() 的地方,您应该放置您的 Intent 。

关于android - 如何创建仅包含某些可扩展项目的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2994455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com