我是整个android / java的新东西,今天让我有点困难。 我知道之前有很多关于这个问题的问题,但到目前为止我没有尝试过任何工作,我的应用程序一直在喋喋不休:/
我要做的是使用两个不同的textView填充listView,具体取决于消息的来源。我在LogCat中的错误是“您必须为TextView提供资源ID”。
我的代码在下面看到;
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/label_client"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingLeft="5dp"
android:textColor="@color/Black"
android:background="@drawable/green"
android:text="@string/hello_world">
</TextView>
<TextView
android:id="@+id/label_server"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingLeft="5dp"
android:textColor="@color/Black"
android:background="@drawable/yellow"
android:text="@string/hello_world">
</TextView>
</LinearLayout>
RowAdapter.java:
public class RowAdapter extends ArrayAdapter<String>{
int textViewResourceId;
private final ArrayList<String> arrayList;
int sender;
public RowAdapter(Context context, int textViewResourceId, ArrayList<String> arrayList, int sender){
super(context, R.layout.row, arrayList);
this.textViewResourceId = textViewResourceId;
this.arrayList = arrayList;
this.sender = sender;
}
//@Override
public View getview(int position, View convertView, ViewGroup parent){
View rowView = convertView;
if (rowView==null){
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(textViewResourceId, parent, false);
}
String s = arrayList.get(position);
if(sender==0){
if (s != null) {
TextView tt = (TextView) rowView.findViewById(R.id.label_client);
if (tt != null){
tt.setText(s);
}
}
}
if(sender==1){
if (s != null) {
TextView tt = (TextView) rowView.findViewById(R.id.label_server);
if (tt != null){
tt.setText(s);
}
}
}
return rowView;
}
}
当我从主要活动中调用它时,它就像这样:
public class MainActivity extends Activity {
private ArrayList<String> arrayList = new ArrayList<String>();
ListView listView;
RowAdapter radapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radapter = new RowAdapter(this, R.layout.row, arrayList, sender);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(radapter);
new Thread(new ClientThread()).start();
}
public void onClick(View view) {
sender = 0;
AddChat(str);
radapter.notifyDataSetChanged();
}
public void AddChat(String msg){
arrayList.add(msg);
}
}
main.xml中
<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=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:drawSelectorOnTop="false"
tools:listitem="@layout/row" >
</ListView>
</RelativeLayout>
对于长篇文章真的很抱歉,只是觉得最好给你全部的信息背景:)
答案 0 :(得分:0)
请尝试使用BaseAdapter:
public class RowAdapter extends BaseAdapter {
private Context context;
private int textViewResourceId;
private final ArrayList<String> arrayList;
private int sender;
public RowAdapter(Context context, int textViewResourceId,
ArrayList<String> arrayList, int sender) {
super();
this.textViewResourceId = textViewResourceId;
this.arrayList = arrayList;
this.sender = sender;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(textViewResourceId, parent, false);
}
String s = arrayList.get(position);
if (sender == 0) {
if (s != null) {
TextView tt = (TextView) convertView
.findViewById(R.id.label_client);
if (tt != null) {
tt.setText(s);
}
}
}
if (sender == 1) {
if (s != null) {
TextView tt = (TextView) convertView
.findViewById(R.id.label_server);
if (tt != null) {
tt.setText(s);
}
}
}
return convertView;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}