Android自定义ArrayAdapter Listview不能与异步任务一起单击
我已经被困在这个问题上差不多一天了,并且尝试了很多以前关于堆栈溢出、在线教程等的帖子,但在我的情况下似乎没有任何效果。我已经尝试过为每行列表项中的所有文本视图设置focusable、focusable in touch和clickable to false,为list_item_main.xml的相对布局设置android:genderantfocusability=“blocksDescendants”,但没有任何效果。任何帮助都将不胜感激!谢谢,设置如下: tab.xml(listview的布局):Android自定义ArrayAdapter Listview不能与异步任务一起单击,android,android-fragments,android-listview,android-asynctask,android-arrayadapter,Android,Android Fragments,Android Listview,Android Asynctask,Android Arrayadapter,我已经被困在这个问题上差不多一天了,并且尝试了很多以前关于堆栈溢出、在线教程等的帖子,但在我的情况下似乎没有任何效果。我已经尝试过为每行列表项中的所有文本视图设置focusable、focusable in touch和clickable to false,为list_item_main.xml的相对布局设置android:genderantfocusability=“blocksDescendants”,但没有任何效果。任何帮助都将不胜感激!谢谢,设置如下: tab.xml(listview的
list_item_main.xml(每行项目):
DealAdapter:
public class DealAdapter extends ArrayAdapter<Offer2SaleTransaction> {
private List<Offer2SaleTransaction> activeDeals;
private Context context;
private static final Logger logger = Logger.getLogger(DealAdapter.class.getName());
public DealAdapter(List<Offer2SaleTransaction> activeDeals, Context ctx) {
super(ctx, R.layout.list_item_main, activeDeals);
this.activeDeals = activeDeals;
this.context = ctx;
}
public int getCount() {
if (activeDeals != null)
return activeDeals.size();
return 0;
}
public Offer2SaleTransaction getItem(int position) {
if (activeDeals != null)
return activeDeals.get(position);
return null;
}
public long getItemId(int position) {
if (activeDeals != null)
return activeDeals.get(position).hashCode();
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item_main, null);
}
Offer2SaleTransaction deal = activeDeals.get(position);
TextView textName = (TextView) v.findViewById(R.id.Name);
textName.setText(deal.getName());
TextView textQty = (TextView) v.findViewById(R.id.Qty);
textQty.setText(Float.toString(deal.getQty()));
// Hardcoded date as of now
TextView textDate = (TextView) v.findViewById(R.id.Date);
textDate.setText("01-01-1994");
logger.info("This is something I want to know " + deal.getSaleDate());
TextView textPrice = (TextView) v.findViewById(R.id.Rate);
textPrice.setText(Float.toString(deal.getRate()));
return v;
}
public List<Offer2SaleTransaction> getActiveDeals() {
return activeDeals;
}
public void setActiveDeals(List<Offer2SaleTransaction> activeDeals) {
this.activeDeals = activeDeals;
}}
公共类DealAdapter扩展了ArrayAdapter{
私人上市交易;
私人语境;
私有静态最终记录器Logger=Logger.getLogger(DealAdapter.class.getName());
公共DealAdapter(列出activeDeals,上下文ctx){
超级(ctx,R.layout.list\u item\u main,activeDeals);
this.activeDeals=activeDeals;
this.context=ctx;
}
public int getCount(){
如果(activeDeals!=null)
返回activeDeals.size();
返回0;
}
公开发售2saleTransaction getItem(内部位置){
如果(activeDeals!=null)
返回activeDeals.get(位置);
返回null;
}
公共长getItemId(int位置){
如果(activeDeals!=null)
返回activeDeals.get(position.hashCode();
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
v=充气机充气(R.layout.list\u item\u main,空);
}
Offer2SaleTransaction-deal=activeDeals.get(位置);
TextView textName=(TextView)v.findViewById(R.id.Name);
textName.setText(deal.getName());
TextView textQty=(TextView)v.findViewById(R.id.Qty);
textQty.setText(Float.toString(deal.getQty());
//截至目前的硬编码日期
TextView textDate=(TextView)v.findViewById(R.id.Date);
textDate.setText(“01-01-1994”);
info(“这是我想知道的”+deal.getSaleDate());
TextView textPrice=(TextView)v.findViewById(R.id.Rate);
textPrice.setText(Float.toString(deal.getRate());
返回v;
}
公共列表getActiveDeals(){
退货;
}
public void setActiveDeals(列出activeDeals){
this.activeDeals=activeDeals;
}}
最后,显示listview的片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="400dp"
android:clickable="true"
android:id="@+id/android:list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</LinearLayout>
public class StatusFragment extends ListFragment implements OnItemClickListener{
DealAdapter adpt;
ListView lView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab, container, false);
adpt = new DealAdapter(new ArrayList<Offer2SaleTransaction>(), getActivity());
lView = (ListView) view.findViewById(android.R.id.list);
lView.setAdapter(adpt);
lView.setOnItemClickListener(this);
// Exec async load task
new EndpointsStatusAsyncTask().execute(new Pair<Context, String>(getActivity(), "XXXXXX"));
return view;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getActivity(), "PLEASE WORK",Toast.LENGTH_LONG).show();
}
private class EndpointsStatusAsyncTask extends AsyncTask<Pair<Context, String>, Void, List<Offer2SaleTransaction>> {
private Offer2SaleTransactionApi myApiService = null;
private final Logger logger = Logger.getLogger(EndpointsStatusAsyncTask.class.getName());
private Context context;
private String TAG = "Background";
@Override
protected List<Offer2SaleTransaction> doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
Offer2SaleTransactionApi.Builder builder = new Offer2SaleTransactionApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("https:xxxx");
myApiService = builder.build();
}
context = params[0].first;
String userID = params[0].second;
try {
return myApiService.getOffer2SaleTransaction(userID).execute().getItems();
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(List<Offer2SaleTransaction> result) {
if (result == null)
{
Toast.makeText(context, "It didn't work", Toast.LENGTH_LONG).show();
logger.info("Failure in connecting to execute onPostExecute");
}
else
{
super.onPostExecute(result);
adpt.setActiveDeals(result);
adpt.notifyDataSetChanged();
}
}
}}
public类StatusFragment扩展了ListFragment实现了McClickListener{
adpt;
列表视图lView;
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
视图=充气机。充气(R.layout.tab,容器,假);
adpt=新的DealAdapter(新的ArrayList(),getActivity());
lView=(ListView)view.findviewbyd(android.R.id.list);
lView.setAdapter(adpt);
lView.setonicmclicklistener(这个);
//执行异步加载任务
新端点StatusAsyncTask().execute(新对(getActivity(),“XXXXXX”);
返回视图;
}
public void onItemClick(AdapterView arg0、视图arg1、整型位置、长id){
Toast.makeText(getActivity(),“请工作”,Toast.LENGTH_LONG.show();
}
私有类EndpointStatusAsyncTask扩展了AsyncTask{
private Offer2SaleTransactionApi myApiService=null;
私有最终记录器Logger=Logger.getLogger(EndpointsStatusAsyncTask.class.getName());
私人语境;
私有字符串TAG=“Background”;
@凌驾
受保护列表doInBackground(成对…参数){
如果(myApiService==null){//只执行一次
Offer2SaleTransactionApi.Builder Builder=新Offer2SaleTransactionApi.Builder(AndroidHttp.newCompatibleTransport(),
新的AndroidJsonFactory(),null)
.setRootUrl(“https:xxxx”);
myApiService=builder.build();
}
context=params[0]。首先;
字符串userID=params[0]。秒;
试一试{
返回myApiService.getOffer2SaleTransation(userID.execute().getItems();
}捕获(IOE异常){
返回null;
}
}
@凌驾
受保护的void onPostExecute(列表结果){
如果(结果==null)
{
Toast.makeText(上下文,“它不起作用”,Toast.LENGTH_LONG.show();
logger.info(“连接到execute onPostExecute时失败”);
}
其他的
{
super.onPostExecute(结果);
adpt.setActiveDeals(结果);
adpt.notifyDataSetChanged();
}
}
}}
由于您要扩展ListFragment,因此必须重写onListItemClick
,并使用它代替onItemClick
- 删除McClickListener中的工具
- 删除lView.setonicmclicklistener(this)李>
- 删除mclick
谢谢。它起作用了!现在我觉得自己很愚蠢,因为我一直在看xml文件。
public class StatusFragment extends ListFragment implements OnItemClickListener{
DealAdapter adpt;
ListView lView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab, container, false);
adpt = new DealAdapter(new ArrayList<Offer2SaleTransaction>(), getActivity());
lView = (ListView) view.findViewById(android.R.id.list);
lView.setAdapter(adpt);
lView.setOnItemClickListener(this);
// Exec async load task
new EndpointsStatusAsyncTask().execute(new Pair<Context, String>(getActivity(), "XXXXXX"));
return view;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getActivity(), "PLEASE WORK",Toast.LENGTH_LONG).show();
}
private class EndpointsStatusAsyncTask extends AsyncTask<Pair<Context, String>, Void, List<Offer2SaleTransaction>> {
private Offer2SaleTransactionApi myApiService = null;
private final Logger logger = Logger.getLogger(EndpointsStatusAsyncTask.class.getName());
private Context context;
private String TAG = "Background";
@Override
protected List<Offer2SaleTransaction> doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
Offer2SaleTransactionApi.Builder builder = new Offer2SaleTransactionApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("https:xxxx");
myApiService = builder.build();
}
context = params[0].first;
String userID = params[0].second;
try {
return myApiService.getOffer2SaleTransaction(userID).execute().getItems();
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(List<Offer2SaleTransaction> result) {
if (result == null)
{
Toast.makeText(context, "It didn't work", Toast.LENGTH_LONG).show();
logger.info("Failure in connecting to execute onPostExecute");
}
else
{
super.onPostExecute(result);
adpt.setActiveDeals(result);
adpt.notifyDataSetChanged();
}
}
}}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "PLEASE WORK",Toast.LENGTH_LONG).show();k(l, v, position, id);
}