onListItemClick无法正常工作

时间:2014-03-05 08:47:12

标签: android

未调用函数onListItemClick

ListActivity

package it.devapp.listview;


import java.util.ArrayList;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListNewsActivity extends ListActivity {

ArrayList<News> news;
Util util = new Util();

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

    setContentView(R.layout.main);

    LoadData task = new LoadData();
    task.execute();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {


    News news = (News) getListAdapter().getItem(position);
    Intent i = new Intent(getApplicationContext(), NewsActivity.class);
    i.putExtra("news",news);
    startActivity(i);

}


private final class MyAdapter extends BaseAdapter
{

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

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


        RowWrapper wrapper;
        if (convertView == null)
        {
            convertView = getLayoutInflater().inflate(R.layout.activity_list_news, null);
            wrapper = new RowWrapper(convertView);
            convertView.setTag(wrapper);
        }
        else
        {
            wrapper = (RowWrapper) convertView.getTag();
        }

        News news = (News) getItem(position);
        wrapper.poulate(news);

        return convertView;
    }

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

    @Override
    public Object getItem(int position)
    {
        //return News.notizie[position];
        return news.get(position);
    }

    @Override
    public int getCount()
    {
        return news.size();
    }

}


private static class RowWrapper
{
    private TextView data,titolo;
    private ImageView immagine;



    public RowWrapper(View convertView)
    {

        data = (TextView) convertView.findViewById(R.id.datanews);
        immagine = (ImageView) convertView.findViewById(R.id.immagine);
        titolo = (TextView) convertView.findViewById(R.id.titolo);


    }

    public void poulate(News news)
    {

        titolo.setText(news.getTitolo());
        data.setText(news.getDate());

        if ("".equals(news.getImg())){
            immagine.setImageResource(R.drawable.news);

        }else{
            UrlImageViewHelper.setUrlDrawable(immagine, news.getImg(),null, 60000);
        }

    }
}

public class LoadData extends AsyncTask<Void, Void, ArrayList<News>> {
    ProgressDialog progressDialog;
    //declare other objects as per your need
    @Override
    protected void onPreExecute()
    {
        progressDialog= ProgressDialog.show(ListNewsActivity.this, "Caricamento dati","Attendere prego", true);

        //do initialization of required objects objects here                
    };      
    @Override
    protected ArrayList<News> doInBackground(Void... params)
    {   

        news =  util.getNews();


        return news;
    }       
    @Override
    protected void onPostExecute(ArrayList<News> news)
    {
        super.onPostExecute(news);
        setListAdapter(new MyAdapter());
        progressDialog.dismiss();
    };
 }

}

activity_list_news.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dip"
    android:layout_marginRight="3dip"
     >
   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dip"
    android:layout_marginRight="3dip"
    android:background="#aaFFFFFF"

     >
    <ImageView
            android:id="@+id/immagine"
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:layout_marginLeft="2dip"
    />


    <TextView
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="5dip"
            android:id="@+id/datanews"
            android:layout_toRightOf="@id/immagine"
            android:layout_width="wrap_content"
            style="@style/Date"
            android:layout_height="25dip"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"
            />


     <TextView
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:id="@+id/titolo"
            android:layout_toRightOf="@id/immagine"
            android:layout_width="wrap_content"
            style="@style/Titolo"
            android:layout_below="@+id/datanews"
            android:layout_height="50dip"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"
            />
     </RelativeLayout>

</RelativeLayout>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sfondo"> 

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:layout_gravity="center"
    android:src="@drawable/logo" 
    android:layout_marginTop="10dip"
    />

 <ListView

    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false"
    android:cacheColorHint="#00000000"
    android:divider="#00000000"

    />

</LinearLayout>

1 个答案:

答案 0 :(得分:4)

您应该在true

中返回false而不是isEnabled
   @Override
    public boolean isEnabled(int position) {
        return false;
    }

来自文档:

  

如果指定位置的项目不是分隔符,则返回true。   (分隔符是不可选择的,不可点击的项目。)