4

我想知道如何从单击的列表视图的项目中获取字符串。例如,我有一个这种格式的条目。

# - 约翰·史密斯

john@gmail.com

3451234532

纽约

我想得到约翰史密斯。我可以在那个位置拿到物品,但我不知道其余的。

我的代码:

package com.example.deneme;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class Activity2 extends Activity {

private SQLiteAdapter mySQLiteAdapter;
 ListView listContent;
 SimpleCursorAdapter cursorAdapter;
 Cursor cursor;
 Button buttonDeleteAll,buttonDeleteRow;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);


buttonDeleteAll = (Button)findViewById(R.id.deleteall);
buttonDeleteRow = (Button)findViewById(R.id.deleterow); 
listContent = (ListView)findViewById(R.id.contentlist);
listContent.setClickable(true);
buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener);
buttonDeleteRow.setOnClickListener(buttonDeleteRowOnClickListener);


mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();



cursor = mySQLiteAdapter.queueAll();
String[] from = new String[]{ SQLiteAdapter.COLUMN_NAME, SQLiteAdapter.COLUMN_EMAIL,SQLiteAdapter.COLUMN_PHONE, SQLiteAdapter.COLUMN_ADDRESS};

int[] to = new int[]{R.id.text1, R.id.text2,R.id.text3, R.id.text4};

cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);



listContent.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        String str = ((TextView)arg1).getText().toString();
        Toast.makeText(Activity2.this, str, Toast.LENGTH_SHORT).show();
    }
});



}


   Button.OnClickListener buttonDeleteRowOnClickListener = new Button.OnClickListener(){

       public void onClick(View arg0) {

     //    String n = inputContent1.getText().toString();
     //    mySQLiteAdapter.deleteRow(n);
           updateList();
           Toast.makeText(Activity2.this, "Entry Deleted", Toast.LENGTH_SHORT).show();      


       }
        };

        Button.OnClickListener buttonDeleteAllOnClickListener
        = new Button.OnClickListener(){
       public void onClick(View arg0) {

        mySQLiteAdapter.deleteAll();
        updateList();
        Toast.makeText(Activity2.this, "All Entries Deleted", Toast.LENGTH_SHORT).show();      

       }
        };

        private void updateList(){
              cursor.requery();
               }
}

使用你的方式后,我得到了这些错误:

07-27 07:52:15.726: E/AndroidRuntime(856): FATAL EXCEPTION: main
07-27 07:52:15.726: E/AndroidRuntime(856): java.lang.ClassCastException:              android.widget.LinearLayout
07-27 07:52:15.726: E/AndroidRuntime(856):  at  com.example.deneme.Activity2$3.onItemClick(Activity2.java:58)
07-27 07:52:15.726: E/AndroidRuntime(856):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-27 07:52:15.726: E/AndroidRuntime(856):  at android.widget.ListView.performItemClick(ListView.java:3513)
07-27 07:52:15.726: E/AndroidRuntime(856):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
4

3 回答 3

3

好吧,这取决于您在列表视图中填充了什么。如果这些是 TextView,那么您所要做的就是:

((TextView)arg1).getText().toString()

本质View arg1上是点击的视图。鉴于您已经填充了该列表,您知道该视图是什么,因为您是创建并返回它的人(在您的适配器中):

public View getView(int position, View convertView, ViewGroup parent)

因此,无论该视图的类型如何,请检查其文档以了解如何从中获取文本

编辑:

好吧,您正在使用的光标适配器为每一行返回一个 LinearLayout。它有几个 TextView,每个选定的列都有一个。要获得第一列,请执行以下操作:

LinearLayout row = (LinearLayout)((LinearLayout)arg1).getChildAt(0);
TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();

如果您想要不同的列,只需将参数更改为getChildAt(i)

于 2012-07-27T07:49:49.880 回答
3
listContent.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        Cursor cursor = (Cursor) parent.getItemAtPosition(position);
        String name = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.COLUMN_EMAIL));
        Toast.makeText(Activity2.this, name, Toast.LENGTH_SHORT).show();
    }
});
于 2012-07-27T08:23:13.787 回答
0

要获取文本,只需使用

String item = ((TextView)arg1).getText().toString();

另一方面,如果您将实现的函数的参数重命名为:

public void onItemClick(AdapterView<?> parent, View view, int position,
                long id)

现在您可以清楚地看到每个参数的作用。

于 2012-07-27T07:48:58.913 回答