gpt4 book ai didi

Android ListView 问题。不会显示添加的 View

转载 作者:行者123 更新时间:2023-11-30 04:16:07 25 4
gpt4 key购买 nike

当我在调试中运行此代码时,我可以看到我的 TextView 和搜索栏被添加到数组中并附加到适配器,但它们仍然没有显示在我的屏幕上。想法?谢谢

ColorsActivity

public class ColorsActivity extends ListActivity {
/** Called when the activity is first created. */

//Array Adapter that will hold our ArrayList and display the items on the ListView
SeekBarAdaptor seekBarAdaptor;

//List that will host our items and allow us to modify that array adapter
ArrayList<LinearLayout> seekBarArrayList=null;
// TextView myValueText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// LinearLayout myLayout = (LinearLayout)findViewById(R.layout.seekbars);

setContentView(R.layout.seekbarlist);

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout red = (LinearLayout)inflater.from(this).inflate(R.layout.seekbars, null);
CustomSeekBar.createCustomSeekBar(this, red, "red");

//CustomSeekBar blue = new CustomSeekBar(this, "blue");
//CustomSeekBar green = new CustomSeekBar(this, "green");

//Initialize ListView
ListView lstTest= getListView();


//Initialize our ArrayList
seekBarArrayList = new ArrayList<LinearLayout>();
seekBarArrayList.add(red);
//seekBarArrayList.add(blue);
//seekBarArrayList.add(green);

//Initialize our array adapter
seekBarAdaptor = new SeekBarAdaptor(this, R.layout.seekbars, seekBarArrayList);

//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(seekBarAdaptor);
// lstTest.addView(red);



// Amarino.connect(this, "00:11:11:21:05:53");
}



}

自定义搜索栏

 public class CustomSeekBar //implements SeekBar.OnSeekBarChangeListener {
{
static Context myContext;
static TextView myValue;
static TextView myLabel;
static SeekBar mySeekBar;

public static void createCustomSeekBar(Context context, LinearLayout layout, String label){
myContext = context;
mySeekBar = new SeekBar(myContext);
myLabel = new TextView(myContext);
myValue = new TextView(myContext);

myLabel.setText(label);
//mySeekBar.setOnSeekBarChangeListener(this);
layout.addView(myLabel);
layout.addView(myValue);
layout.addView(mySeekBar);


}

/*public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
myValue.setText(progress);
//Amarino.sendDataToArduino(myContext, "00:11:11:21:05:53", 'A', progress);
}
public void onStopTrackingTouch(SeekBar seekBar){

}
public void onStartTrackingTouch (SeekBar seekBar){
}*/


}

seekbars.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBarLayout"
>

</LinearLayout>

搜索栏列表

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mainLayout">

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>

</LinearLayout>

寻找适配器

public class SeekBarAdaptor extends ArrayAdapter<LinearLayout>
{

int resource;
String response;
Context context;

LinearLayout alertView;
TextView seekBarLabel;
TextView seekBarValue;


//Initialize adapter
public SeekBarAdaptor(Context context, int resource, List<LinearLayout> items) {
super(context, resource, items);
this.resource=resource;

}



@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout alertView;
//Get the current alert object
LinearLayout sb = getItem(position);


//Inflate the view
if(convertView==null)
{
alertView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, alertView, true);
}
else
{
alertView = (LinearLayout) convertView;
}

//seekBarLabel = (TextView)alertView.findViewById(R.id.seekBarLabel);

//seekBarLabel.setText(Integer.toString(position));

return alertView;
}


}

最佳答案

首先,如果您在将数组添加到适配器后对其进行修改,请确保调用 adapter.notifyDataSetChanged()。

在 SeekBarAdaptor 中发现了您的错误。它在 getView 中,我评论/修复了它。

public class SeekBarAdaptor extends ArrayAdapter<LinearLayout>
{

int resource;
String response;
Context context;

LinearLayout alertView;
TextView seekBarLabel;
TextView seekBarValue;


//Initialize adapter
public SeekBarAdaptor(Context context, int resource, List<LinearLayout> items) {
super(context, resource, items);
this.resource=resource;

}



@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout alertView;
//Get the current alert object

// I don't know what you're doing here but you're never using it.
LinearLayout sb = getItem(position);


//Inflate the view
if(convertView==null)
{
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);

// here's your bug! you forgot to assign this to alertView.
alertView = vi.inflate(resource, parent, false);
}
else
{
alertView = (LinearLayout) convertView;
}

//seekBarLabel = (TextView)alertView.findViewById(R.id.seekBarLabel);

//seekBarLabel.setText(Integer.toString(position));

return alertView;
}


}

关于Android ListView 问题。不会显示添加的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985821/

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