首先,谢谢你的回答!我是Android的新手,这是我的问题;
我想将字符串数组的值带到listview。
程序适用于以下代码:
private String [] fiilliste={"A","Ağlamak","Almak","B","Bulmak"};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fiilliste);
vlist.setAdapter(adapter);
但我想将xml文件用于字符串数组。这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="fi">
<item>A</item>
<item>Almak</item>
<item>Anlamak</item>
<item>Anlasmak</item>
<item>Anlatmak</item>
<item>Ayrilmak</item>
</string-array>
</resources>
我只是用以下代码更改了代码,现在程序无效。
String [] fiilliste= getResources().getStringArray(R.array.fi);
你可以帮我解决这个问题吗?谢谢!
活动代码:
package com.gazi.tezproje;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
public class verbs extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
ActionBar bar=getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
MenuInflater menu1 = getMenuInflater();
menu1.inflate(R.menu.info, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuinfo:
Toast.makeText(getApplicationContext(),getString(R.string.verbs), Toast.LENGTH_LONG).show();
break;
case android.R.id.home:
finish();
break;
}
return false;
}
private ListView vlist;
EditText ara;
String [] fiilliste= getResources().getStringArray(R.array.fi);
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbs);
vlist=(ListView)findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fiilliste);
vlist.setAdapter(adapter);
ara = (EditText) findViewById(R.id.editText1);
vlist.setTextFilterEnabled(true);
ara.addTextChangedListener(new TextWatcher(){
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
/* When user changed the Text */
verbs.this.adapter.getFilter().filter(cs);
}
@Override
public void afterTextChanged(Editable arg0) {
/*TODO Auto-generated method stub*/
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
/*TODO Auto-generated method stub*/
}
});
}
}
答案 0 :(得分:51)
更改为
String [] fiilliste;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbs);
fiilliste = getResources().getStringArray(R.array.fi);
// move this in onCreate
我假设你在strings.xml
中有字符串数组fi。
你可能得到了NullPointerException
。您需要getResources()
的活动上下文。所以将它移到onCreate
。
答案 1 :(得分:19)
这就是我所做的。我在array.xml中创建了一个字符串数组
<resources>
<string-array name="numbers">
<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>
</string-array>
并在我的xml布局中放置一个lisView
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:entries="@array/numbers"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
这是我的MainActivity的样子。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView listView;
String[] number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
number = getResources().getStringArray(R.array.numbers);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, number);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
Toast.makeText(this, textView.getText() , Toast.LENGTH_SHORT).show();
}
}
答案 2 :(得分:2)
从.xml文件中删除android:entries
&amp;将字符串数组定义为
String[] number = getResources().getStringArray(R.id.numbers);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, number);
答案 3 :(得分:0)
您的问题是代码应该放在创建或任何其他功能上。不是静态变量