0

我已经在这里搜索了很长一段时间,但无法找到我当前的问题(尽管还有很多其他问题)。

我用一个监听器制作了一个微调器,在那个监听器内部,我试图调用在 onCreate 处显示的 ListView 的失效。现在,微调器包含语言,根据语言,ListView 中应显示不同的内容。问题是当我在微调器中选择一个项目时,什么也没有发生。

我的主要活动:

public class MainActivity extends Activity implements OnItemSelectedListener {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private ListView termsListView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String selectedLanguage = "";
    createListView(selectedLanguage);
}

public void createListView(String selectedLanguage)
{
    termsListView = (ListView) findViewById(R.id.termsListView);
    SharedPreferences sharedPref = getSharedPreferences("Language", 0);
    String language = sharedPref.toString();


 // Initialize the terms array
    Term [] items = {
            new Term(1, "Acceptance", "The process of assessing whether a system satisfies all its requirements", false, "Acceptans", "", language),
            new Term(2, "Acceptance test", "A test that assesses whether a system satisfies all its requirements", false, "Acceptans test", "", language),
            new Term(3, "Activity diagram", "A diagram type in UML which models the flow of actions in a system or in a component including data flows and areas of responsibility where necessary", false, "Aktivitets diagram", "", language),
            new Term(4, "Actor", "1. Generally in RE: A person, a system or a technical device in the context of a system that interacts with the system.2. Especially in goal-oriented RE: a person, a system or a technical device that may act and process information in order to achieve some goals", false, "Aktör", "", language),
    };
    ArrayAdapter<Term> adapter = new ArrayAdapter<Term>(this,android.R.layout.simple_list_item_1, items);
    termsListView.setAdapter(adapter);

    termsListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            String item = ((TextView)view).getText().toString();

            Intent intent = new Intent(view.getContext(), DisplayDescription.class);
            intent.putExtra(EXTRA_MESSAGE, item);
            startActivity(intent);

        }
    });
    createSpinner();

}
public void createSpinner(){
    Spinner spinner = (Spinner) findViewById(R.id.languageSpinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.languages_array, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);
}
public void addListenertoSpinner(){
    final Spinner spinner1 = (Spinner) findViewById(R.id.languageSpinner);
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            String spinnerValue = spinner1.getSelectedItem().toString();
            SharedPreferences sharedPref = getSharedPreferences("Language", 0);
            SharedPreferences.Editor editedPrefs = sharedPref.edit();
            editedPrefs.putString("Language", spinnerValue);
            editedPrefs.commit();
            termsListView.invalidate();

            }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });


} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


//@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    // TODO Auto-generated method stub

}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

还有我的 XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollMainActivityLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:id="@+id/activityMainLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
  <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/selectLanguageTitle" />
  <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
  <Spinner
        android:id="@+id/languageSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/languages_prompt"/>
  <Button 
        android:id="@+id/languageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/languageSpinner"
        android:text="@string/languageButtonTitle"/>
  </RelativeLayout>
  <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/glossaryTitle" />
  <ListView
        android:id="@+id/termsListView"
        android:layout_width="fill_parent"
        android:layout_height="310dp">
  </ListView>
</LinearLayout>

术语.java

package se.inceptive.irebglossary;

import java.util.ArrayList;

import android.content.Context;
//import android.content.SharedPreferences;

public class Term extends MainActivity{
private int termId;
private String termNameEnglish;
private String termDescriptionEnglish;
private boolean termJA;
private String termNameSwedish;
private String termDescriptionSwedish;
private static ArrayList<Term> terms;
//  private String selectedLanguage;
private Context context;
private String termSetLanguage;


 public Term(){
        super();
 }

 public Term(int collectId, String collectEnglish, String collectDescriptionEnglish, boolean collectJA, String collectSwedish, String collectDescriptionSwedish, String collectLanguage) {
        super();
        this.termId = collectId;
        this.termNameEnglish = collectEnglish;
        this.termDescriptionEnglish = collectDescriptionEnglish;
        this.termJA = collectJA;
        this.termNameSwedish = collectSwedish;
        this.termDescriptionSwedish = collectDescriptionSwedish;
        this.termSetLanguage = collectLanguage;

        if(terms == null)
            terms = new ArrayList<Term>();

        terms.add(this);
 }

 public static String getDescriptionFromName(String name)
 {
     for(Term term : terms)
     {
         if(name.equals(term.termNameEnglish))
             return term.termDescriptionEnglish;
         if(name.equals(term.termNameSwedish))
             return term.termDescriptionSwedish;
     }
     return null;
 }

 @Override
 public String toString() {
     //String selectedLanguage = Context.getPreferences().toString();
     //String selectedLanguage = this.context.getResources().getString(R.string.selectedLanguage);

     if(termSetLanguage.equals("English"))
     {
         return this.termNameEnglish;
     }
     if(termSetLanguage.equals("Svenska"))
     {
         return this.termNameSwedish;
     }
     else
     {
         return this.termNameEnglish;
     }
 }

}

此外,我没有收到任何 logCat 错误。

非常感谢任何帮助。

4

1 回答 1

0

你打电话到哪里了addListenertoSpinner();?在声明你的 Spinner 后调用它

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        String languages={"English","Spanish","french"};

    termsListView = (ListView) findViewById(R.id.termsListView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listdata(0));
    termsListView.setAdapter(adapter);


    Spinner spinner = (Spinner) findViewById(R.id.languageSpinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            languages, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            String spinnerValue = spinner.getSelectedItem().toString();

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listdata(pos));
            termsListView.setAdapter(adapter);// here we will update the listview data according to selected language



            }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });





}



public String[] listdata(int language)

{
    if(language==0)
    {
        String languagearray[]={"English-One","English-Two"};
        return languagearray;
    }

    else if(language==1)
    {
        String languagearray[]={"Spanish-One","Spanish-Two"};
        return languagearray;
    }
    else
    {
        String languagearray[]={"French-One","French-Two"};
        return languagearray;
    }
}
于 2013-05-23T12:15:21.243 回答