我想在ListView中单击该项。我在网上搜索了很多,找到了一些解决方案,其中使用扩展适配器类创建了一个新类,然后调用了getView()方法,但对我来说不起作用,因为我正在从数据库中填充数据。
我希望我的文本视图也可以点击,但我卡在按钮上,所以如果你能帮助我,然后请帮助我的可点击按钮和文本视图。
这是我的listview xml,onclick=“navigatetomap”

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"
        android:orientation="vertical" >
    <TextView
        android:id="@+id/txtbbName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="38dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtbbaddr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtloc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtbbName"
        android:layout_marginRight="38dp"
        android:layout_marginTop="48dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtbbaddr"
        android:layout_marginTop="22dp"
        android:text="Navigate"
        android:onClick="navigateToMap"/>


    </LinearLayout>

</RelativeLayout>

此“我的活动”正在加载从其他活动按下的按钮。
我刚刚试图在navigateTomap()代码中获取textView的值,当我单击它时,它给了我一个toast,但显示了错误的值(上面列表项的值)。
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BloodBank extends Activity {
    ListView bloodbanklst;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blood_bank);

         String[] arrayColumns = new String[]{"name","addr","longitude","latitude","location","ph_no1","ph_no2"};
         int[] arrayViewIDs = new int[]{R.id.txtbbName,R.id.txtbbaddr,R.id.txtloc};
         Context context;
         bloodbanklst=(ListView) findViewById(R.id.bloodbankList);



         DataBaseHelper myDbHelper;
         myDbHelper = new DataBaseHelper(this);
         SQLiteDatabase db=myDbHelper.getReadableDatabase();
    Cursor c=db.rawQuery("select * from bloodBank", null);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.blooddisplaylist,c,arrayColumns, arrayViewIDs);
    bloodbanklst.setAdapter(adapter);





    }

    public void navigateToMap(View view){
        TextView txt1=(TextView) findViewByIdR.id.txtloc);
        String abc = (String) txt1.getText();
        Toast.makeText(this,"Value from the text"+ abc,Toast.LENGTH_LONG).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.blood_bank, menu);
        return true;
    }

}

最佳答案

您正试图从XML中指定的TextView中使用getText()获取文本。虽然对于布局来说这很好,但是一个ListView将有许多甚至数百个相同的文本视图,因为您将为每个列表项对该视图进行膨胀。NavigateToMap()不知道要从哪个文本视图中提取。
您应该使用一个onitemclicklistener来列出单击并调用带有视图的navigatetomap
被点击了
像这样的:

 item.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               navigateToMap(view)   // this passes the view you inflated and filled
                                     //with data, bot just the XML that defines
                                     //what it looks like

            }
     });

然后
 public void navigateToMap(View view){
    TextView txt1=(TextView) view.getChildAt(some index)   // find which child first
    String abc = (String) txt1.getText();
    Toast.makeText(this,"Value from the text"+ abc,Toast.LENGTH_LONG).show();
}

10-06 09:43