0

チャットプログラムを作ろうとしています。私の問題は、背景画像が更新される (送信者が変更される) たびに、配列内のすべての項目が同じ背景になることです。送信メッセージが 1 つのタイプの背景を取得し、受信メッセージが別のタイプの背景を取得するように、これを変更するにはどうすればよいですか?

    public class RowAdapter extends BaseAdapter {

private Context context;
private int textViewResourceId;
private final ArrayList<String> messageList;
private final ArrayList<String> senderList;
public String messageDirection; 
boolean out; 

public RowAdapter(Context context, int textViewResourceId, ArrayList<String> messageList, ArrayList<String> senderList) {
super();

this.textViewResourceId = textViewResourceId;
this.messageList = messageList; 
this.senderList = senderList; 
this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){ 
View rowView = convertView;
ViewHolder holder;
long timestamp = System.currentTimeMillis();
String readableTimestamp = (String)DateUtils.getRelativeTimeSpanString(timestamp);

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(textViewResourceId, parent, false);

    holder = new ViewHolder();
    holder.label = (TextView)rowView.findViewById(R.id.label);
    holder.message=(TextView)rowView.findViewById(R.id.message);
    holder.time = (TextView)rowView.findViewById(R.id.textTime);
    holder.RL = (RelativeLayout)rowView.findViewById(R.id.userAndMessage);
    rowView.setTag(holder);
}else{
    holder = (ViewHolder)rowView.getTag();
}

holder.label.setText(senderList.get(position));
holder.message.setText(messageList.get(position));
holder.time.setText(readableTimestamp);

if(out){
    if (messageList.get(position)!= null) {
        holder.RL.setBackgroundResource(R.drawable.yellow);
       ((RelativeLayout) rowView).setGravity(Gravity.RIGHT);
    }

}else{
    if (messageList.get(position)!= null) {
        holder.RL.setBackgroundResource(R.drawable.green);
       ((RelativeLayout) rowView).setGravity(Gravity.LEFT);
    }
}
return rowView;
}

public void setOut(boolean out){
this.out = out; 
}
static class ViewHolder{
RelativeLayout RL; 
TextView label;
TextView message;
TextView time;

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

@Override 
public Object getItem(int position) {
    return position;
}

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

}
4

1 に答える 1

2

わかりました、これをやってみてください

これは単なる例であり、その中にプロセスを追加する必要があります

メイン レイアウト xml

<LinearLayout 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:orientation="vertical"
    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=".MainActivity" >

    <ListView
        android:id="@+id/listView_Chat"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:background="#123456"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="fromMeBt"
            android:text="From me" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="fromOtherBt"
            android:text="From other" />
    </LinearLayout>

</LinearLayout>

リスト行 xml ファイルを追加する

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/txt_chat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

ChatObject という名前の新しいクラスを作成します

public class ChatObject {

    private String mssg;
    private int from;

    public ChatObject(String mssg, int from) {
        super();
        this.mssg = mssg;
        this.from = from;
    }

    public String getMssg() {
        return mssg;
    }

    public void setMssg(String mssg) {
        this.mssg = mssg;
    }

    public int getFrom() {
        return from;
    }

    public void setFrom(int from) {
        this.from = from;
    }

}

次に、このように MainActivity にいくつかの変更を加えます

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ArrayList<ChatObject> items = new ArrayList<ChatObject>();
    ListView listView_chat;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new CustomAdapter(items);
        listView_chat = (ListView) findViewById(R.id.listView_Chat);
        listView_chat.setAdapter(adapter);
    }

    @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 void fromMeBt(View v) {
        ChatObject object = new ChatObject("from me :)", 0);
        items.add(object);
        adapter.notifyDataSetChanged();
        listView_chat.setSelection(items.size());
    }

    public void fromOtherBt(View v) {
        ChatObject object = new ChatObject("from other :)", 1);
        items.add(object);
        adapter.notifyDataSetChanged();
        listView_chat.setSelection(items.size());
    }

    class CustomAdapter extends BaseAdapter {

        ArrayList<ChatObject> items;

        public CustomAdapter(ArrayList<ChatObject> items) {
            // TODO Auto-generated constructor stub
            this.items = items;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_chat, null);
            TextView txt_chat = (TextView) convertView.findViewById(R.id.txt_chat);

            ChatObject object = items.get(position);
            txt_chat.setText(object.getMssg());

            if (object.getFrom() == 0) {
                convertView.setBackgroundColor(Color.BLUE);
            }else {
                convertView.setBackgroundColor(Color.CYAN);
            }

            return convertView;
        }

    }
}

それだけです。xml レイアウトのパディングを変更するか、これを dimens.xml ファイルに追加する場合

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>

今幸せです?これがお役に立てば幸いです

于 2013-10-16T11:08:42.257 に答える