android - 几个 TextViews,使用自定义数组适配器

标签 android android-listview android-arrayadapter

我对整个 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;
}

当我从主要 Activity 中调用它时,它是这样完成的:

    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>

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

最佳答案

请尝试 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;
}

关于android - 几个 TextViews,使用自定义数组适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19319606/

相关文章:

android - 不同的分辨率与 dpi

android - 在 ListView 的每个项目中显示 ProgressBar

java - Dagger 2 注入(inject)没有模块的单例

java - Android 的动态文本更新 - ViewRootImpl$CalledFromWrongThreadException

java - 如何处理android中 ListView 项中的复选框事件

android - 在垂直 ListView 中同步水平 RecyclerView

java - 如何获取HashMap的第一个元素

java - Android ListView - 获取选中的项目

android - 在 Activity 中设置 reclerview 时出现字节分配问题

java - 根据 Android 中的微调器选择更新 ListView