Sjubussen
Sjubussen

Reputation: 51

Listview and adapter for dummies

I seen through all previous question about this topic but cannot get my head around it.It is tested on a Samsung Galaxy S4. This is from my activity_main.xml.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/llListe"
    android:orientation="vertical"
    >
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lstListe"
        android:layout_gravity="center"
        android:background="#bcffb9" />
</LinearLayout>

This is the MainActivity.java. I have tested the result from the speechRecognizer and it return the spoken word. The problem is to add this string to a ListView. I'm quit new at this so please forgive. WHAT AM I DOING WRONG?

public class MainActivity extends Activity implements View.OnClickListener {

protected static final int REQUEST_OK = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.btnSpeek).setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en-US");
    try {
        startActivityForResult(intent,REQUEST_OK);
    } catch (Exception e) {
        Toast.makeText(this, "Unable to initialize speech engine.", Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ListView lv;
    ArrayList<String> inputString =  new ArrayList<String>();
    ArrayAdapter<String> adapter;
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
        inputString = (data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS));
        //Toast.makeText(this,"You said "+inputString.get(0), Toast.LENGTH_SHORT).show();
        lv = (ListView) findViewById(R.id.lstListe);
        adapter = new ArrayAdapter(this,R.id.lstListe,inputString);
        lv.setAdapter(adapter);

    }
}

}

Upvotes: 0

Views: 427

Answers (1)

Caroline
Caroline

Reputation: 406

I think you should put this line super.onActivityResult(requestCode, resultCode, data); in the else condition of the onActivityResult method, so that you're sure your code is executed when the request code is REQUEST_OK.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ListView lv;
    ArrayList<String> inputString =  new ArrayList<String>();
    ArrayAdapter<String> adapter;

    if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
        inputString = (data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS));
        //Toast.makeText(this,"You said "+inputString.get(0), Toast.LENGTH_SHORT).show();
        lv = (ListView) findViewById(R.id.lstListe);
        adapter = new ArrayAdapter(this,R.id.lstListe,inputString);
        lv.setAdapter(adapter);
    } 
    else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Upvotes: 1

Related Questions