Autore Topic: ArrayList di HashMap come argomento di un baseadapter  (Letto 398 volte)

Offline Caco85

  • Nuovo arrivato
  • *
  • Post: 15
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Motorola moto g
  • Sistema operativo:
    Windows 7
ArrayList di HashMap come argomento di un baseadapter
« il: 21 Luglio 2014, 11:11:01 CEST »
0
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:
Codice (Java): [Seleziona]
public class SpeechBubbleAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<HashMap<String,String>> msgMap_al = new ArrayList<HashMap<String,String>>();
    private String msg;
    private String sender;
    private final String KEY_MSG = "message";
    private final String KEY_SENDER = "sender";
    private final String KEY_DATETIME = "datetime";


    public SpeechBubbleAdapter(Context context,ArrayList<HashMap<String,String>> messages){
        this.context=context;
        msgMap_al = messages;

    }

    @Override
    public int getCount() {
        return msgMap_al.size();
    }

    @Override
    public Object getItem(int position) {
        return msgMap_al.get(position).get(KEY_MSG);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        TextView speech_text_view;
        TextView datetime_text_view;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {



        ViewHolder holder;
        if(convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.speech_bubble, parent, false);
        }

        holder = new ViewHolder();

        holder.speech_text_view = (TextView) convertView.findViewById(R.id.message_text_view);
        holder.datetime_text_view = (TextView) convertView.findViewById(R.id.datetime_text_view);

        Log.d("SpeechBubbleAdapter: ","Message: "+msgMap_al.get(position).get(KEY_MSG)+"  |  Datetime: "+msgMap_al.get(position).get(KEY_DATETIME)+"   |   Position: "+position);
        holder.speech_text_view.setText(msgMap_al.get(position).get(KEY_MSG));
        holder.datetime_text_view.setText(msgMap_al.get(position).get(KEY_DATETIME));

        LayoutParams lp = (LayoutParams) holder.speech_text_view.getLayoutParams();

        if(msgMap_al.get(position).get(KEY_SENDER)=="a******@live.it")
        {
            lp.gravity = Gravity.RIGHT;
        }

        else
        {
            lp.gravity = Gravity.LEFT;
        }
        holder.speech_text_view.setLayoutParams(lp);

        return convertView;
    }
}
Questo adapter viene utilizzato da questa activity:
Codice (Java): [Seleziona]
public class ChatActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{

    EditText msg_et;
    private String recipient;
    private EditText ed;
    private final String KEY_MSG = "message";
    private final String KEY_SENDER = "sender";
    private final String KEY_RECIPIENT = "recipient";
    private final String KEY_DATETIME= "datetime";
    private SpeechBubbleAdapter sb;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_activity);
        ed = (EditText) findViewById(R.id.edit_text);
        msg_et = (EditText) findViewById(R.id.edit_text);
        recipient = getIntent().getExtras().getString(new ContactsListFragment().KEY_EMAIL);
        lv =(ListView) findViewById(R.id.chat_list_view);
        Bundle b = new Bundle();
        b.putString(KEY_RECIPIENT,recipient);
        LoaderManager lm = getLoaderManager();
        lm.initLoader(0, b, this);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }


    public void onClickSendButton(View v) {
        ContentValues cv = new ContentValues();
        cv.put(SandMessagesProvider.KEY_SENDER, "Caco@caco.it");  // sender = variabile globale
        cv.put(SandMessagesProvider.KEY_RECIPIENT, recipient);
        cv.put(SandMessagesProvider.KEY_DATETIME, FormattedDateTime.getDateTime());
        cv.put(SandMessagesProvider.KEY_TEXT, ed.getText().toString());
        ed.setText("");
        ContentResolver cr = getContentResolver();
        Uri rowUri = cr.insert(SandMessagesProvider.CONTENT_URI, cv);




    }



    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {SandMessagesProvider.KEY_SENDER,SandMessagesProvider.KEY_DATETIME,SandMessagesProvider.KEY_TEXT};
        String where = SandMessagesProvider.KEY_SENDER+"=?"+" OR " + SandMessagesProvider.KEY_RECIPIENT+"=?";
        String[] whereArgs ={args.getString(KEY_RECIPIENT),args.getString(KEY_RECIPIENT)};
        String sortOrder = SandMessagesProvider.KEY_ID;

        Uri queryUri = SandMessagesProvider.CONTENT_URI;
        CursorLoader cl = new CursorLoader(this, queryUri, projection, where, whereArgs, sortOrder);

        return cl;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (data.getCount()>0){
            ArrayList<HashMap<String,String>> al = new ArrayList<HashMap<String, String>>();

            int i=0;
        while (data.moveToNext()){
            //Log.d("ChatActivity","New hashmap "+i);
            HashMap<String,String> hm=new HashMap<String, String>();;

            hm.put(KEY_SENDER,data.getString(0));
            hm.put(KEY_DATETIME,data.getString(1));
            hm.put(KEY_MSG,data.getString(2));
            Log.d("ChatActivity: ","Message: "+hm.get(KEY_MSG)+"  |  Datetime: "+hm.get(KEY_DATETIME));
            al.add(hm);

            i++;

        }


            sb = new SpeechBubbleAdapter(this,al);
            lv.setAdapter(sb);

    }

    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

Quando lancio l'applicazione nella list view viene mostrato un solo item. Questo è il logcat:
Codice: [Seleziona]
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