2014-10-30 8 views
8

कस्टम सूची व्यू बनाने के लिए मैंने कुछ बदलावों के साथ एक अच्छा ट्यूटोरियल का पालन किया जो मुझे मेरी सूची दृश्य में प्रत्येक पंक्ति के लिए एकाधिक छवियां प्रदर्शित करने की अनुमति देगा। मैं सूची में कई आइटम चुनकर हाइलाइट करना चाहता हूं और फिर मेरे विकल्प मेनू पर एक विकल्प चुनकर उन सभी सूची आइटमों में कुछ प्रकार की कार्रवाई करता हूं। हालांकि, मुझे लगता है कि समस्या यह है कि मैं कई आइटमों का चयन नहीं कर सकता, भले ही मैंने .xml फ़ाइल में ListView में android:choiceMode="multipleChoice" जोड़ा है। मुझे एहसास है कि यह चेक बॉक्स या रेडियो बटन का उपयोग करके किया जा सकता है, लेकिन मैं इससे बचना पसंद करूंगा। मैंने नीचे अपना स्रोत कोड संलग्न किया है। किसी भी सहायता की सराहना की जाएगी। अंत में, एक महान ट्यूटोरियल के लिए http://custom-android-dn.blogspot.com/ पर धन्यवाद। धन्यवाद।कस्टम एडाप्टर के साथ सूचीदृश्य में एकाधिक आइटमों को चुनना/हाइलाइट करना - एंड्रॉइड

CustomlistviewActivity.java

package DucNguyen.example.customlistview; 

public class CustomlistviewActivity extends Activity { 

    private ListView listViewFootballLegend; 
    private Context ctx; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_customlistview);   
     ctx=this; 
     List<FootballLegend> legendList= new ArrayList<FootballLegend>(); 
     legendList.add(new FootballLegend("Pele","October 23, 1940 (age 72)","pele","brazil")); 
     legendList.add(new FootballLegend("Diego Maradona","October 30, 1960 (age 52)","maradona","argentina")); 
     legendList.add(new FootballLegend("Johan Cruyff","April 25, 1947 (age 65)","cruyff","netherlands")); 
     legendList.add(new FootballLegend("Franz Beckenbauer","September 11, 1945 (age 67)","beckenbauer","germany")); 
     legendList.add(new FootballLegend("Michel Platini","June 21, 1955 (age 57)","platini","france")); 
     legendList.add(new FootballLegend("Ronaldo De Lima","September 22, 1976 (age 36)","ronaldo","brazil")); 

     listViewFootballLegend = (ListView) findViewById(R.id.FootballLegend_list); 
     listViewFootballLegend.setAdapter(new FootballLegendListAdapter(ctx, R.layout.legend_row_item, legendList)); 

     // Click event for single list row 
     listViewFootballLegend.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       FootballLegend o = (FootballLegend) parent.getItemAtPosition(position); 
       Toast.makeText(CustomlistviewActivity.this, o.getName().toString(), Toast.LENGTH_SHORT).show(); 
      } 
     });  
    } 
} 

FootballLegendListAdapter.java

package DucNguyen.example.customlistview; 

public class FootballLegendListAdapter extends ArrayAdapter<FootballLegend> { 
    private int    resource; 
    private LayoutInflater inflater; 
    private Context   context; 
    public FootballLegendListAdapter (Context ctx, int resourceId, List<FootballLegend> objects) { 
     super(ctx, resourceId, objects); 
     resource = resourceId; 
     inflater = LayoutInflater.from(ctx); 
     context=ctx; 
    } 
    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 
     convertView = (RelativeLayout) inflater.inflate(resource, null); 
     FootballLegend Legend = getItem(position); 
       TextView legendName = (TextView) convertView.findViewById(R.id.legendName); 
     legendName.setText(Legend.getName()); 

     TextView legendBorn = (TextView) convertView.findViewById(R.id.legendBorn); 
     legendBorn.setText(Legend.getNick()); 

     ImageView legendImage = (ImageView) convertView.findViewById(R.id.legendImage); 
     String uri = "drawable/" + Legend.getImage(); 
     int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     Drawable image = context.getResources().getDrawable(imageResource); 
     legendImage.setImageDrawable(image); 

     ImageView NationImage = (ImageView) convertView.findViewById(R.id.Nation); 
     uri = "drawable/" + Legend.getNation(); 
     imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); 
     image = context.getResources().getDrawable(imageResource); 
     NationImage.setImageDrawable(image); 

     return convertView; 
    } 
} 

FootballLegend.java

package DucNguyen.example.customlistview; 
public class FootballLegend { 
    public FootballLegend(String name, String born, String image, String nation) { 
     super(); 
     this.name = name; 
     this.born = born; 
     this.image = image; 
     this.nation = nation; 
    } 
    private String name; 
    private String born; 
    private String image; 
    private String nation; 

    public String getName() { 
     return name; 
    } 
    public void setName(String nameText) { 
     name = nameText; 
    } 
    public String getNick() { 
     return born; 
    } 
    public void setNick(String born) { 
     this.born = born; 
    } 
    public String getImage() { 
     return image; 
    } 
    public void setImage(String image) { 
     this.image = image; 
    } 
     public String getNation() { 
     return nation; 
    } 
    public void setNation(String image) { 
     this.image = nation; 
    } 
} 

legend_row_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <!-- ListRow Left side Thumbnail image --> 
    <LinearLayout android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dip" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="5dip"> 
     <ImageView 
      android:id="@+id/legendImage" 
      android:layout_width="50dip" 
      android:layout_height="50dip" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/legendName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:textColor="#040404" 
     android:typeface="sans" 
     android:textSize="15dip" 
     android:textStyle="bold"/> 

    <TextView 
     android:id="@+id/legendBorn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/legendName" 
     android:textColor="#343434" 
     android:textSize="10dip" 
     android:layout_marginTop="1dip" 
     android:layout_toRightOf="@+id/thumbnail" /> 

    <ImageView 
     android:id="@+id/Nation" 
     android:layout_width="45dp" 
     android:layout_height="30dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="5dp" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

activity_customlistview.xml

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

    <ListView 
     android:id="@+id/FootballLegend_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingTop="5dp" 
     android:choiceMode="multipleChoice" > 
    </ListView> 

</LinearLayout> 

उत्तर

8
  1. जड़ प्रत्येक आइटम आप सूची में प्रदर्शित करेगा Checkable को लागू करना चाहिए की View। इस इंटरफेस को कार्यान्वित करते समय भी देखने योग्य राज्य को अपडेट करें। इसे करने के लिए this answer देखें।

  2. <selector> इस रूट View के लिए पृष्ठभूमि के रूप में खींचने योग्य सेट करें। चेक किए गए राज्य और सामान्य राज्यों के लिए कौन से अलग रंग/drawables।

  3. ListView की पसंद मोड को एकल विकल्प या बहु-विकल्प में सेट करें।

  4. सूची एडाप्टर में, उपरोक्त बनाए गए दृश्य के साथ मूल दृश्य लेआउट के रूप में आइटम दृश्य प्रदान करें।

अब, ListView अपने आइटम विचारों पर सेटिंग को चेक/अनियंत्रित राज्य का ख्याल रखना होगा। वर्तमान में चयनित आइटम प्राप्त करने के लिए ListView पर getCheckedItemIds() या getCheckedItemPositions() पर भी कॉल कर सकते हैं।

+0

यदि मैं अपनी सूची के लिए चेक करने योग्य लागू करता हूं, तो सूची में चेकबॉक्स नहीं होंगे? यही वह है जिसे मैं टालने की कोशिश कर रहा हूं। धन्यवाद – Willis

+1

@ विलिस नंबर 'चेक करने योग्य' जावा इंटरफेस है। जो कुछ भी लागू करता है, वह 'सेट चेक (बूलियन)' और 'टॉगल()' विधियों को रखने का वादा करता है। यह पूरी तरह से आपके ऊपर है जो आप दृश्य में दिखाना चाहते हैं। –

+0

धन्यवाद। मेरा मानना ​​है कि मुझे यही चाहिए। – Willis

संबंधित मुद्दे