ANDROID:当我单击列表项1时,列表项2正在打开 [英] ANDROID : when I click list item 1 , list item 2 is openning

查看:61
本文介绍了ANDROID:当我单击列表项1时,列表项2正在打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从解析器获取数据到ListView,并且添加了2个过滤器,一个过滤器按首字母搜索,另一个输入名称.

I'm getting data from parse to a ListView and I have added 2 filters, one is search by first letter other is typing the name .

当我单击第一个列表项视图时,第二个列表项正在打开 .通常,当我单击第一个列表项时,应该打开单个页面 如果有人知道请愿帮助

when I click the first list item view 2nd list item is opening .normally when I click first list item it's single page should open if anyone knows pleas help

java文件

import android.content.*;
import android.os.Bundle;
import android.support.v4.app.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class Individuals extends android.support.v4.app.ListFragment
        implements FindCallback<ParseObject> {

    private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
    SearchView sv;
    View mRootView;
    ArrayList<HashMap> mListHeader;
    IndividualsAdaptor adaptor;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_individuals, container, false);
        return mRootView;
    }
    @Override
    public void onViewCreated(View view, Bundle b) {
        super.onViewCreated(view, b);
        sv = (SearchView) view.findViewById(R.id.ser1);

        ListView listView = getListView();
        ImageView mListHeader = new ImageView(getContext());
        mListHeader.setImageResource(R.drawable.individuals_img);
        listView.addHeaderView(mListHeader);

        adaptor = new IndividualsAdaptor(getActivity(), mOrganization);
        setListAdapter(adaptor);
        ParseQuery.getQuery("_User").findInBackground(this);

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
          public boolean onQueryTextSubmit(String text) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String text) {
                adaptor.getFilter().filter(text);
               return true;
            }
        });
    }
    @Override
    public void done(List<ParseObject> scoreList, ParseException e) {
        if (e == null) {
            Log.d("score", "Retrieved " + scoreList.size() + " _User");
            mOrganization.clear();
            mOrganization.addAll(scoreList);

            Collections.sort(mOrganization, new Comparator<ParseObject>() {
                @Override
                public int compare(ParseObject t0, ParseObject t1) {
                    return t0.getString("lastname").toLowerCase().compareTo(t1.getString("lastname").toLowerCase());
                }
            });

            // Create Section Header Position List.
            mListHeader = new ArrayList<>();
            String strHeader = "";
            for(int i=0; i<mOrganization.size(); i++){
                if(strHeader.equals("")||(!strHeader.equals(String.valueOf(mOrganization.get(i).getString("lastname").charAt(0))))){
                    HashMap<String, String> headerItem = new HashMap<>();
//                  ParseObject tmpObject = mOrganization.get(i);
                    strHeader = String.valueOf(mOrganization.get(i).getString("lastname").charAt(0));
                    headerItem.put("alphabet", strHeader);
                    headerItem.put("position", String.valueOf(i));
                    mListHeader.add(headerItem);
                }
            }
            final String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                    "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
            ListView listIndex = (ListView) mRootView.findViewById(R.id.listIndex);
            BaseAdapter indexAdapter = new BaseAdapter() {
                @Override
                public int getCount() {
                    return alphabet.length;
                }
                @Override
                public Object getItem(int position) {
                    return alphabet[position];
                }
                @Override
                public long getItemId(int i) {
                    return 0;
                }
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    TextView tvIndex;
                    if(convertView == null){
                        tvIndex = new TextView(getContext());
                        tvIndex.setTextSize(14);
                    } else {
                        tvIndex = (TextView) convertView;
                    }
                    String currentIndex = getItem(position).toString();
                    tvIndex.setText(currentIndex);
                    tvIndex.setEnabled(false);
                    for(int i=0; i<mListHeader.size(); i++){
                        if(currentIndex.equals(mListHeader.get(i).get("alphabet"))){
                            tvIndex.setEnabled(true);
                            break;
                        }
                    }
                    return tvIndex;
                }
            };
            listIndex.setAdapter(indexAdapter);
            listIndex.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    TextView tv = (TextView) view;
                    int listPosition = 0;
                    for(int i=0; i<mListHeader.size(); i++){
                        if(tv.getText().equals(mListHeader.get(i).get("alphabet"))){
                            listPosition = i;
                            break;
                        }
                    }
                    listPosition = Integer.parseInt((String)mListHeader.get(listPosition).get("position"));
                    ListView listView = getListView();
                    listView.setSelection(listPosition);
                }
            });

            ((IndividualsAdaptor) getListAdapter()).updateBackupList(mOrganization);
            ((IndividualsAdaptor) getListAdapter()).updateHeaderList(mListHeader);
            ((IndividualsAdaptor) getListAdapter()).notifyDataSetChanged();
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
    @Override
    public void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ParseObject prs = mOrganization.get(position);
        String objectId = prs.getObjectId();
        Intent i = new Intent(getActivity(), SingleItemView.class);
        i.putExtra("objectId", objectId);
        i.putExtra("image", objectId);
        startActivity(i);
    }
}

xml文件

xml file

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <RelativeLayout
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="110dp"
    />


    <TextView android:id="@+id/tvEmpty"
              android:layout_width="match_parent"
              android:layout_height="60dp"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:background="#083266"/>

   <SearchView
        android:id="@+id/ser1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search.."
        android:background="@color/FBC_RED"
        android:layout_below="@id/tvEmpty"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="0dp">
    </SearchView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_below="@+id/ser1">

        <ListView
            android:id="@id/android:list"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.89" />

        <ListView
            android:id="@+id/listIndex"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:layout_weight="0.08">
        </ListView>


    </LinearLayout>
</LinearLayout>

推荐答案

因为适配器上的位置为您提供一个,而不是0.因此,在onItemClickListener中尝试:

Because the position on the adapter gives you one, and not 0. So in the onItemClickListener try:

ParseObject prs = mOrganization.get(position - 1);

这篇关于ANDROID:当我单击列表项1时,列表项2正在打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆