Ciao a tutti, non riesco proprio a risolvere questo problema, magari voi potete aiutarmi:
per mostrare gli item di una list view ho creato una classe che estende BaseAdapter:
x1
2public class SpeechBubbleAdapter extends BaseAdapter {
3
4private Context context;
5private ArrayList<HashMap<String,String>> msgMap_al = new ArrayList<HashMap<String,String>>();
6private String msg;
7private String sender;
8private final String KEY_MSG = "message";
9private final String KEY_SENDER = "sender";
10private final String KEY_DATETIME = "datetime";
11
12
13public SpeechBubbleAdapter(Context context,ArrayList<HashMap<String,String>> messages){
14this.context=context;
15msgMap_al = messages;
16
17}
18
19
20public int getCount() {
21return msgMap_al.size();
22}
23
24
25public Object getItem(int position) {
26return msgMap_al.get(position).get(KEY_MSG);
27}
28
29
30public long getItemId(int position) {
31return position;
32}
33
34class ViewHolder {
35TextView speech_text_view;
36TextView datetime_text_view;
37}
38
39
40public View getView(int position, View convertView, ViewGroup parent) {
41
42
43
44ViewHolder holder;
45if(convertView == null)
46{
47LayoutInflater inflater = (LayoutInflater)context.getSystemService
48(Context.LAYOUT_INFLATER_SERVICE);
49convertView = inflater.inflate(R.layout.speech_bubble, parent, false);
50}
51
52holder = new ViewHolder();
53
54holder.speech_text_view = (TextView) convertView.findViewById(R.id.message_text_view);
55holder.datetime_text_view = (TextView) convertView.findViewById(R.id.datetime_text_view);
56
57Log.d("SpeechBubbleAdapter: ","Message: "+msgMap_al.get(position).get(KEY_MSG)+" | Datetime: "+msgMap_al.get(position).get(KEY_DATETIME)+" | Position: "+position);
58holder.speech_text_view.setText(msgMap_al.get(position).get(KEY_MSG));
59holder.datetime_text_view.setText(msgMap_al.get(position).get(KEY_DATETIME));
60
61LayoutParams lp = (LayoutParams) holder.speech_text_view.getLayoutParams();
62
63if(msgMap_al.get(position).get(KEY_SENDER)=="a******@live.it")
64{
65lp.gravity = Gravity.RIGHT;
66}
67
68else
69{
70lp.gravity = Gravity.LEFT;
71}
72holder.speech_text_view.setLayoutParams(lp);
73
74return convertView;
75}
76}
Questo adapter viene utilizzato da questa activity:
1111public class ChatActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{
2
3EditText msg_et;
4private String recipient;
5private EditText ed;
6private final String KEY_MSG = "message";
7private final String KEY_SENDER = "sender";
8private final String KEY_RECIPIENT = "recipient";
9private final String KEY_DATETIME= "datetime";
10private SpeechBubbleAdapter sb;
11private ListView lv;
12
13
14protected void onCreate(Bundle savedInstanceState) {
15super.onCreate(savedInstanceState);
16setContentView(R.layout.chat_activity);
17ed = (EditText) findViewById(R.id.edit_text);
18msg_et = (EditText) findViewById(R.id.edit_text);
19recipient = getIntent().getExtras().getString(new ContactsListFragment().KEY_EMAIL);
20lv =(ListView) findViewById(R.id.chat_list_view);
21Bundle b = new Bundle();
22b.putString(KEY_RECIPIENT,recipient);
23LoaderManager lm = getLoaderManager();
24lm.initLoader(0, b, this);
25}
26
27
28
29public boolean onCreateOptionsMenu(Menu menu) {
30// Inflate the menu; this adds items to the action bar if it is present.
31getMenuInflater().inflate(R.menu.chat, menu);
32return true;
33}
34
35
36public boolean onOptionsItemSelected(MenuItem item) {
37// Handle action bar item clicks here. The action bar will
38// automatically handle clicks on the Home/Up button, so long
39// as you specify a parent activity in AndroidManifest.xml.
40int id = item.getItemId();
41if (id == R.id.action_settings) {
42return true;
43}
44return super.onOptionsItemSelected(item);
45
46}
47
48
49public void onClickSendButton(View v) {
50ContentValues cv = new ContentValues();
51cv.put(SandMessagesProvider.KEY_SENDER, "Caco@caco.it"); // sender = variabile globale
52cv.put(SandMessagesProvider.KEY_RECIPIENT, recipient);
53cv.put(SandMessagesProvider.KEY_DATETIME, FormattedDateTime.getDateTime());
54cv.put(SandMessagesProvider.KEY_TEXT, ed.getText().toString());
55ed.setText("");
56ContentResolver cr = getContentResolver();
57Uri rowUri = cr.insert(SandMessagesProvider.CONTENT_URI, cv);
58
59
60
61
62}
63
64
65
66
67public Loader<Cursor> onCreateLoader(int id, Bundle args) {
68String[] projection = {SandMessagesProvider.KEY_SENDER,SandMessagesProvider.KEY_DATETIME,SandMessagesProvider.KEY_TEXT};
69String where = SandMessagesProvider.KEY_SENDER+"=?"+" OR " + SandMessagesProvider.KEY_RECIPIENT+"=?";
70String[] whereArgs ={args.getString(KEY_RECIPIENT),args.getString(KEY_RECIPIENT)};
71String sortOrder = SandMessagesProvider.KEY_ID;
72
73Uri queryUri = SandMessagesProvider.CONTENT_URI;
74CursorLoader cl = new CursorLoader(this, queryUri, projection, where, whereArgs, sortOrder);
75
76return cl;
77}
78
79
80public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
81if (data.getCount()>0){
82ArrayList<HashMap<String,String>> al = new ArrayList<HashMap<String, String>>();
83
84int i=0;
85while (data.moveToNext()){
86//Log.d("ChatActivity","New hashmap "+i);
87HashMap<String,String> hm=new HashMap<String, String>();;
88
89hm.put(KEY_SENDER,data.getString(0));
90hm.put(KEY_DATETIME,data.getString(1));
91hm.put(KEY_MSG,data.getString(2));
92Log.d("ChatActivity: ","Message: "+hm.get(KEY_MSG)+" | Datetime: "+hm.get(KEY_DATETIME));
93al.add(hm);
94
95i++;
96
97}
98
99
100sb = new SpeechBubbleAdapter(this,al);
101lv.setAdapter(sb);
102
103}
104
105}
106
107
108public void onLoaderReset(Loader<Cursor> loader) {
109
110}
111}
Quando lancio l'applicazione nella list view viene mostrato un solo item. Questo è il logcat:
codice:07-21 09:08:36.010 656-656/com.wix.caco85.sand I/ActivityThread﹕ Pub com.wix.caco85.sand.sandcontactsprovider: com.wix.caco85.sand.database.SandContactsProvider 07-21 09:08:36.044 656-656/com.wix.caco85.sand I/ActivityThread﹕ Pub com.wix.caco85.sand.sandmessagesprovider: com.wix.caco85.sand.database.SandMessagesProvider 07-21 09:08:36.462 656-659/com.wix.caco85.sand D/dalvikvm﹕ GC_CONCURRENT freed 200K, 2% free 14329K/14599K, paused 8ms+4ms 07-21 09:08:36.540 656-656/com.wix.caco85.sand D/gralloc_goldfish﹕ Emulator without GPU emulation detected. 07-21 09:09:00.380 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: hey | Datetime: 14/7/14 9:34:23 07-21 09:09:00.380 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: ciao caco | Datetime: 14/7/14 9:37:43 07-21 09:09:00.380 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: h | Datetime: 15/7/14 8:2:32 07-21 09:09:00.380 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: ciaooo | Datetime: 15/7/14 8:2:39 07-21 09:09:00.380 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: heyla | Datetime: 15/7/14 8:6:29 07-21 09:09:00.380 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: ciao | Datetime: 15/7/14 13:31 07-21 09:09:00.390 656-656/com.wix.caco85.sand D/ChatActivity:﹕ Message: hey | Datetime: 15/7/14 13:32 07-21 09:09:00.450 656-656/com.wix.caco85.sand D/SpeechBubbleAdapter:﹕ Message: hey | Datetime: 14/7/14 9:34:23 | Position: 0 07-21 09:09:00.460 656-656/com.wix.caco85.sand D/SpeechBubbleAdapter:﹕ Message: hey | Datetime: 14/7/14 9:34:23 | Position: 0 07-21 09:09:00.460 656-656/com.wix.caco85.sand D/SpeechBubbleAdapter:﹕ Message: hey | Datetime: 14/7/14 9:34:23 | Position: 0 07-21 09:09:01.150 656-659/com.wix.caco85.sand D/dalvikvm﹕ GC_CONCURRENT freed 65K, 1% free 14718K/14855K, paused 5ms+114ms