Android:ListView单击事件无法使用SimpleAdapter工作

Android:ListView单击事件无法使用SimpleAdapter工作,android,listview,simpleadapter,Android,Listview,Simpleadapter,在我的应用程序中,我试图在单击列表项时将TextView设置为删除线。但Listview剪辑项不起作用 代码如下: listView = ( ListView ) findViewById(R.id.listview); SimpleAdapter adapter = new SimpleAdapter( this, aList, R.layout.add_list_detail, new String[] { "txt", "cur" }

在我的应用程序中,我试图在单击列表项时将TextView设置为删除线。但Listview剪辑项不起作用

代码如下:

    listView = ( ListView ) findViewById(R.id.listview);
    SimpleAdapter adapter = new SimpleAdapter( this, aList, R.layout.add_list_detail, 
                   new String[] { "txt", "cur" }, new int[] { R.id.txt, R.id.cur } );
    // Setting the adapter to the listView
    listView.setAdapter(adapter);


    Button btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent details = new  Intent(AddNewList.this, AddNewGrocery.class); // Call
            startActivityForResult(details, 1);
        }
       });
    listView.setClickable(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

           // Object o = lv.getItemAtPosition(position);
            /* write you handling code like...
            String st = "sdcard/";
            File f = new File(st+o.toString());
            // do whatever u want to do with 'f' File object
            */  

              TextView item = (TextView) arg1.findViewById(R.id.txt);
              item.setPaintFlags(item.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
          }
        });


         protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                Bundle b = data.getExtras();
                if ( b!= null ){
                String strName=data.getStringExtra("name");
                String strQty=data.getStringExtra("quantity");
                System.out.println(strName);
                System.out.println(strQty);





                SimpleAdapter adapter = new SimpleAdapter( this, aList, R.layout.add_list_detail, 
                        new String[] { "txt", "cur" }, new int[] { R.id.txt, R.id.cur } );

                    HashMap<String, String> hm = new HashMap<String,String>();

                    hm.put("txt", strName);
                    hm.put("cur",strQty);
                    aList.add(hm);

                // Keys used in Hashmap
                String[] from = {"txt","cur" };

                // Ids of views in listview_layout
                int[] to = {R.id.txt,R.id.cur};


                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

                }
            }
            if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }
listView=(listView)findViewById(R.id.listView);
simpledapter adapter=新的simpledapter(this,aList,R.layout.add\u list\u detail,
新字符串[]{“txt”,“cur”},新int[]{R.id.txt,R.id.cur});
//将适配器设置为listView
setAdapter(适配器);
按钮btnAdd=(按钮)findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent details=newintent(AddNewList.this,addnewgery.class);//调用
startActivityForResult(详情,1);
}
});
listView.setClickable(真);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(适配器视图arg0、视图arg1、内部位置、长arg3){
//对象o=lv.getItemAtPosition(位置);
/*写你处理代码像。。。
字符串st=“sdcard/”;
文件f=新文件(st+o.toString());
//对“f”文件对象执行任何操作
*/  
TextView项=(TextView)arg1.findViewById(R.id.txt);
item.setPaintFlags(item.getPaintFlags()| Paint.STRIKE_-THRU_-TEXT_-FLAG);
}
});
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==1){
if(resultCode==RESULT\u OK){
Bundle b=data.getExtras();
如果(b!=null){
字符串strName=data.getStringExtra(“名称”);
字符串strQty=data.getStringExtra(“数量”);
System.out.println(strName);
系统输出打印项次(strQty);
simpledapter adapter=新的simpledapter(this,aList,R.layout.add\u list\u detail,
新字符串[]{“txt”,“cur”},新int[]{R.id.txt,R.id.cur});
HashMap hm=新的HashMap();
hm.put(“txt”,strName);
hm.put(“当前”,strQty);
添加(hm);
//Hashmap中使用的键
字符串[]from={“txt”,“cur”};
//listview\u布局中的视图ID
int[]to={R.id.txt,R.id.cur};
setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
if(resultCode==RESULT\u取消){
//如果没有结果,请编写代码
}
}
}

如果在列表视图中使用任何布局,请在xml中使用该布局

android:genderantfocusability=“blocksDescendants”

或以编程方式
mListView.setgroundantfocusability(ListView.FOCUS\u BLOCK\u子体)


在列表中的第一个父布局中。这就像魔术一样,点击将不会被列表中的任何元素所消耗,而是直接进入列表项。

我在您的代码中没有发现问题,但我向您发布了完整的代码,该代码有效:

MainActivity.java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    ListView listView;
    Activity activity;

    ArrayList<Map<String, String>> aList;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;
        aList = new ArrayList<Map<String, String>>();
        String[] from = { "name", "quantity" };
        int[] to = { R.id.txt, R.id.cur };
        listView = (ListView) findViewById(R.id.add_list_detail);
        SimpleAdapter adapter = new SimpleAdapter(this, aList,
                R.layout.add_list_detail, from, to);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                TextView item = (TextView) arg1.findViewById(R.id.txt);
                item.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            }
        });

        Button btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent details = new Intent(activity, Test.class); // Call
                startActivityForResult(details, 1);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                Bundle b = data.getExtras();
                if (b != null) {
                    String strName = data.getStringExtra("name");
                    String strQty = data.getStringExtra("quantity");
                    System.out.println(strName);
                    System.out.println(strQty);

                    SimpleAdapter adapter = new SimpleAdapter(this, aList,
                            R.layout.add_list_detail, new String[] { "txt",
                                    "cur" }, new int[] { R.id.txt, R.id.cur });

                    HashMap<String, String> hm = new HashMap<String, String>();

                    hm.put("txt", strName);
                    hm.put("cur", strQty);
                    aList.add(hm);

                    // Keys used in Hashmap
                    String[] from = { "txt", "cur" };

                    // Ids of views in listview_layout
                    int[] to = { R.id.txt, R.id.cur };

                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }
            }
            if (resultCode == RESULT_CANCELED) {
                // Write your code if there's no result
            }
        }
    }

}
添加_list_detail.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mytest.MainActivity"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/cur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mytest.MainActivity" >



    <ListView
        android:id="@+id/add_list_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="153dp" >
    </ListView>

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="40dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

testlayout.xml:

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

    <Button
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


尝试删除
listView.setClickable(true)谢谢你的回复..我试过了,但是McClickListener还不能工作。有什么例外吗?您能发布完整的活动吗?不,没有例外,但我正在将值从第二个活动传递到第一个活动。此时onCreate方法没有调用。下面是第二个活动的代码:Intent returnIntent=newintent();returnIntent.putExtra(“名称”,名称);returnIntent.putExtra(“数量”,数量);setResult(结果正常,返回意图);完成();你能把整个活动的代码贴出来吗?第二次活动怎么开始?您是否使用startActivityForResult?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>