1

我一直堅持這個問題近一天了,並嘗試了無數以前的帖子在堆棧溢出,在線教程等,但似乎沒有在我的情況下工作。我已經嘗試設置focusable,focusable touch和clickable爲false在每行列表項中的所有textview,在list_item_main.xml的相對佈局中設置android:descendantFocusability =「blocksDescendants」,但沒有任何工作。任何幫助將真正被讚賞!感謝的設置如下:Android自定義ArrayAdapter Listview不可點擊與異步任務

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"/> 

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; 
}} 

Final LY,片段顯示列表視圖:

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(); 
     } 
    } 
}} 

回答

0

,因爲要擴展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); 
} 
+0

非常感謝。有用!感覺愚蠢的,因爲我一直在看xml文件。 – 2015-03-08 21:11:46

+0

歡迎您 – Blackbelt 2015-03-08 21:15:39