getQuantityString() method not working - android

this is my strings.xml:
<resources>
<plurals name="subtitle_plural">
<item quantity="one">%1$d crime</item>
<item quantity="other">%1$d crimes</item>
</plurals>
</resources>
and this is my code using plural:
CrimeLab crimeLab = CrimeLab.get(getActivity());
int crimeSize = crimeLab.getCrimes().size();
String subTitle = getResources().getQuantityString(R.plurals.subtitle_plural, crimeSize, crimeSize);
when crimeSize is 1, variable subTitle should be 1 crime, but it always be 1 crimes。
anyone help me, why getQuantityString() not working at here?

Try to change you plurals into below
<plurals name="subtitle_plural">
<item quantity="one">%d crime </item>
<item quantity="other">%d crimes </item>
</plurals>

Related

Android CardView not displaying some TextView

As u can see there is some text missing in some CardView
I am trying to simply make TextView appear inside a CardView.All I need to do is make texts appear inside the CardView.The CardView appear, but some CardView do not show any text inside them.
item_names.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp"
android:elevation="5dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/numberCount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="#+id/tvName"
android:layout_below="#+id/numberCount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="#+id/arabicName"
android:layout_below="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="#+id/meaning"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/arabicName"
android:gravity="center"/>
</RelativeLayout>
activity_allah_names.xml
<RelativeLayout 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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context="com.islamiclearn.namesofallah.activity.AllahNamesActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/ad_view1"
android:layout_alignParentTop="true"
android:id="#+id/rcv"/>
<com.google.android.gms.ads.AdView
android:id="#+id/ad_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:layout_alignParentBottom="true"/>
AllahNamesActivity.java
public class AllahNamesActivity extends AppCompatActivity {
private List<Names> list;
private RecyclerView mRecyclerView;
private AdView mAdView1;
/*
Intent communicate b/w Android components like Activity,
Service, Broadcast
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_allah_names);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView1 = (AdView) findViewById(R.id.ad_view1);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Start loading the ad in the background.
mAdView1.loadAd(adRequest);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mRecyclerView = (RecyclerView) findViewById(R.id.rcv);
getNames();
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(AllahNamesActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("Position", position);
bundle.putParcelableArrayList("LIST", (ArrayList<Names>) list);
intent.putExtras(bundle);
startActivity(intent);
}
}));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
private void getNames() {
new AsyncTask<Void, Void, List<Names>>() {
#Override
protected List<Names> doInBackground(Void... params) {
return fillArray();
}
#Override
protected void onPostExecute(List<Names> names) {
super.onPostExecute(names);
if (names != null && names.size() > 0) {
list = names;
mRecyclerView.setLayoutManager(new GridLayoutManager(AllahNamesActivity.this, 2));
mRecyclerView.setAdapter(new NamesAdapter(list));
}
}
}.execute();
}
private List<Names> fillArray() {
String[] arraCount = getResources().getStringArray(item_count);
String[] arra = getResources().getStringArray(names);
String[] arraArabic = getResources().getStringArray(R.array.names_arabic);
String[] arraMeaning = getResources().getStringArray(R.array.meaning);
String[] music = getResources().getStringArray(R.array.music);
List<Names> namesList = new ArrayList<>();
for (int i = 0; i < arra.length; i++) {
Names names = new Names();
names.setCount(arraCount[i]);
names.setName(arra[i]);
names.setArabicName(arraArabic[i]);
names.setMeaning(arraMeaning[i]);
int resID = getId(music[i]);
names.setResId(resID);
namesList.add(names);
}
Items items = new Items();
List<String> list = new ArrayList<>();
items.setArabicList(list);
return namesList;
}
public int getId(String resName) {
return getResources().getIdentifier(resName,
"raw", getPackageName());
}
}
arrays.xml
<resources>
<string-array name="item_count">
<item>"1"</item>
<item>"2"</item>
<item>"3"</item>
<item>"4"</item>
<item>"5"</item>
<item>"6"</item>
<item>"7"</item>
<item>"8"</item>
<item>"9"</item>
<item>"10"</item>
<item>"11"</item>
<item>"12"</item>
<item>"13"</item>
<item>"14"</item>
<item>"15"</item>
<item>"16"</item>
<item>"17"</item>
<item>"18"</item>
<item>"19"</item>
<item>"20"</item>
<item>"21"</item>
<item>"22"</item>
<item>"23"</item>
<item>"24"</item>
<item>"25"</item>
<item>"26"</item>
<item>"27"</item>
<item>"28"</item>
<item>"29"</item>
<item>"30"</item>
<item>"31"</item>
<item>"32"</item>
<item>"33"</item>
<item>"34"</item>
<item>"35"</item>
<item>"36"</item>
<item>"37"</item>
<item>"38"</item>
<item>"39"</item>
<item>"40"</item>
<item>"41"</item>
<item>"42"</item>
<item>"43"</item>
<item>"44"</item>
<item>"45"</item>
<item>"46"</item>
<item>"47"</item>
<item>"48"</item>
<item>"49"</item>
<item>"50"</item>
<item>"51"</item>
<item>"52"</item>
<item>"53"</item>
<item>"54"</item>
<item>"55"</item>
<item>"56"</item>
<item>"57"</item>
<item>"58"</item>
<item>"59"</item>
<item>"60"</item>
<item>"61"</item>
<item>"62"</item>
<item>"63"</item>
<item>"64"</item>
<item>"65"</item>
<item>"66"</item>
<item>"67"</item>
<item>"68"</item>
<item>"69"</item>
<item>"70"</item>
<item>"71"</item>
<item>"72"</item>
<item>"73"</item>
<item>"74"</item>
<item>"75"</item>
<item>"76"</item>
<item>"77"</item>
<item>"78"</item>
<item>"79"</item>
<item>"80"</item>
<item>"81"</item>
<item>"82"</item>
<item>"83"</item>
<item>"84"</item>
<item>"85"</item>
<item>"86"</item>
<item>"87"</item>
<item>"88"</item>
<item>"89"</item>
<item>"90"</item>
<item>"91"</item>
<item>"92"</item>
<item>"93"</item>
<item>"94"</item>
<item>"95"</item>
<item>"96"</item>
<item>"97"</item>
<item>"98"</item>
<item>"99"</item>
</string-array>
<string-array name="names">
<item>Ar-Rahman</item>
<item>\'Ar-Rahim</item>
<item>Al-Malik</item>
<item> Al-Quddus</item>
<item>As-Salam</item>
<item>Al-Mu\'min</item>
<item>Al-Muhaymin</item>
<item>Al-Aziz</item>
<item>Al-Jabbar</item>
<item>Al-Mutakabbir</item>
<item>Al-Khaliq</item>
<item> Al-Bari\'</item>
<item>Al-Musawwir</item>
<item>Al-Ghaffar</item>
<item>Al-Qahhar </item>
<item>Al-Wahhab</item>
<item>Ar-Razzaq</item>
<item>Al-Fattah</item>
<item>Al-\'Alim</item>
<item>Al-Qabid</item>
<item>Al-Basit</item>
<item>Al-Khafid</item>
<item>Ar-Rafi</item>
<item>Al-Mu\'izz</item>
<item>Al-Mudhill</item>
<item>As-Sami</item>
<item>Al-Basir</item>
<item>Al-Hakam</item>
<item>Al-\'Adl</item>
<item>Al-Latif</item>
<item>Al-Khabir</item>
<item>Al-Halim</item>
<item>Al-Azim</item>
<item>Al-Ghafur</item>
<item>Ash-Shakur</item>
<item>Al-Ali</item>
<item>Al-Kabir</item>
<item>Al-Hafiz</item>
<item>Al-Muqit</item>
<item>Al-Hasib</item>
<item>Al-Jalil</item>
<item>Al-Karim</item>
<item>Ar-Raqib</item>
<item>Al-Mujib</item>
<item>Al-Wasi</item>
<item>Al-Hakim</item>
<item>Al-Wadud</item>
<item>Al-Majid</item>
<item>Al-Ba\'ith</item>
<item>Ash-Shahid</item>
<item>Al-Haqq</item>
<item>Al-Wakil</item>
<item>Al-Qawiyy</item>
<item>Al-Matin</item>
<item>Al-Waliyy</item>
<item>Al-Hamid</item>
<item>Al-Muhsi</item>
<item>Al-Mubdi\'</item>
<item>Al-Mu\'id</item>
<item>Al-Muhyi</item>
<item>Al-Mumit</item>
<item>Al-Hayy</item>
<item>Al-Qayyum</item>
<item>Al-Wajid</item>
<item>Al-Majid</item>
<item>Al-Wahid</item>
<item>Al-Ahad</item>
<item>As-Samad</item>
<item>Al-Qadir</item>
<item>Al-Muqtadir</item>
<item>Al-Muqaddim</item>
<item>Al-Mu\'akhkhir</item>
<item>Al-Awwal</item>
<item>Al-Akhir</item>
<item>Az-Zahir</item>
<item>Al-Batin</item>
<item>Al-Wali</item>
<item>Al-Muta\'ali</item>
<item>Al-Barr</item>
<item>At-Tawwab</item>
<item>Al-Muntaqim</item>
<item>Al-\'Afuww</item>
<item>Ar-Ra\'uf</item>
<item>Malik-al-Mulk</item>
<item>Dhu-al-Jalal \n wa-al-Ikram</item>
<item>Al-Muqsit</item>
<item>Al-Jami\'</item>
<item>Al-Ghani</item>
<item>Al-Mughni</item>
<item>Al-Mani\'</item>
<item>Ad-Darr</item>
<item>An-Nafi\'</item>
<item>An-Nur</item>
<item>Al-Hadi</item>
<item>Al-Badi</item>
<item>Al-Baqi</item>
<item>Al-Warith</item>
<item>Ar-Rashid</item>
<item>As-Sabur</item>
</string-array>
<string-array name="names_arabic">
<item>الرحمن</item>
<item >الرحيم</item>
<item> الملك</item>
<item> القدوس</item>
<item>السلام</item>
<item>المؤمن</item>
<item >المهيمن</item>
<item >العزيز</item>
<item>الجبار</item>
<item >المتكبر</item>
<item >الخالق</item>
<item >البارئ</item>
<item >المصور</item>
<item >الغفار</item>
<item >القهار</item>
<item >الوهاب</item>
<item >الرزاق</item>
<item >الفتاح</item>
<item >العليم</item>
<item >القابض</item>
<item >الباسط</item>
<item >الخافض</item>
<item >الرافع</item>
<item >المعز</item>
<item >المذل</item>
<item >السميع</item>
<item >البصير</item>
<item >الحكم</item>
<item >العدل</item>
<item >اللطيف</item>
<item >الخبير</item>
<item >الحليم</item>
<item >العظيم</item>
<item >الغفور</item>
<item >الشكور</item>
<item >العلى</item>
<item >الكبير</item>
<item >الحفيظ</item>
<item >المقيت</item>
<item >الحسيب</item>
<item >الجليل</item>
<item >الكريم</item>
<item >الرقيب</item>
<item >المجيب</item>
<item >الواسع</item>
<item >الحكيم</item>
<item >الودود</item>
<item >المجيد</item>
<item >الباعث</item>
<item >الشهيد</item>
<item >الحق</item>
<item >الوكيل</item>
<item >القوى</item>
<item >المتين</item>
<item > الولى</item>
<item >الحميد</item>
<item >المحصى</item>
<item >المبدئ</item>
<item >المعيد</item>
<item >المحيى</item>
<item >المميت</item>
<item >الحي</item>
<item >القيوم</item>
<item >الواجد</item>
<item >الماجد</item>
<item >الواحد</item>
<item >الصمد</item>
<item >الاحد</item>
<item >القادر</item>
<item >المقتدر</item>
<item >المقدم</item>
<item >المؤخر</item>
<item >الأول</item>
<item >الأخر</item>
<item >الظاهر</item>
<item >الباطن</item>
<item >الوالي</item>
<item >المتعالي</item>
<item >البر</item>
<item >التواب</item>
<item >المنتقم</item>
<item >العفو</item>
<item >الرؤوف</item>
<item >مالك الملك</item>
<item >ذو الجلال و الإكرام</item>
<item >المقسط</item>
<item >الجامع</item>
<item >الغنى</item>
<item >المغنى</item>
<item > المانع</item>
<item > الضار</item>
<item >النافع</item>
<item > النور</item>
<item >الهادي</item>
<item >البديع</item>
<item >الباقي</item>
<item >الوارث</item>
<item > الرشيد</item>
<item > الصبور</item>
</string-array>
<string-array name="meaning">
<item >The All-Compassionate</item>
<item >The All-Merciful</item>
<item >The Absolute Ruler</item>
<item >The Pure One</item>
<item >The Source of Peace</item>
<item >The Inspirer of Faith</item>
<item >The Guardian</item>
<item >The Victorious</item>
<item >The Compeller</item>
<item >The Greatest</item>
<item >The Creator</item>
<item >The Maker of Order</item>
<item >The Shaper of Beauty</item>
<item >The Forgiving</item>
<item >The Subduer</item>
<item >The Giver of All</item>
<item >The Sustainer</item>
<item >The Opener</item>
<item >The Knower of All</item>
<item >The Constrictor</item>
<item >The Reliever</item>
<item >The Abaser</item>
<item >The Exalter</item>
<item >The Bestower of Honors</item>
<item >The Humiliator</item>
<item >The Hearer of All</item>
<item >The Seer of All</item>
<item >The Judge</item>
<item >The Just</item>
<item >The Subtle One</item>
<item >The All-Aware</item>
<item >The Forbearing</item>
<item >The Magnificent</item>
<item >The Forgiver and Hider of Faults</item>
<item >The Rewarder of Thankfulness</item>
<item >The Highest</item>
<item >The Greatest</item>
<item >The Preserver</item>
<item >The Nourisher</item>
<item >The Accounter</item>
<item >The Mighty</item>
<item >The Generous</item>
<item >The Watchful One</item>
<item >The Responder to Prayer</item>
<item >The All-Comprehending</item>
<item >The Perfectly Wise</item>
<item >The Loving One</item>
<item >The Majestic One</item>
<item >The Resurrector</item>
<item >The Witness</item>
<item >The Truth</item>
<item >The Trustee</item>
<item >The Possessor of All Strength</item>
<item >The Forceful One</item>
<item >The Governor</item>
<item >The Praised One</item>
<item >The Appraiser</item>
<item >The Originator</item>
<item >The Restorer</item>
<item >The Giver of Life</item>
<item >The Taker of Life</item>
<item >The Ever Living One</item>
<item >The Self-Existing One</item>
<item >The Finder</item>
<item >The Glorious</item>
<item >The Unique, The Single</item>
<item >The One, The Indivisible</item>
<item >The Satisfier of All Needs</item>
<item >The All Powerful</item>
<item >The Creator of All Power</item>
<item >The Expediter</item>
<item >The Delayer</item>
<item >The First</item>
<item >The Last</item>
<item >The Manifest One</item>
<item >The Hidden One</item>
<item >The Protecting Friend</item>
<item >The Supreme One</item>
<item >The Doer of Good</item>
<item >The Guide to Repentance</item>
<item >The Avenger</item>
<item >The Forgiver</item>
<item >The Clement</item>
<item >The Owner of All</item>
<item >The Lord of Majesty and Bounty</item>
<item >The Equitable One</item>
<item >The Gatherer</item>
<item >The Rich One</item>
<item >The Enricher</item>
<item >The Preventer of Harm</item>
<item >The Creator of The Harmful</item>
<item >The Creator of Good</item>
<item >The Light</item>
<item >The Guide</item>
<item >The Originator</item>
<item >The Everlasting One</item>
<item >The Inheritor of All</item>
<item >The Righteous Teacher</item>
<item >The Patient One </item>
</string-array>
<string-array name="music">
<item> rahman</item>
<item> rahim </item>
<item> malik </item>
<item> quddus </item>
<item> salam</item>
<item> mumin </item>
<item> muhaimin</item>
<item> aziz</item>
<item> jabbar</item>
<item> mutakabbir</item>
<item> khaliq</item>
<item> bari</item>
<item> musawwir</item>
<item> ghaffar</item>
<item> qahhar</item>
<item> wahhab</item>
<item> razzaq</item>
<item> fattah</item>
<item> alim </item>
<item> qabid </item>
<item> basit</item>
<item> khafid </item>
<item> rafi </item>
<item> muizz </item>
<item> mudhill </item>
<item> sami</item>
<item> basir</item>
<item> hakam</item>
<item> adl </item>
<item> latif</item>
<item> khabir</item>
<item> halim</item>
<item> azim</item>
<item> ghafur</item>
<item>shakur</item>
<item> ali</item>
<item> kabir</item>
<item>hafiz</item>
<item> muqit</item>
<item> hasib</item>
<item> jalil</item>
<item> karim</item>
<item> raqib</item>
<item> mujib</item>
<item> wasi</item>
<item> hakim</item>
<item> wadud</item>
<item> majeed</item>
<item> baith</item>
<item> shahid</item>
<item> haqq</item>
<item> wakil</item>
<item> qawi</item>
<item> matin</item>
<item> wali</item>
<item> hamid</item>
<item> muhsi</item>
<item> mubdi</item>
<item> muid</item>
<item> muhyi</item>
<item> mumit</item>
<item> hayy</item>
<item> qayyum</item>
<item> wajid</item>
<item> majeed</item>
<item> wahid</item>
<item> a</item>
<item> samad</item>
<item> qadir</item>
<item> muqtadir</item>
<item> muqaddim</item>
<item> muakhkhir2a</item>
<item> awwal</item>
<item> akhir</item>
<item> zahir</item>
<item> batin</item>
<item> wali</item>
<item> muta_ali</item>
<item> barr</item>
<item> tawwab</item>
<item> muntaqim</item>
<item> afuw</item>
<item> rauf</item>
<item> malik_ul_mulk</item>
<item> dhu_l_jalali_wal_ikram</item>
<item> muqsit</item>
<item> jami</item>
<item> ghaniy</item>
<item> mughni</item>
<item> mani</item>
<item> darr</item>
<item> nafi</item>
<item> nur</item>
<item> hadi</item>
<item> badi</item>
<item> baqi</item>
<item> warith</item>
<item>rashid</item>
<item>sabur</item>
</string-array>
Probably the problem is with your arrays, you need to debug to check. You can post your arrays.

How to change the color of String array or String array elements

I want to change the color of each array element or whole array given bellow.so how
can i achieve this?
String[] title = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
}
is there any way that
title.setColor or
title[0].setColor
Which will change the color of String value directly.i found a lot but all answers were for string-array resource.
Try this,
Your string items are as below,
<string-array name="menu_items" >
<item >Abundance</item>
<item >Anxiety</item>
<item >Bruxism</item>
<item >Discipline</item>
<item >Drug Addiction</item>
</string-array>
<string-array name="menu_items_labels" >
<item ><FONT COLOR="#006600"><b>Abundance</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Anxiety</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Bruxism</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Discipline</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Drug Addiction</b></FONT></item>
</string-array>
In your spinner, try to use of the following,
// For setting the html code "menu_items_labels" with font color white
Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setAdapter(new ArrayAdapter<CharSequence>(this,
R.layout.multiline_spinner_dropdown_item, myActivity.this.getResources().getTextArray(R.array.menu_items_labels)));
To retrieve the values of string (Without HTML Formatting),
strTemp = getResources().getStringArray(R.array.menu_items)[spinner.getSelectedItemPosition()];

Remove an clicked item from listview android

public class ListCategorieActivity extends Activity implements AdapterView.OnItemClickListener {
public static String RISULTATO = "RISULTATO";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_categorie);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(ListCategorieActivity.this);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Bundle bundle = new Bundle();
Intent mIntent = new Intent();
String[] some_array = getResources().getStringArray(R.array.sections);
bundle.putString(RISULTATO,some_array[position]);
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
The Activity ListCategorieActivity show a clickable list of item. My task is eliminate (delete) the item from the listview when the item is clicked. How I can accomplish this task with this code?
activity_list_categorie.xml:
<RelativeLayout 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: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="com.example.utente.myapplication.ListCategorieActivity">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="#array/sections" >
</ListView>
array.xml:
<resources>
<string-array name="sections">
<item >OG 1</item>
<item >OG 2</item>
<item >OG 3</item>
<item >OG 4</item>
<item >OG 5</item>
<item >OG 6</item>
<item >OG 7</item>
<item >OG 8</item>
<item >OG 9</item>
<item >OG 10</item>
<item >OG 11</item>
<item >OG 12</item>
<item >OG 13</item>
<item >OS 1</item>
<item >OS 2-A</item>
<item >OS 2-B</item>
<item >OS 3</item>
<item >OS 4</item>
<item >OS 5</item>
<item >OS 6</item>
<item >OS 7</item>
<item >OS 8</item>
<item >OS 9</item>
<item >OS 10</item>
<item >OS 11</item>
<item >OS 12-A</item>
<item >OS 12-B</item>
<item >OS 13</item>
<item >OS 14</item>
<item >OS 15</item>
<item >OS 16</item>
<item >OS 17</item>
<item >OS 18-A</item>
<item >OS 18-B</item>
<item >OS 19</item>
<item >OS 20-A</item>
<item >OS 20-B</item>
<item >OS 21</item>
<item >OS 22</item>
<item >OS 23</item>
<item >OS 24</item>
<item >OS 25</item>
<item >OS 26</item>
<item >OS 27</item>
<item >OS 28</item>
<item >OS 29</item>
<item >OS 30</item>
<item >OS 31</item>
<item >OS 32</item>
<item >OS 33</item>
<item >OS 34</item>
<item >OS 35</item>
</string-array>
I think I not using an adapter.It is correct?
Just add listview.getAdapter.remove(position); in the OnItemClick method. I suppose you are using an ArrayAdapter though.
EDIT
I'm afraid my explanation was pretty sloppy. The OnItemClick method has 4 arguments: AdapterView l is the ListView whose children are being observed and int position is the child position in the ListView; if (and only if) the list adapter is an ArrayAdapter object, the remove(Object item) method is available and can be used to delete a list item; in order to get the right Object, one must call
Object item = ((ArrayAdapter)l.getAdapter()).getItem(position);
to get the Object to delete; then, the ((ArrayAdapter)l.getAdapter()).remove(item); can be called to remove the selected object.
You can put string resource into Arraylist, and simply use myArray.remove(position) method to delete an element in the list view.
You have 2 options
Remove the item from the list using the remove() method of your ArrayAdapter.
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
Or modify the ArrayList and call notifyDataSetChanged().
arrayList.remove([index]);
arrayAdapter.notifyDataSetChanged();

Custom Spinner At Runtime

I am creating a custom spinner at runtime. after running the application the spinner style has successfully changed, but there are no more drop down when I click on the spinner. Any help would be appreciated.
Code in styles.xml
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="#attr/spinner_style">#style/spinner_style</item>
</style>
<style name="spinner_style" parent="#android:style/Theme.Holo.Light">
<item name="android:background">#drawable/gradiant_spinner</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>
in attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <attr name="spinner_style" format="reference"/></resources>
in MyCustomSpinner:
public class MyCustomSpinner extends Spinner
{
public MyCustomSpinner(Context context) {
super(context, null, R.attr.spinner_style);
}
}
in Activity:
final MyCustomSpinner spinner = new MyCustomSpinner(context); //INITIALIZE THE SPINNER
spinner.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, lookupValues));
Try this:
final MyCustomSpinner spinner = new MyCustomSpinner(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter, lookupValues));
adapter.notifyDataSetChanged();
if it don't work try to the keyword "final" before MyCustomSpinner spinner = ..

Retrieve 2Dimensional array from string.xml in android

i want to keep 100 question and its answer in String .xml , each question having multiple answer , some question having 5 answer and some question having 7 answerlike this .... I want to retrieve those question and answer in my java class
<string-array name="array01">
<item name="question">question</item>
<item name="answer1">answer1</item>
<item name="answer2">answer2</item>
<item name="answer3">answer3</item>
<item name="answer4">answer4</item>
<item name="answer5">answer5</item>
</string-array>
<string-array name="array02">
<item name="question">question</item>
<item name="answer1">answer1</item>
<item name="answer2">answer2</item>
<item name="answer3">answer3</item>
<item name="answer4">answer4</item>
<item name="answer5">answer5</item>
<item name="answer6">answer6</item>
</string-array>
<string-array name="array03">
<item name="question">question</item>
<item name="answer1">answer1</item>
<item name="answer2">answer2</item>
<item name="answer3">answer3</item>
</string-array>
<array name="array0">
<item>#array/array01</item>
<item>#array/array02</item>
<item>#array/array03</item>
</array>
in java:
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.array0);
int alength = ta.length();
System.out.println(alength);//3
String[][] array = new String[alength][];
for (int i = 0; i < alength; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
System.out.println(array[i]);//[Ljava.lang.String;#42637ec0
} else {
System.out.println("something wrong with the XML");
// something wrong with the XML
}
}
ta.recycle(); // Important!
}

Categories

Resources