开发者

getting problem to add items in listview from spinner item selection on button click

Hello friends i m getting problem to add items in listview from spinner item selection on button. Here i m providing code please help me to get rid of the problem.

    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
 开发者_如何学Go   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.
    }
}

}


Your getCount() function in EfficientAdapter is returning 0. So you wont get any callback to getView(). Also getItem(position) is returning null which you trying to set to textview in getView funcion of EfficientAdapter .

I guess you should have a list set to EfficientAdapter who's size should be returned in getCount() and on spinner operation you add your addtional data to the list and notify your list adaptor ie..,EfficientAdapter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜