gpt4 book ai didi

android - onListItemClick 不工作

转载 作者:搜寻专家 更新时间:2023-11-01 08:00:17 24 4
gpt4 key购买 nike

未调用函数 onListItemClick:

列表 Activity

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>

主.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>

最佳答案

你应该在 isEnabled 中返回 true 而不是 false

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

来自文档:

Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item).

关于android - onListItemClick 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22192648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com