在按钮单击时从微调器项目选择中添加listview中的项目时出现问题

时间:2011-09-08 05:49:55

标签: android

朋友您好我在按钮上的微调器项目选择中添加listview中的项目时出现问题。在这里,我提供代码,请帮助我解决问题。

    import java.util.ArrayList;

    import android.app.ListActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.Adapter;

    public class DefectlistviewActivity extends ListActivity {


ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;


    /** Called when the activity is first created. */

ListView defectDisplay;



Spinner spinner;

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    spinner = (Spinner)this.findViewById(R.id.Defect);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.defects_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapter);

   // defectDisplay = (ListView) findViewById(R.id.list);


    Button add = (Button) findViewById(R.id.Add_new_Defect_Button);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            startActivity(new Intent("com.android.HCS.AddDialog"));

        }
    });
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 
    }   

    private class EfficientAdapter extends BaseAdapter {

    TextView tv;
  //    private LayoutInflater mInflater;
      Context context;


    @Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View row, ViewGroup convertView) {
    // TODO Auto-generated method stub


    LayoutInflater mInflater =            (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//  mInflater = LayoutInflater.from(context);

 if (null == convertView) {
        row =  mInflater.inflate(R.layout.listview, null);
       } else {
        row =  convertView;
       }


         tv = (TextView) row.findViewById(R.id.Defect1);
       //tv.setText(getItem(position));
         String itemVal = (String)getItem(position);
         tv.setText(itemVal);


       return row;
}
}



public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(final AdapterView<?> parent,
        final View view, final int pos, long id) {
        Button add_defect = (Button) findViewById(R.id.Add_Defect_Button);
        add_defect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 Toast.makeText(parent.getContext(),
                          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

                 setListAdapter(new EfficientAdapter());

            }
        });



    }


    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

}

1 个答案:

答案 0 :(得分:0)

EfficientAdapter中的getCount()函数返回0.所以你不会得到任何关于getView()的回调。此外,getItem(position)返回null,您尝试在EfficientAdapter的getView函数中设置为textview。

我想你应该有一个设置为EfficientAdapter的列表,其大小应该在getCount()中返回,并且在spinner操作中你将你的addtional数据添加到列表并通知你的列表适配器,即...,EfficientAdapter