我从解析到ListView获取数据,我添加了2个过滤器,一个是按首字母搜索,另一个是输入名称。
当我点击第一个列表项目视图时,第二个列表项目正在打开 。当我点击第一个列表项时,它的单页应该打开 如果有人知道请求帮助
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文件
<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 :(得分:2)
因为适配器上的位置给你一个,而不是0.所以在onItemClickListener
尝试:
ParseObject prs = mOrganization.get(position - 1);