AlertDialog selectedItem为null

时间:2012-09-14 06:07:23

标签: android android-arrayadapter android-dialog onitemclicklistener

enter image description here

我有ListwithImage列表的代码。但是当我尝试使用Dialog onclickItem时,但是Dialog显示为null。我实现了onclickItemListener。我尝试了没有图像的代码,Dialog显示选中的列表。但是当我尝试使用图像时,它将无法正常工作。

mycode的:

public class MainActivity extends Activity {
     EditText edittext;
     ListView listview;

     String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
               "Eight", "Nine", "Ten" };

             int[] image = { R.drawable.one, R.drawable.two, R.drawable.three,
               R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
               R.drawable.eight, R.drawable.nine, R.drawable.ten };

             int textlength = 0;

             ArrayList<String> text_sort = new ArrayList<String>();
             ArrayList<Integer> image_sort = new ArrayList<Integer>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edittext = (EditText) findViewById(R.id.EditText01);
          listview = (ListView) findViewById(R.id.ListView01);
          listview.setAdapter(new MyCustomAdapter(text, image));

          edittext.addTextChangedListener(new TextWatcher()
          {

           public void afterTextChanged(Editable s)
           {

           }

           public void beforeTextChanged(CharSequence s, int start,
            int count, int after)
           {

           }

           public void onTextChanged(CharSequence s, int start,
            int before, int count)
           {

            textlength = edittext.getText().length();
            text_sort.clear();
            image_sort.clear();

            for (int i = 0; i < text.length; i++)
            {
             if (textlength <= text[i].length())
             {
              if (edittext.getText().toString().
           equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
              {
               text_sort.add(text[i]);
               image_sort.add(image[i]);
              }
             }
            }

            listview.setAdapter(new MyCustomAdapter
             (text_sort, image_sort));

            listview.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long rowId) {
                    // TODO Auto-generated method stub
                    listview.getItemAtPosition(position);
                    AlertDialog.Builder adb = new AlertDialog.Builder(
                            MainActivity.this);
                            adb.setTitle("List");
                            adb.setMessage(" selected Item is="
                            + listview.getItemAtPosition(position));
                            adb.setPositiveButton("Ok", null);
                            adb.show();    

                }

            });

           }
          });
         }

         class MyCustomAdapter extends BaseAdapter
         {

          String[] data_text;
          int[] data_image;

          MyCustomAdapter()
          {

          }

          MyCustomAdapter(String[] text, int[] image)
          {
           data_text = text;
           data_image = image;
          }

          MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image)
          { 

           data_text = new String[text.size()];
           data_image = new int[image.size()];

           for(int i=0;i<text.size();i++)
           {
            data_text[i] = text.get(i);
            data_image[i] = image.get(i);
           }

          }

          public int getCount()
          {
           return data_text.length;
          }

          public String getItem(int position)
          {
           return null;
          }

          public long getItemId(int position)
          {
           return position;
          }

          public View getView(int position, View convertView, ViewGroup parent)
          {

           LayoutInflater inflater = getLayoutInflater();
           View row;

           row = inflater.inflate(R.layout.listview, parent, false);

           TextView textview = (TextView) row.findViewById(R.id.TextView01);
           ImageView imageview = (ImageView) row
             .findViewById(R.id.ImageView01);

           textview.setText(data_text[position]);
           imageview.setImageResource(data_image[position]);

           return (row);

          }
         }

2 个答案:

答案 0 :(得分:1)

您的警报中出现错误值 错

   +listview.getItemAtPosition(position));

 adb.setMessage(" selected Item is="+text_sort.get(position).toString());

答案 1 :(得分:1)

尝试更改此内容:

adb.setMessage(" selected Item is= " + listview.getItemAtPosition(position));

对此:

adb.setMessage(" selected Item is= " + text_sort.get(position));