[英]Android ListView issues. Will not show views added
当我在调试中运行此代码时,可以看到我的TextView
和seekbars已添加到数组中并附加到适配器上,但是它们仍未显示在屏幕上。 想法? 谢谢
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");
}
}
CustomSeekBar
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>
seekbarslist
<?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>
seekbaradaptor
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;
}
}
//Initialize ListView
ListView lstTest= getListView();
将上面的行替换为下面的行
ListView lstTest = (ListView) findViewById(R.id.mylist);
并在seekbarslist.xml
更改
android:id="@android:id/list"
至
android:id="@android:id/mylist"
并尝试现在这个也改变
public class ColorsActivity extends ListActivity
至
public class ColorsActivity extends Activity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.