2013-07-15 19 views
-2

我有一個需要點擊的新東西列表,但是這個不起作用。 onListItemClick永遠不會被調用。我的應用程序中有另一個按預期工作,我無法弄清楚有什麼不同。它發生在我身上,也許有衝突,因爲它們都使用提供的@ id/android:list,但它們處於不同的活動中,而且我還沒有發現其他人抱怨同一個問題,所以我不確定。使用@ id/android的Android多重列表:列表

這是一堆代碼。我將不勝感激任何建議。

工作:

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

    // get the item that was clicked 
    v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
      position); 

    Intent myIntent = new Intent(this, Details.class); 

    myIntent.putExtra(res.getString(R.string.project), project); 

    startActivity(myIntent); 
}// onListItemClick 

不工作:

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

    // get the item that was clicked 
    final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
      position); 

    AlertDialog.Builder builder = new AlertDialog.Builder(Share.this); 
    builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title)) 
      .setMessage(res.getString(R.string.share_check_dialog_text)) 
      .setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        sendShareEmail(vSitePeople); 
       } 
      }).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        // cancelled, so do nothing 
       } 
      }); 
    AlertDialog msgBox = builder.create(); 
    msgBox.show(); 
}// onListItemClick 

使用XML:

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

<!-- dummy item to prevent edittext from gaining focus on activity start --> 

<LinearLayout 
    android:layout_width="0px" 
    android:layout_height="0px" 
    android:focusable="true" 
    android:focusableInTouchMode="true" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/title_background" > 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/ic_logo" > 
    </ImageView> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/logo" 
     android:gravity="center" 
     android:paddingBottom="5dp" 
     android:paddingLeft="50dp" 
     android:paddingRight="60dp" 
     android:paddingTop="5dp" 
     android:text="@string/app_header" 
     android:textColor="#ffffffff" 
     android:textSize="15sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/search_gradient" > 

    <ImageView 
     android:id="@+id/searchBoxIcon" 
     android:layout_width="38dp" 
     android:layout_height="38dp" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/action_search" > 
    </ImageView> 

    <EditText 
     android:id="@+id/searchBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="@+id/searchBoxIcon" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="4dp" 
     android:layout_toRightOf="@+id/searchBoxIcon" 
     android:background="@drawable/search_box" 
     android:hint="@string/search_hint" 
     android:inputType="text" 
     android:maxLines="1" 
     android:minHeight="30sp" 
     android:paddingBottom="2dp" 
     android:paddingLeft="25sp" 
     android:paddingTop="2dp" 
     android:textColor="#ff000000" /> 
</RelativeLayout> 

<ListView 
    android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@color/divider_gray" 
    android:cacheColorHint="#00000000" 
    android:divider="@color/divider_gray" 
    android:dividerHeight="1dp" 
    android:footerDividersEnabled="false" 
    android:headerDividersEnabled="false" /> 

<TextView 
    android:id="@+id/android:empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:text="@string/loading" 
    android:textColor="@color/loading_gray" 
    android:textSize="20sp" /> 

</LinearLayout> 

不使用XML

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

<!-- dummy item to prevent edittext from gaining focus on activity start --> 

<LinearLayout 
    android:layout_width="0px" 
    android:layout_height="0px" 
    android:focusable="true" 
    android:focusableInTouchMode="true" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/title_background" > 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/ic_logo" > 
    </ImageView> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/logo" 
     android:gravity="center" 
     android:paddingBottom="5dp" 
     android:paddingLeft="50dp" 
     android:paddingRight="60dp" 
     android:paddingTop="5dp" 
     android:text="@string/share_header" 
     android:textColor="#ffffffff" 
     android:textSize="15sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

<ListView 
    android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@color/divider_gray" 
    android:cacheColorHint="#00000000" 
    android:divider="@color/divider_gray" 
    android:dividerHeight="1dp" 
    android:footerDividersEnabled="false" 
    android:headerDividersEnabled="false" /> 

<TextView 
    android:id="@+id/android:empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:text="@string/loading" 
    android:textColor="@color/loading_gray" 
    android:textSize="20sp" /> 

</LinearLayout> 

這裏更多的是如何破碎的工作,以防萬一你想要更多的代碼。

不工作行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="?android:attr/listPreferredItemHeight" 
android:layout_weight="1" 
android:baselineAligned="false" 
android:orientation="vertical" 
android:padding="6dp" 
android:background="#ffffffff" 
android:layout_margin="10dp" > 

<TextView 
    android:id="@+id/toptext" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_toLeftOf="@id/moreInfo" 
    android:gravity="center_vertical" 
    android:minHeight="20sp" 
    android:textColor="#ff000000" 
    android:textStyle="bold" /> 

</RelativeLayout> 

不工作查看適配器

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder = null; 
    if (convertView == null) 
    { 
     holder = new ViewHolder(); 

     LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = vi.inflate(R.layout.share_row, null); 
     convertView.setTag(holder); 
    }// if 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    v_SitePeople i = items.get(position); 

    if (i != null) 
    { 
     TextView topText = (TextView) convertView.findViewById(R.id.toptext); 
     topText.setGravity(Gravity.CENTER_VERTICAL); 
     topText.setMinHeight(40); 
     if (topText != null) 
     { 
      if (i.SitePerson != null) 
      { 
       if (i.PersonTitle != null) 
       { 
        topText.setText(String.format(i.SitePerson + ", " + i.PersonTitle)); 
       } 
       else 
       { 
        topText.setText(i.SitePerson); 
       } 
      }// if has ProtocolNumber 
      else 
      { 
       if (i.Nickname != null) 
       { 
        topText.setText(i.Nickname); 
       } 
      }// if does not have ProtocolNumber 
     }// if 
    }// if 
    return convertView; 
}// getView 

非常感謝你的幫助。

根據要求提供更多代碼。

工作setListAdapter:

public void updateDisplay(ArrayList<v_ProjectInvestigatorSiteContact> vProjectInvestigatorSiteContactList) 
{ 
    if (fullAdapter != null) 
    { 
     if (fullAdapter.isEmpty()) 
     { 
      fullAdapter = new ProjectAdapter(this, R.layout.row, vProjectInvestigatorSiteContactList); 
      fullAdapter = reOrder(fullAdapter); 
      setListAdapter(fullAdapter); 
     } 
     else 
     { 
      filteredAdapter = new ProjectAdapter(this, R.layout.row, vProjectInvestigatorSiteContactList); 
      filteredAdapter = reOrder(filteredAdapter); 
      setListAdapter(filteredAdapter); 
     } 
    } 
    else 
    { 
     fullAdapter = new ProjectAdapter(this, R.layout.row, vProjectInvestigatorSiteContactList); 
     fullAdapter = reOrder(fullAdapter); 
     setListAdapter(fullAdapter); 
    } 
}// updateDisplay 

工作列表設置:

private class DownloadProjectsTask extends AsyncTask<String, Void, ArrayList<v_ProjectInvestigatorSiteContact>> { 
    @Override 
    protected ArrayList<v_ProjectInvestigatorSiteContact> doInBackground(String... URLs) 
    { 
     return ProjectsHelper.parseProjects(URLs, CurrentStudies.this); 
    }// doInBackground 

    @Override 
    protected void onPostExecute(ArrayList<v_ProjectInvestigatorSiteContact> vProjectInvestigatorSiteContactList) 
    { 
     updateDisplay(vProjectInvestigatorSiteContactList); 
    }// onPostExecute 
}// DownloadProjectsTask 

工作適配器:

公共類ProjectAdapter擴展ArrayAdapter { 私人ArrayList的物品; 私人資源資源;

public ProjectAdapter(Context context, int textViewResourceId, ArrayList<v_ProjectInvestigatorSiteContact> items) 
{ 
    super(context, textViewResourceId, items); 
    this.items = items; 
    this.res = context.getResources(); 
}// ProjectAdapater 

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

@Override 
public v_ProjectInvestigatorSiteContact getItem(int position) 
{ 
    return items.get(position); 
} 

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

@Override 
public int getViewTypeCount() 
{ 
    return 2; 
} 

@Override 
public int getItemViewType(int position) 
{ 
    if (items.get(position).ProjectID == null) 
    { 
     return 0; 
    } 
    else 
    { 
     return 1; 
    } 
} 

@Override 
public boolean isEnabled(int position) 
{ 
    if (getItemViewType(position) == 0) 
    { 
     return false; 
    } 
    return true; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder = null; 
    int type = getItemViewType(position); 
    if (convertView == null) 
    { 
     holder = new ViewHolder(); 

     LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (type == 0) 
     { 
      convertView = vi.inflate(R.layout.row_header, null); 
     } 
     else 
     { 
      convertView = vi.inflate(R.layout.row, null); 
     } 
     convertView.setTag(holder); 
    }// if 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    v_ProjectInvestigatorSiteContact i = items.get(position); 

    if (i != null) 
    { 
     TextView topText = (TextView) convertView.findViewById(R.id.toptext); 
     TextView bottomText = (TextView) convertView.findViewById(R.id.bottomtext); 
     if (topText != null) 
     { 
      if (i.ProtocolNumber != null) 
      { 
       if (i.WebIndication != null) 
       { 
        topText.setText(i.ProtocolNumber); 
        bottomText.setText(i.WebIndication); 
       } 
       else if (i.ProjectName != null) 
       { 
        topText.setText(i.ProtocolNumber); 
        bottomText.setText(i.ProjectName); 
       } 
      }// if has ProtocolNumber 
      else 
      { 
       if (i.WebIndication != null) 
       { 
        topText.setText(i.WebIndication); 
        topText.setMinHeight(40); 
        topText.setGravity(Gravity.CENTER_VERTICAL); 
       } 
       else if (i.ProjectName != null) 
       { 
        topText.setText(i.ProjectName); 
        topText.setMinHeight(40); 
        topText.setGravity(Gravity.CENTER_VERTICAL); 
       } 
       else 
       { 
        topText.setText(i.SiteID); 
        topText.setMinHeight(40); 
        topText.setGravity(Gravity.CENTER_VERTICAL); 
       } 
      }// if does not have ProtocolNumber 
     }// if 

     // Coming Soon logic 
     if (i.ProjectStatusID != null) 
     { 
      if (i.ProjectStatusID.equals(res.getString(R.string.feasibility_id))) 
      { 
       topText.setTextColor(res.getColor(R.color.coming_soon_gray)); 
       bottomText.setText("(Coming Soon)"); 
      } 
      else { 
       topText.setTextColor(Color.BLACK); 
      } 
     }//if 

     if (bottomText != null) 
     { 
      if (type == 0) 
      { 
       if (position == getCount() - 1 || getItemViewType(position + 1) == 0) 
       { 
        bottomText.setVisibility(View.VISIBLE); 
        bottomText.setText(res.getString(R.string.no_studies)); 
        bottomText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 0, (float) 1.0)); 
       } 
       else 
       { 
        bottomText.setVisibility(View.GONE); 
       } 
      }// header followed by another header, or end of list 
     }// if 

     //recenters row headers 
     if (type == 0) 
     { 
      topText.setGravity(Gravity.CENTER); 
     } 
    }// if 
    return convertView; 
}// getView 

public static class ViewHolder { 
    public TextView textView; 
} 

} // ProjectAdapter

不工作setListAdapter:

public void updateDisplay(ArrayList<v_SitePeople> vSitePeopleShareList) 
{ 
     adapter = new ShareAdapter(this, R.layout.share_row, vSitePeopleShareList); 
     setListAdapter(adapter); 
}// updateDisplay 

不工作列表設置:

private class DownloadShareTask extends AsyncTask<String, Void, ArrayList<v_SitePeople>> { 
    @Override 
    protected ArrayList<v_SitePeople> doInBackground(String... params) 
    { 
     return ProjectsHelper.getSharePeople(params[0], Share.this); 
    }// doInBackground 

    @Override 
    protected void onPostExecute(ArrayList<v_SitePeople> vSitePeopleShareList) 
    { 
     updateDisplay(vSitePeopleShareList); 
    }// onPostExecute 
}// DownloadProjectsTask 
+1

最重要的是您的活動都是從ListActivity擴展的,因爲一旦您在xml中提供了默認的android:id/list屬性,系統就會期望這些屬性。是這樣嗎?另外,在設置每個列表的地方共享代碼並設置適配器。 –

+0

是的。它們都擴展了ListActivity。我將使用您要求的代碼編輯OP。 – erosebe

+0

我會嘗試從行xml中的relativeLayout中移除重量。你不能在relativeLayout中使用體重。 – koso

回答

-2

我想通了。我沒有發佈相關的代碼,或者我相信你們會爲我發現它。

我從一段代碼中複製了一個非工作適配器,如果viewType爲0,則返回false。並且由於我的第二個列表只有一種類型,所以一切都被禁用。

謝謝大家的努力。我很抱歉,我沒有給你正確的代碼。

+0

我希望你已經學會了複製粘貼編程的陷阱。當你有這樣的重複時,通常最好把它重構成可重用的類和/或方法。 –