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

时间:2012-04-03 01:00:33

标签: android listview

当我在调试中运行此代码时,我可以看到我的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");
}



}

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


}

2 个答案:

答案 0 :(得分:1)

首先,如果在将数组添加到适配器后修改数组,请确保调用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;
}


}

答案 1 :(得分:0)

//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