Witam,
ostatnio zacząłem pisać w androidzie (także uprzedzam, że w kodzie mogą być błędy, ale w końcu od czegoś trzeba zacząć) i natrafiłem na 2 dziwne problemy, otóż gdy z EditText chcę pobrać wartość getText().toString() to string jest pusty "", chociaż nie powinien oraz w listview nie dodają się żadne elementy (chociaż w samej liście w debuggerze już tak). Ktoś wie o co może chodzić? Proszę o jakieś wskazówki, gdyż przeglądnąłem już kilka stron w googlu i dalej nie mam pomysłu :)
layout_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:weightSum="1"
android:paddingTop="20dp">
<Button
android:layout_width="75dp"
android:layout_height="100dp"
android:text="@string/send_button"
android:id="@+id/sendButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onSendButtonClick" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewMessages"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/sendButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/messageBox"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton"
android:layout_above="@+id/listViewMessages"
android:layout_alignTop="@+id/sendButton"
android:inputType="text" />
</RelativeLayout>
layout_for_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_height="match_parent"
android:layout_width="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
activity:
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.InputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SliderActivity extends AppCompatActivity {
private int[] layouts;
private Thread thread;
private Socket socket;
private PreferencesService preferencesService;
private PacketWorker packetWorker;
private PacketService packetService;
private AlertService alertService;
private MessageListService listService;
private static EditText sendBox;
private static ListView listViewMessages;
private MessagesListAdapter adapter;
private List<Message> listMessages;
private String nick;
private View inflatedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
inflatedView = getLayoutInflater().inflate(R.layout.layout_chat, null, false);
listViewMessages = (ListView) inflatedView.findViewById(R.id.listViewMessages);
sendBox = (EditText) inflatedView.findViewById(R.id.messageBox);
setContentView(R.layout.layout_for_slide);
thread = new Thread(new ClientThread());
thread.start();
listMessages = new ArrayList<>();
adapter = new MessagesListAdapter(getApplicationContext(), listMessages);
listViewMessages.setAdapter(adapter);
preferencesService = new PreferencesService(this);
packetWorker = new PacketWorker();
packetService = new PacketService();
alertService = new AlertService(this);
listService = new MessageListService(adapter);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
layouts = new int[]{
R.layout.layout_chat,
R.layout.layout_friends};
MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
nick = getIntent().getStringExtra("nick");
}
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public MyViewPagerAdapter() {
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(layouts[position], container, false);
container.addView(view);
return view;
}
@Override
public int getCount() {
return layouts.length;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}
public void onSendButtonClick(View view) {
sendBox = (EditText) inflatedView.findViewById(R.id.messageBox);
String message = sendBox.getText().toString();
if(message.isEmpty()) {
Toast.makeText(getApplicationContext(),
"Nie możesz wysłać pustej wiadomości!",
Toast.LENGTH_SHORT).show();
return;
}
listService.addToList(listMessages, new Message(nick, message, MessageType.YOU));
}
class ClientThread implements Runnable {
//... tu pominę kod wątku nasłuchującego przychodzące pakiety
}
MessagesListAdapter.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MessagesListAdapter extends BaseAdapter {
private Context context;
private List<Message> messagesItems;
public MessagesListAdapter(Context context, List<Message> navDrawerItems) {
this.context = context;
this.messagesItems = navDrawerItems;
}
@Override
public int getCount() {
return messagesItems.size();
}
@Override
public Object getItem(int position) {
return messagesItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message m = messagesItems.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (messagesItems.get(position).isUser() == MessageType.YOU) {
convertView = mInflater.inflate(R.layout.list_item_message_right,
null);
} else if(messagesItems.get(position).isUser() == MessageType.FRIEND){
convertView = mInflater.inflate(R.layout.list_item_message_center,
null);
} else {
convertView = mInflater.inflate(R.layout.list_item_message_left,
null);
}
TextView lblFrom = (TextView) convertView.findViewById(R.id.lblMsgFrom);
TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
txtMsg.setText(m.getMessage());
lblFrom.setText(m.getFromName());
return convertView;
}
}
MessageListService.java
import java.util.List;
public class MessageListService {
private MessagesListAdapter adapter;
public MessageListService(MessagesListAdapter adapter) {
this.adapter = adapter;
}
public void addToList(List<Message> list, Message message) {
if(list.size() > 100) {
list.remove(0);
}
list.add(message);
adapter.notifyDataSetChanged();
}
}
Z góry dziękuję za pomoc ;)
Edit:
Doszedłem do etapu, że jeśli ustawie ContentView na layout_chat ( setContentView(R.layout.layout_chat); ) to wtedy wszystko dobrze działa, jednak ja muszę mieć ContentView na layout_for_slide...