2016-07-28 11 views
0

Ich benutze das ListView Widget auf Android, und in der Vorschau-Liste Inhalt wählte ich "Checkliste" Element Grundsätzlich ist es eine Liste von Elementen und ich sollte in der Lage check einige Elemente und wenn ich das Häkchen neben dem Element wird sichtbar (es ist kein Kontrollkästchen, das ist der Unterschied zwischen vielen anderen überprüfbaren Listen) Ich weiß nicht, wie man es verwendet, würde ich gerne wissen zumindest, wie ich einige Artikel überprüfen kann, ist, dass das Häkchen sichtbar machen, denn wenn ich auf einen Eintrag klicken, wird es anklickbar ist aber nichts passiert ...wie man eine überprüfte Liste Artikel Listenansicht auf Android

image of listview in simulator

image of listview in editor

hier ist mein xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:rsb="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" 
android:background="#fffefdff"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="540dp" 
    android:weightSum="1" 
    android:id="@+id/linearLayoutPreferences" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:focusableInTouchMode="false" 
    android:divider="#ff080808" 
    android:dividerPadding="@dimen/activity_horizontal_margin" 
    android:showDividers="middle|beginning|end"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     tools:listitem="@android:layout/simple_list_item_checked" 
     android:clickable="true" 
     android:fastScrollAlwaysVisible="false" 
     android:choiceMode="multipleChoice" 
     android:contextClickable="false" /> 
</LinearLayout> 

hier ist meine Java-Datei

public class Popneighbourhood extends AppCompatActivity { 

ListView listNeighbourhood; 
String[] neighbourhood = new String[]{ 
     "Alamo Square/NOPA", "Castro/Upper Market", "Central Richmond", "Cole Valley/Ashbury Heights", "Downtown/Civic/Van Ness", "Duboce Triangle", 
     "Financial District", "Glen Park", "Haight Ashbury", "Hayes Vallez", "Ingleside/SFSU/CCSF", "Inner Richmond", 
     "Inner Sunset/UCSF", "Jordan Park/Laurel Heights", "Laurel Heights/Presidio", "Lower Haight", "Lower Nob Hill", "Lower Pac Heights", 
     "Marina/Cow Hollow", "Mission Bay", "Mission District", "Nob Hill", "Noe Valley", "North Beach/Telegraph Hill", 
     "Oakland North/Temescal", "Pacific Heights" 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_popneighbourhood); 
    ActionBar actionBar=getSupportActionBar(); 
    actionBar.setDisplayShowHomeEnabled(true); 
    actionBar.setIcon(R.mipmap.logofrontdoor); 

    listNeighbourhood = (ListView) findViewById(R.id.listView); 

    //android.R.layout.simple_list_item_1 est une vue disponible de base dans le SDK android, 
    //Contenant une TextView avec comme identifiant "@android:id/text1" 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Popneighbourhood.this, 
      android.R.layout.simple_list_item_1, neighbourhood); 
    listNeighbourhood.setAdapter(adapter); 

Antwort

0

// Adapter Code, ich habe gerade einen der Adapter Quelle von meinem Projekt, beachten Sie die Imageview, iv_item_fragment_dashboard_country_list_select, wird bei der Auswahl umgeschaltet.

public class DashboardCountryListAdapter extends BaseAdapter { 

    private Context mContext; 
    private List<Country> mCountryList = new ArrayList<>(); 

    public DashboardCountryListAdapter(Context context, List<Country> countryList) { 
     mContext = context; 
     mCountryList = countryList; 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return mCountryList.isEmpty() ? null : mCountryList.get(position); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_dashboard_country_list, parent, false); 
      viewHolder = new ViewHolder(convertView); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     if (!mCountryList.isEmpty()) { 
      Country country = mCountryList.get(position); 
      int image = country.getImage(); 
      if (image != -1) { 
       viewHolder.iv_item_fragment_dashboard_country_list.setImageResource(image); 
       if (country.getSelected()){ 
        viewHolder.iv_item_fragment_dashboard_country_list_select.setVisibility(View.VISIBLE); 
       } 
       else 
        viewHolder.iv_item_fragment_dashboard_country_list_select.setVisibility(View.GONE); 
      } 

      viewHolder.tv_item_fragment_dashboard_country_list.setText(country.getName()); 
     } 

     return convertView; 
    } 

    class ViewHolder { 
     @Bind(R.id.iv_item_fragment_dashboard_country_list) 
     ImageView iv_item_fragment_dashboard_country_list; 
     @Bind(R.id.tv_item_fragment_dashboard_country_list) 
     TextView tv_item_fragment_dashboard_country_list; 
     @Bind(R.id.iv_item_fragment_dashboard_country_list_select) 
     ImageView iv_item_fragment_dashboard_country_list_select; 

     public ViewHolder(View view) { 
      ButterKnife.bind(this, view); 
     } 
    } 

    public void updateList(List<Country> list) { 
     mCountryList = list; 
     notifyDataSetChanged(); 
    } 
} 

// xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="50dp"> 

    <ImageView 
     android:id="@+id/iv_item_fragment_dashboard_country_list" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:adjustViewBounds="true" 
     android:layout_width="20dp" 
     android:layout_height="15dp" /> 

    <com.UTU.View.UtuTextView 
     android:id="@+id/tv_item_fragment_dashboard_country_list" 
     android:textColor="@color/chic_black" 
     android:textSize="18sp" 
     android:gravity="center_vertical" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 

    <ImageView 
     android:id="@+id/iv_item_fragment_dashboard_country_list_select" 
     android:src="@drawable/icon_check_teal" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:adjustViewBounds="true" 
     android:layout_width="13dp" 
     android:layout_height="10dp" /> 
</LinearLayout> 
0

Ich kann Ihnen sagen, warum der Editor aus der laufenden App sieht anders aus:

In Ihrem Layout XML die Listenansicht hat dieses Attribut:

tools:listitem="@android:layout/simple_list_item_checked" 

aber in Ihrem Code haben Sie

ArrayAdapter<String> adapter = new ArrayAdapter<String>(Popneighbourhood.this, 
      android.R.layout.simple_list_item_1, neighbourhood); 

Sie verwenden also ein völlig anderes Layout für das Listenelement.

Sie müssen R.layout.simple_list_item_checked in den Adapterkonstruktor setzen, und Sie müssen wahrscheinlich zu dem Konstruktor wechseln, der die ID des zu verwendenden TextView angibt.

+0

danke, ich habe das Attribut geändert, auf dem Simulator kann ich jetzt die verschiedenen Elemente auswählen, wie kann ich mit diesen Elementen arbeiten, würde ich gerne wissen, wie man die Anzahl der ausgewählten Elemente ... die bekommen Die Funktion getCheckedItemCount gibt nichts zurück, es scheint, als wären die Häkchen nur für das Bild vorhanden, aber sie werden nicht erkannt ... Ich bin ziemlich neu und habe eine schwere Zeit mit dieser Listenansicht. – Arthur

+0

I ' m gut, es ist in Ordnung :) Danke für Ihre Hilfe – Arthur

Verwandte Themen