Bonjour !
J'essaie de customiser une ListView et un ArrayAdapter.
J'ai une classe toute bete Movie :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
package com.example.recmovieapp;
 
public class Movie 
{
	int id;
	String title;
	int rate;
 
	public Movie()
	{
		id = 0;
		title = null;
		rate= 0;
	}
 
	public Movie(int i,String t, int r)
	{
		id = i;
		title = t;
		rate = r;
	}
 
	public int getID()
	{
		return id;
	}
 
	public String getTitle()
	{
		return title;
	}
 
	public int getRate()
	{
		return rate;
	}
}
Voici Mon ArrayAdapter :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
package com.example.recmovieapp;
 
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class MovieAdapter extends ArrayAdapter<Movie>
{
	Context myContext;
	int layoutRessourceId;
	ArrayList<Movie> myList;
 
	public MovieAdapter(Context c, int layoutRID, ArrayList<Movie> list)
	{
		super(c,layoutRID,list);
		myContext = c;
		layoutRessourceId = layoutRID;
		myList = new ArrayList<Movie>(list);
	}
 
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		if(row == null)
		{
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(layoutRessourceId, parent, false);
		}
		TextView movieID = (TextView) row.findViewById(R.id.MovieId);
		TextView movieTitle = (TextView) row.findViewById(R.id.MovieTitle);
		TextView movieRate = (TextView) row.findViewById(R.id.MovieRate);
		movieID.setText(myList.get(position).getID());
		//movieTitle.setText(myList.get(position).getTitle());
		//movieRate.setText(myList.get(position).getRate());
		return row;
	}
}
Ma principale activity possede les balise listview :activity_user_info.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
<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: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=".UserInfoActivity" >
 
    <TextView
        android:id="@+id/UserId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
 
    <TextView
        android:id="@+id/UserGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/UserId"
        android:layout_below="@+id/UserId"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
 
    <TextView
        android:id="@+id/UserAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/UserGender"
        android:layout_below="@+id/UserGender"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
 
    <TextView
        android:id="@+id/UserOccupation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/UserAge"
        android:layout_below="@+id/UserAge"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
 
    <ListView
        android:id="@+id/UserMovies"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/UserOccupation"
        android:layout_below="@+id/UserOccupation"
        android:layout_marginTop="14dp" >
    </ListView>
 
</RelativeLayout>
Ma list view est customisable avec ce layout : list_movie.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
<?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" 
    android:padding="10dp">
 
<TextView
        android:id="@+id/MovieId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
 
    <TextView
        android:id="@+id/MovieTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/MovieId"
        android:layout_below="@+id/MovieId"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
 
    <TextView
        android:id="@+id/MovieRate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/MovieRate"
        android:layout_below="@+id/MovieRate"
        android:textAppearance="?android:attr/textAppearanceMedium"
     />
</LinearLayout>
J'essaie de creer ma list view dans cette activity :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
package com.example.recmovieapp;
 
import java.util.ArrayList;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
 
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
public class UserInfoActivity extends Activity 
{
	TextView tID = null;
	TextView tGender;
	TextView tAge;
	TextView tOccupation;
	ProgressDialog pDialog;
	ListView movieList;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_info);
 
		tID = (TextView)findViewById(R.id.UserId);
		tGender = (TextView)findViewById(R.id.UserGender);
		tAge = (TextView)findViewById(R.id.UserAge);
		tOccupation = (TextView)findViewById(R.id.UserOccupation);
		movieList = (ListView) findViewById(R.id.UserMovies);
		GetUserInfo myThread = new GetUserInfo(UserInfoActivity.this);
		myThread.execute();
 
	}
 
	@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;
	}
 
	public class GetUserInfo extends AsyncTask<String, Void, String>
	{
 
		Context mContext = null;
		Exception exception = null;
		User myUser;
		ArrayList<Movie> listItems = new ArrayList<Movie>();
 
		GetUserInfo(Context context)
		{
			mContext = context;
		}
 
		protected void onPreExecute() 
		{
            super.onPreExecute();
            pDialog = new ProgressDialog(mContext);
            pDialog.setMessage("Loading details, please wait ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
		@Override
		protected String doInBackground(String... arg0) 
		{
 
			try
			{
				//Create the HTTP request
				HttpParams httpParameters = new BasicHttpParams();
 
				//Setup timeouts
				HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
				HttpConnectionParams.setSoTimeout(httpParameters, 15000);			
 
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost("http://kevin.pinero.alwaysdata.net/RecMovie/db.php");      
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
 
				String result = EntityUtils.toString(entity);
 
				// Create a JSON object from the request response
				JSONObject jsonObject = new JSONObject(result);
				JSONArray user = jsonObject.getJSONArray("user");
				JSONObject u = user.getJSONObject(0);
				//Retrieve the data from the JSON object
				myUser = new User(u.getInt("id"),u.getString("gender"),u.getString("age"),u.getString("occupation"));
 
				JSONArray movies = jsonObject.getJSONArray("movies");
				for(int i = 0; i < movies.length(); i++)
				{
					JSONObject movie = movies.getJSONObject(i);
					listItems.add(new Movie(movie.getInt("id"), movie.getString("title"), movie.getInt("rate")));
				}
			}
			catch (Exception e)
			{
				Log.e("doInBackground", "Error:", e);
				exception = e;
			}
 
			return null;
		}
 
		@Override
		protected void onPostExecute(String str)
		{
			//Update the UI
			tID.setText("ID : " + myUser.getID());
			tGender.setText("Gender : " + myUser.getGender());
			tAge.setText("Age : " + myUser.getAge());
			tOccupation.setText("Occupation : " + myUser.getOccupation());
			MovieAdapter adapter = new MovieAdapter(mContext, R.layout.list_movie, listItems);
			movieList.setAdapter(adapter);
 
			pDialog.dismiss();
		}
 
	}
}
Pour faire court je pense cesl ignes sont les plus importantes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
ListView movieList;
movieList = (ListView) findViewById(R.id.UserMovies);
ArrayList<Movie> listItems = new ArrayList<Movie>();
MovieAdapter adapter = new MovieAdapter(mContext, R.layout.list_movie, listItems);
			movieList.setAdapter(adapter);
(mon listItems est rempli dans le asynctask class)

Le probleme est que l'application crashe a
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
MovieAdapter adapter = new MovieAdapter(mContext, R.layout.list_movie, listItems);
			movieList.setAdapter(adapter);
Je pense que j'ai mal fait quelque chose dans ma class Adapter mais je ne trouve pas quoi ?

mon logcat :
08-20 15:17:19.464: E/AndroidRuntime(824): FATAL EXCEPTION: main
08-20 15:17:19.464: E/AndroidRuntime(824): android.content.res.Resources$NotFoundException: String resource ID #0xf0
08-20 15:17:19.464: E/AndroidRuntime(824): at android.content.res.Resources.getText(Resources.java:230)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.TextView.setText(TextView.java:3769)
08-20 15:17:19.464: E/AndroidRuntime(824): at com.example.recmovieapp.MovieAdapter.getView(MovieAdapter.java:37)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.ListView.onMeasure(ListView.java:1158)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.View.measure(View.java:15518)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.View.measure(View.java:15518)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.View.measure(View.java:15518)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.View.measure(View.java:15518)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-20 15:17:19.464: E/AndroidRuntime(824): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.View.measure(View.java:15518)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.Choreographer.doFrame(Choreographer.java:532)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.os.Handler.handleCallback(Handler.java:725)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.os.Looper.loop(Looper.java:137)
08-20 15:17:19.464: E/AndroidRuntime(824): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-20 15:17:19.464: E/AndroidRuntime(824): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 15:17:19.464: E/AndroidRuntime(824): at java.lang.reflect.Method.invoke(Method.java:511)
08-20 15:17:19.464: E/AndroidRuntime(824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-20 15:17:19.464: E/AndroidRuntime(824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-20 15:17:19.464: E/AndroidRuntime(824): at dalvik.system.NativeStart.main(Native Method)
A priori l'erreur viendrait de se bloc :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public View getView(int position, View convertView, ViewGroup parent)
	{
		View row = convertView;
		if(row == null)
		{
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(layoutRessourceId, parent, false);
		}
		TextView movieID = (TextView) row.findViewById(R.id.MovieId);
		TextView movieTitle = (TextView) row.findViewById(R.id.MovieTitle);
		TextView movieRate = (TextView) row.findViewById(R.id.MovieRate);
		movieID.setText(myList.get(position).getID());
		//movieTitle.setText(myList.get(position).getTitle());
		//movieRate.setText(myList.get(position).getRate());
		return row;
	}
Juste ici
Code : Sélectionner tout - Visualiser dans une fenêtre à part
movieID.setText(myList.get(position).getID());
Quelqu'un pourrait il me conseiller ?
J'ai lus plusieurs tuto et je vois vraiment pas ce qu'il ne va pas : /