我能够发送和接收消息xmpp客户端并在相应的布局中以listview显示它们。当我发送或接收消息时。我将发送和接收的消息组合在不同布局的单个文本视图中。我需要在不同布局的textview中显示它们。我希望在1个布局的textview中显示收到的消息,并在另一个布局textview中相应地发送消息。
请在这个问题上指导我..
提前致谢。
答案 0 :(得分:0)
您可以通过以下方式执行此操作: -
package com.aman.sample.bubblelist;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity {
String[] data = { "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data ", "Sample User Data ",
"Sample User Data ", "Sample User Data " };
Drawable[] usrimg = null;
String bgimg = "", _user = "", _pass = "";
int odd_resID, even_resID;
ListView myList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// finding the list view
myList = (ListView) findViewById(R.id.myList);
myList.setAdapter(new MyCustomAdapter());
myList.setCacheColorHint(0);
}
class MyCustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return data.length;
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return data.length;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
int _intPosition = getItemViewType(position);
String even_color, odd_color;
SharedPreferences prefList = getSharedPreferences("PrefsFile",
MODE_PRIVATE);
;
even_color = prefList.getString("even_bubble_color", "pink");
odd_color = prefList.getString("odd_bubble_color", "green");
int even_color_id = getResources().getIdentifier(even_color,
"drawable", "com.teks.chilltwit"), odd_color_id = getResources()
.getIdentifier(odd_color, "drawable", "com.teks.chilltwit");
ImageView even_view, odd_view;
if (_intPosition % 2 == 0) {
row = inflater.inflate(R.layout.list_row_layout_even, parent,
false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText(data[position]);
} else {
row = inflater.inflate(R.layout.list_row_layout_odd, parent,
false);
TextView textLabel = (TextView) row.findViewById(R.id.text);
textLabel.setText(data[position]);
}
return (row);
}
}
}