0

我对整个 android/java 很陌生,今天让我有点难过。我知道以前有很多关于这个主题的问题,但到目前为止我尝试过的任何东西似乎都不起作用,而且我的应用程序一直在崩溃:/

我要做的是用两个不同的 textView 填充 listView,具体取决于消息的来源。我的 LogCat 错误是“您必须为 TextView 提供资源 ID”。

我的代码在下面看到;

行.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>

行适配器.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);
    }
        }

主要的.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>

真的很抱歉这么长的帖子,只是觉得最好给你完整的信息背景:)

4

1 回答 1

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;
}
于 2013-10-11T19:06:32.813 回答