gpt4 book ai didi

android - 尝试使用 Fragment 创建聊天?

转载 作者:行者123 更新时间:2023-11-29 15:57:04 25 4
gpt4 key购买 nike

我正在尝试使用 Fragment 创建聊天。为此,我在谷歌上找到了一个例子。该项目使用 Activity,我正在尝试使用 Fragment,因为我的项目都是使用 Fragments 开发的。当我从 Activity 更改为 Fragment 这一行时:adapter = new DiscussArrayAdapter(getView().getContext(), R.layout.listitem_discuss); 抛出异常

这是我的尝试

XML

///activity_discuss
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp">
</ListView>

<RelativeLayout
android:id="@+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/chatText"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/buttonSend" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/buttonSend"
android:layout_alignBottom="@+id/chatText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

</RelativeLayout>


///listitem_discuss
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/bubble_yellow"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>

</LinearLayout>

fragment

public class HelloBubblesActivity extends Fragment {
private DiscussArrayAdapter adapter;
private ListView lv;
private LoremIpsum ipsum;
private EditText editText1;
private Button btnSend;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_discuss, container, false);
lv = (ListView)getView().findViewById(R.id.listView1);

adapter = new DiscussArrayAdapter(getView().getContext(), R.layout.listitem_discuss);
lv.setAdapter(adapter);

editText1 = (EditText)view.findViewById(R.id.chatText);
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
//adapter.add(new OneComment(false, editText1.getText().toString()));
receiveMessage();
editText1.setText("");
return true;
}
return false;
}
});

btnSend = (Button)view.findViewById(R.id.buttonSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//adapter.add(new OneComment(false, editText1.getText().toString()));
receiveMessage();
editText1.setText("");
}
});

return view;
}




private void receiveMessage(){
String msg = editText1.getText().toString();
new ChatDAO().receiveMessage("");
}


/** recebe msg */
private void addItems() {
adapter.add(new OneComment(true, "Hello bubbles!"));
}

}

适配器

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

private TextView countryName;
private List<OneComment> countries = new ArrayList<OneComment>();
private LinearLayout wrapper;

public DiscussArrayAdapter(Context context, int resource) {
super(context, resource);
}

@Override
public void add(OneComment object) {
countries.add(object);
super.add(object);
}



public int getCount() {
return this.countries.size();
}

public OneComment getItem(int index) {
return this.countries.get(index);
}

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}

wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

OneComment coment = getItem(position);

countryName = (TextView) row.findViewById(R.id.comment);

countryName.setText(coment.comment);

countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

return row;
}

public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

}

最佳答案

此时在您的代码中,getView() 将返回 null,因为从 onCreateView() 方法返回的 View 被设置为 Fragment 的看法。使用 getActivity() 而不是 getView().getContext() 来获取必要的上下文。

关于android - 尝试使用 Fragment 创建聊天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com