Listview Onitemclick什么都没发生

时间:2015-05-05 13:15:44

标签: android android-listview

这是我的代码

public class VorherbestimmungenActivity extends Activity {
private ListView lv1;
ArrayAdapter<String> adapter;
EditText inputSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ThemeUtils.onActivityCreateSetTheme(this);
    setContentView(R.layout.vorherbestimmung);

String products[] = getResources().getStringArray(R.array.vorherbestimmung);
    lv1 = (ListView) findViewById(R.id.listView1);
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    adapter = new ArrayAdapter < String > (this, R.layout.list_item, R.id.product_name, products);

    lv1.setAdapter(adapter);
    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            VorherbestimmungenActivity.this.adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub


    lv1.setOnItemClickListener(new OnItemClickListener() { @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                  int position, long arg3) {

            if (position == 0) {
                Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                String shareMessage = "This is Item1";
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareMessage);
                startActivity(Intent.createChooser(shareIntent, "Entscheiden Sie �ber was Sie teilen m�chten:"));
            } else if (position == 1) {
                Intent shareIntent1 = new Intent(android.content.Intent.ACTION_SEND);
                shareIntent1.setType("text/plain");
                String shareMessage1 = "This is Item2";
                shareIntent1.putExtra(android.content.Intent.EXTRA_TEXT,shareMessage1);
                startActivity(Intent.createChooser(shareIntent1, "Entscheiden Sie �ber was Sie teilen m�chten:"));
            }
                        }

            });
        }
    });
}

}

所以我的问题是,如果我点击该项目没有任何反应。他们不分享这个消息。 如果我将name.equals更改为position它是可行的,但是它们会显示错误的共享文本。 它只有在我搜索该项目时才有效。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

更改

String name = adapter.getItem(position);

<强>与

String name = (String)arg0.getItemAtPosition(position);

答案 1 :(得分:1)

删除代码:

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub 

} 

并将@Override添加到您的public void onItemClick()