Thursday, April 25, 2013

[android help] Execute a thread of an activity based on item position clicked in ListView


Okay, here is the the problem I am having. I am developing a simple messaging client similar to the OEM one found on Android devices.


I currently have a ConversationList.java containing a ListView with the usernames of people the actual user is engaged in conversation with.


I want to launch specific threads of a ConversationThread.java (which is an activity that contains another ListView holding the messages exchanged between users).


I thought originally about instantiating a new ConversationThread upon each addition of a conversation in ConversationList.java, then adding that to an array list. Then based on the position parameter on the onItemClick on the ListView referenced to the position of the ArrayList of ConversationThread execute that particular "thread" of ConversationThread.



public class ConversationList extends Activity
{
ArrayList ListOfActiveThreads = new ArrayList();
ListView convoList;
ArrayAdapter adapter;
ArrayList ActiveConversations = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_list);
convoList = (ListView) findViewById( R.id.Conversations );
Button addConvo = (Button) findViewById(R.id.Talk);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ActiveConversations);
convoList.setAdapter(adapter);

addConvo.setOnClickListener(new OnClickListener()
{
public void onClick(View v) //will add the person to be sending to to the list of active conversations
{
EditText person2add = (EditText) findViewById(R.id.Friend2Sendto);
String person = person2add.getText().toString();
ConversationThread newMessage = new ComversationThread(person);
ListOfActiveThreads.add(newMessage);
adapter.add(person);
adapter.notifyDataSetChanged();
person2add.setText("");

}
});

convoList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView a, View v, int i, long l)
{

Intent mainIntent = new Intent(ConversationList.this, ConversationThread.class); //this starts a new instance of the ConversationThread class each time
startActivity(mainIntent);
/* Here I want to start or resume the ConversationThread.getposition(i) */
}
});

}


My question is, for the ConversationList.java I have a basic android activity template, with an oncreate() class etc. Do I make this class implement Runnable and somehow make it act like a Thread in Java? or is there a better way of creating a list of running activities to be utilized by the ListView.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...