当前位置: 动力学知识库 > 问答 > 编程问答 >

Android ListView issues. Will not show views added

问题描述:

When I run this code in debug, I can see that my TextViews and seekbars are being added into the array and attached to the adaptor, but they still are not showing on my screen. Ideas?

Thanks

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;

}

}

网友答案:

First, make sure you're calling adapter.notifyDataSetChanged() if you're modifying the array after it's added to the adapter.

Found your bug in SeekBarAdaptor. It's in getView and I commented/fixed it.

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

replace the above line with below

ListView lstTest = (ListView)  findViewById(R.id.mylist);

and in seekbarslist.xml change

android:id="@android:id/list" 

to

android:id="@android:id/mylist"

and try now this one also change

public class ColorsActivity extends ListActivity 

to

public class ColorsActivity extends Activity
分享给朋友:
您可能感兴趣的文章:
随机阅读: