Android 自定义 ArrayAdapter Listview 无法通过异步任务点击
Android Custom ArrayAdapter Listview is not clickable with Async Task
我已经被这个问题困了将近一天了,并且尝试了很多关于 Stack Overflow、在线教程等的帖子,但似乎对我来说没有任何效果。我已经尝试为每个行列表项中的所有文本视图设置可聚焦、可触摸聚焦和可点击为 false,在 list_item_main.xml 的相对布局中设置 android:descendantFocusability="blocksDescendants" 但没有任何效果。任何帮助将不胜感激!谢谢 设置如下:
tab.xml(列表视图布局):
<?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>
list_item_main.xml(每行项目):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rLayout">
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XYZ"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#808080"
android:layout_above="@+id/weight"
android:layout_alignLeft="@+id/Qty"
android:layout_alignStart="@+id/Qty"/>
<TextView
android:id="@+id/Qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:text="150"
android:textSize="18sp"
android:textColor="#808080"
android:layout_marginLeft="39dp"
android:layout_marginStart="39dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="87dp"/>
<TextView
android:id="@+id/dateTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:text="Available from: "
android:textSize="18sp"
android:textColor="#808080"
android:layout_alignTop="@+id/Date"
android:layout_alignLeft="@+id/Qty"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" 0.000"
android:textStyle="bold"
android:textSize="24sp"
android:id="@+id/Rate"
android:textColor="#808080"
android:layout_alignTop="@+id/Name"
android:layout_toRightOf="@+id/Date"
android:layout_toEndOf="@+id/Date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="14/08/2007"
android:textSize="18sp"
android:id="@+id/Date"
android:textColor="#808080"
android:layout_below="@+id/weight"
android:layout_toRightOf="@+id/dateTitle"
android:layout_toEndOf="@+id/dateTitle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" Kgs"
android:id="@+id/weight"
android:textSize="18sp"
android:textColor="#808080"
android:layout_alignTop="@+id/Qty"
android:layout_alignRight="@+id/Name"
android:layout_alignEnd="@+id/Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="INR"
android:id="@+id/currency"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#808080"
android:layout_alignTop="@+id/Rate"
android:layout_toRightOf="@+id/Rate"
android:layout_toEndOf="@+id/Rate"/>
交易适配器:
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;
}}
最后,显示列表视图的片段:
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();
}
}
}}
由于您正在扩展 ListFragment,因此您必须覆盖 onListItemClick
,并用它代替 onItemClick
。
- 删除实现 OnItemClickListener
- 删除lView.setOnItemClickListener(这个);
- 删除 onItemClick
然后
@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);
}
我已经被这个问题困了将近一天了,并且尝试了很多关于 Stack Overflow、在线教程等的帖子,但似乎对我来说没有任何效果。我已经尝试为每个行列表项中的所有文本视图设置可聚焦、可触摸聚焦和可点击为 false,在 list_item_main.xml 的相对布局中设置 android:descendantFocusability="blocksDescendants" 但没有任何效果。任何帮助将不胜感激!谢谢 设置如下:
tab.xml(列表视图布局):
<?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>
list_item_main.xml(每行项目):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rLayout">
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XYZ"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#808080"
android:layout_above="@+id/weight"
android:layout_alignLeft="@+id/Qty"
android:layout_alignStart="@+id/Qty"/>
<TextView
android:id="@+id/Qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:text="150"
android:textSize="18sp"
android:textColor="#808080"
android:layout_marginLeft="39dp"
android:layout_marginStart="39dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="87dp"/>
<TextView
android:id="@+id/dateTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:text="Available from: "
android:textSize="18sp"
android:textColor="#808080"
android:layout_alignTop="@+id/Date"
android:layout_alignLeft="@+id/Qty"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" 0.000"
android:textStyle="bold"
android:textSize="24sp"
android:id="@+id/Rate"
android:textColor="#808080"
android:layout_alignTop="@+id/Name"
android:layout_toRightOf="@+id/Date"
android:layout_toEndOf="@+id/Date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="14/08/2007"
android:textSize="18sp"
android:id="@+id/Date"
android:textColor="#808080"
android:layout_below="@+id/weight"
android:layout_toRightOf="@+id/dateTitle"
android:layout_toEndOf="@+id/dateTitle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" Kgs"
android:id="@+id/weight"
android:textSize="18sp"
android:textColor="#808080"
android:layout_alignTop="@+id/Qty"
android:layout_alignRight="@+id/Name"
android:layout_alignEnd="@+id/Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="INR"
android:id="@+id/currency"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#808080"
android:layout_alignTop="@+id/Rate"
android:layout_toRightOf="@+id/Rate"
android:layout_toEndOf="@+id/Rate"/>
交易适配器:
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;
}}
最后,显示列表视图的片段:
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();
}
}
}}
由于您正在扩展 ListFragment,因此您必须覆盖 onListItemClick
,并用它代替 onItemClick
。
- 删除实现 OnItemClickListener
- 删除lView.setOnItemClickListener(这个);
- 删除 onItemClick
然后
@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);
}