简体   繁体   中英

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<ConversationThread> ListOfActiveThreads = new ArrayList<ConversationThread>();
ListView convoList;
ArrayAdapter<String> adapter;
ArrayList<String> ActiveConversations = new ArrayList<String>();

@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<String>(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.

The short answer is, you don't. Use ListFragments for your lists and switch between them in onItemClick. This enables you to implement interfaces to 'talk' between your activity and fragments (or fragment to fragment). To get the network operations or any intensive processing off the UI thread, use a Loader . When the loader finishes, call the interface callbacks you implemented on both the Activity and Fragments to pass the results where they're needed.

Take a look at the Activity and Fragment tutorials to get a better idea of how/when/best practices. It can be confusing for new devs to grok some of the subtle differences.

Here's the tutorial on Fragments . This will give you implementation samples of what I've mentioned above:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM