0

在此处输入图像描述

我有 ListwithImage 列表的代码。但是当我尝试使用 Dialog 显示 onclickItem 时,但 Dialog 显示为空。我实现 onclickItemListener 。我试过这个没有图像的代码,对话框显示选定的列表。但是当我尝试使用图像时,它不起作用。

我的代码:

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

          }
         }
4

2 回答 2

1

尝试改变这个:

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

对此:

adb.setMessage(" selected Item is= " + text_sort.get(position));
于 2012-09-14T07:38:23.340 回答
1

你在你的警报中得到错误的值

   +listview.getItemAtPosition(position));

正确的

 adb.setMessage(" selected Item is="+text_sort.get(position).toString());
于 2012-09-14T06:29:30.663 回答