在项目上单击列表视图没有响应

时间:2013-08-07 19:09:52

标签: android listview

我为Listview设置了自定义适配器,但我的itemClickEvent无效。我已将我的适配器包含在我的主要活动中。我已经尝试了很多建议,但还没有得到任何解决方案。

public class MainActivity extends Activity {

private ListView listView;

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

     Model.LoadModel();
        listView = (ListView) findViewById(R.id.list);
        String[] ids = new String[Model.Items.size()];
        for (int i= 0; i < ids.length; i++){

            ids[i] = Integer.toString(i+1);
        }

        GraphListAdapter adapter = new GraphListAdapter (this,R.layout.list_row, ids);
        listView.setAdapter(adapter);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class GraphListAdapter extends BaseAdapter {

     private final Context context;
        private final String[] Ids;
        private final int rowResourceId;

        public GraphListAdapter(Context context, int textViewResourceId, String[] objects) {

            super();

            this.context = context;
            this.Ids = objects;
            this.rowResourceId = textViewResourceId;

        }

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

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(rowResourceId, parent, false);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
            TextView textView = (TextView) rowView.findViewById(R.id.dashboard_name);
           TextView textView2 = (TextView) rowView.findViewById(R.id.dashboard_info);

            int id = Integer.parseInt(Ids[position]);
            String imageFile = Model.GetbyId(id).imageName;

            textView.setText(Model.GetbyId(id).dashboardName);
            textView2.setText(Model.GetbyId(id).information);
            // get input stream
            InputStream ims = null;
            try {
                ims = context.getAssets().open(imageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            imageView.setImageDrawable(d);
            return rowView;

        }




        private OnItemClickListener clickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // TODO Auto-generated method stub
                System.out.println(pos);

            }
        };

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

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

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


 }

我的列表行包含图像和文本...................

5 个答案:

答案 0 :(得分:2)

listView.setOnItemClickListener

中使用onCreate()
GraphListAdapter adapter = new GraphListAdapter (this,R.layout.list_row, ids);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }
        });

答案 1 :(得分:1)

尝试使用

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> l, View v, int position,
                long id) {
    });

in onCreate ...希望这能解决你的问题......:)

答案 2 :(得分:0)

clickListener没有附加任何内容。将其移至OnCreate并使用listView.setOnItemClickListener

答案 3 :(得分:0)

您需要在listView上设置clickListener。

例如:

datumSpinner = (Spinner) findViewById(R.id.spinner1);
datumSpinner.setOnItemSelectedListener(this);

答案 4 :(得分:0)

listView.setOnItemClickListener(clickListener);在Oncreate中调用它