J'obtiens une erreur en essayant de définir ma vue pour afficher ListView
le fichier que je souhaite afficher (fichier texte). Je suis presque sûr que cela a quelque chose à voir avec le xml. Je veux juste afficher les informations de this.file = fileop.ReadFileAsList("Installed_packages.txt");
. Mon code:
public class Main extends Activity {
private TextView tv;
private FileOperations fileop;
private String[] file;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fileop = new FileOperations();
this.file = fileop.ReadFileAsList("Installed_packages.txt");
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
ListView lv = new ListView(this);
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
setContentView(lv);
}
}
list_item.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"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</LinearLayout>
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5sp"
android:id="@+id/TextView01"
android:text="@string/hello"/>
</ScrollView>
</LinearLayout>
la source
new ArrayAdapter<String>(this, R.layout.a_layout_file, R.id.a_text_view_within_layout, this.file)
Voir javadoc pourandroid.widget.ArrayAdapter
Soution est là
listitem.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="match_parent" > </TextView> </LinearLayout>
Code Java:
String[] countryArray = {"India", "Pakistan", "USA", "UK"}; ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray); ListView listView = (ListView) findViewById(R.id.listview); listView.setAdapter(adapter);
la source
Si vous recevez ce message lorsque vous étendez un ArrayAdapter, vous obtenez cette erreur car vous n'avez pas fourni l'ID de ressource correct pour afficher l'élément. Appelez la super classe dans le constructeur et passez l'ID de ressource de TextView:
//Pass in the resource id: R.id.text_view SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this, R.id.text_view, new ArrayList<>());
Adaptateur:
public class SpinnerAdapter extends ArrayAdapter<MyEntity> { private Context context; private List<MyEntity> values; public SpinnerAdapter(Context context, int textViewResourceId, List<MyEntity> values) { //Pass in the resource id: R.id.text_view super(context, textViewResourceId, values); this.context = context; this.values = values; }
la source