更改默认的listViews项目,而是单击“StringBuffer”(默认情况下显示弹出窗口)

时间:2016-05-02 11:24:50

标签: android listview

我有一个显示项目列表的ListView,我希望在使用StringBuffer点击任何项目时显示更多详细信息。但是我遇到了问题,因为android默认setOnItemClickListener会绕过我在click事件中定义的StringBuffer视图,只是在弹出窗口中显示项目视图。

这是我的代码,我将包含一些屏幕截图,以便更多地阐明我的观点

import android.app.AlertDialog;
import android.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


public class SummerJobsFragmnet extends Fragment {

    public SummerJobsFragmnet() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static Fragment getInstance() {
        Fragment fragment = new SummerJobsFragmnet();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    public void showMessage (String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final MainActivity activity = (MainActivity) getActivity();
        String [] places = activity.getAllPositionsNamesPhone().toArray(
                new String[activity.getAllPositionsNamesPhone().size()]);

        final ListView list = (ListView) getView().findViewById(R.id.joblistView);
        int  prgmImages=R.mipmap.ic_launcher;
        list.setAdapter(new CustomListAdapter(activity,places,prgmImages));

        // OnClick listner for the individual cells of the listView

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


                // get the IDs of each column in the database
                String [] IDs = activity.getAllColumnsIDs().toArray(
                        new String[activity.getAllColumnsIDs().size()]);

                // loop throught the IDs and see if they match the listView
                // item ids. if yes, display the detail
                DataBaseHelper summerJobDB;
                summerJobDB = new DataBaseHelper(activity);
                for (int i = 0; i >= IDs.length; i++) {
                    if (i == id) {

                        Cursor res = summerJobDB.getAllData();
                        if (res.getCount() ==0){
                            // show some message
                            showMessage("Error", "Nothing found");
                            return;
                        }
                        StringBuffer buffer = new StringBuffer();
                        // append the values from the database to the buffer.
                        // this is based on the index number of the columns
                        while (res.moveToNext()) {
                            buffer.append(res.getString(1)+ "/" + res.getString(2) +"\n");
                            buffer.append("Starts on " + res.getString(3)+"\n\n");
                            buffer.append("Address: \n"+ res.getString(5)+"\n\n");
                            buffer.append("Phone: \n"+ res.getString(6)+"\n\n");
                            buffer.append("Hours: \n"+ res.getString(4)+"\n\n");
                        }
                        // show all data here
                        showMessage("Data",buffer.toString());

                    }
                }

            }
        });

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.displayjobs, container, false);
    }
}

,这是CustomListAdapter.java

import android.widget.BaseAdapter;

        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.TextView;
        import android.widget.Toast;

public class CustomListAdapter extends BaseAdapter {
    String [] result;
    Context context;
    int  imageId;
    private static LayoutInflater inflater=null;
    public CustomListAdapter(MainActivity mainActivity, String[] places, int prgmImages) {
        // TODO Auto-generated constructor stub
        result=places;
        context=mainActivity;
        imageId=prgmImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.display_job_cell_view, null);
        holder.tv=(TextView) rowView.findViewById(R.id.Itemname);
        holder.img=(ImageView) rowView.findViewById(R.id.icon);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId);
        /*
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });
        */
        return rowView;
    }

}

第一张图片 Here is what I get

here is the logcat error after making the modifications

1 个答案:

答案 0 :(得分:0)

您已在list-items(rowView.setOnClickListener)上定义了onClickListener - 这是调用而不是listView的setOnItemClickListener。您需要在CustomListAdapter类中删除(注释掉):

rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });

您还需要致电listView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);这样,即使列表的行项设置为focusableclickable,也只能调用listView的项目单击侦听器。 然后对话框应显示您所期望的。请尝试这一点,让我们知道它是否有效或你得到了什么错误。