Android 根据listview中未显示的字段筛选listview
我目前有一个应用程序,它从互联网上获取一个json文件,对其进行解析,并从中创建和输入对象。项目具有以下字段:类型(图像、文本)和数据(如果项目类型为图像,则为url;如果项目类型为文本,则为文本字符串) 当应用程序首次加载时,列表显示所有项目的混合,包括文本和图像。如果项目是图像,则仅图像显示在listview的列表项目中,如果是文本,则显示文本 我需要它做的(并且我有一个问题)是我需要它,这样当用户从菜单中选择“仅图像”时,它将仅在listview中显示具有类型“图像”的对象,并隐藏具有类型“文本”的项,如果他们选择“仅文本”,则它将过滤掉所有图像列表项,并仅显示类型为“图像”的项“文本”。如果他们选择“全部”,它将显示默认值,就像应用程序首次加载时一样 我不会在listview的任何位置显示此对象的类型字段,只显示对象的数据字段,该字段将是从url加载的图像或文本。似乎我遇到的每个过滤示例都是当有人键入文本时,它会过滤列表,但该文本显示在列表中,并且在我需要过滤的列表中可见r for在列表中不可见。我不太确定如何完成我需要的。有人有什么想法吗?希望我解释得足够好 编辑:这就是我到目前为止所拥有的。现在,当我从菜单中选择图像时,它会工作;当我从菜单中选择文本时,它会工作;当我切换回“全部”时,它是空的,并且是斜接。大小为零,即使我从未从中删除任何内容 创建时:Android 根据listview中未显示的字段筛选listview,android,listview,object,android-arrayadapter,Android,Listview,Object,Android Arrayadapter,我目前有一个应用程序,它从互联网上获取一个json文件,对其进行解析,并从中创建和输入对象。项目具有以下字段:类型(图像、文本)和数据(如果项目类型为图像,则为url;如果项目类型为文本,则为文本字符串) 当应用程序首次加载时,列表显示所有项目的混合,包括文本和图像。如果项目是图像,则仅图像显示在listview的列表项目中,如果是文本,则显示文本 我需要它做的(并且我有一个问题)是我需要它,这样当用户从菜单中选择“仅图像”时,它将仅在listview中显示具有类型“图像”的对象,并隐藏具有类型
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
tvNoInet=(TextView)findViewById(android.R.id.empty);
setSupportActionBar(toolbar);
if (isNetworkAvailable()) {
getData theJsonData = new getData();
theJsonData.execute();
}
myListView = (ListView) findViewById(R.id.listViewID);
customAdapter = new ListAdapter(MainActivity.this, R.layout.list,mItems);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mItems.get(position).getmType().toString().equals("text")){
if (isNetworkAvailable()) {
Intent in = new Intent(MainActivity.this, MyWebView.class);
startActivity(in);
}
else{
Toast.makeText(getApplicationContext(), "No internet connection, try again later", Toast.LENGTH_SHORT).show();
}
}
if (mItems.get(position).getmType().toString().equals("image")){
Intent in = new Intent(MainActivity.this,FullImage.class);
in.putExtra("imageURL",mItems.get(position).getmData().toString());
startActivity(in);
}
}
});
}
我的任务是获取json,解析它,创建对象,然后将其添加到mItems,如果类型为image,则将其添加到mImageItems,如果类型为text,则将其添加到mTextItems
public class getData extends AsyncTask<String, Void, String> {
String jsonStr = null;
ProgressDialog progress = new ProgressDialog(MainActivity.this);
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL("http://host/data.json");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
jsonStr = buffer.toString();
} catch (IOException e) {
Log.e("MAIN", "Error ", e);
return null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
try {
JSONArray itemsArray = new JSONArray(jsonStr);
String itemID=null;
String itemType=null;
String itemDate=null;
String itemData=null;
for (int i = 0; i < itemsArray.length(); i++) {
JSONObject jsonItem=itemsArray.getJSONObject(i);
if (jsonItem.has("id")){
itemID=jsonItem.getString("id");
}
if (jsonItem.has("type")){
itemType=jsonItem.getString("type");
}
if (jsonItem.has("date")){
itemDate=jsonItem.getString("date");
}
if (jsonItem.has("data")){
itemData=jsonItem.getString("data");
}
Item myItem=new Item(itemID,itemType,itemDate,itemData);
mItems.add(myItem);
if (itemType.equals("image")){
mImageItems.add(myItem);
}
else if (itemType.equals("text")){
mTextItems.add(myItem);
}
}
Log.e("ALL SIZE: ", String.valueOf(mItems.size()));
Log.e("TEXT SIZE: ", String.valueOf(mTextItems.size()));
Log.e("IMAGE SIZE: ", String.valueOf(mImageItems.size()));
} catch (JSONException jsone) {
jsone.printStackTrace();
Log.e(LOG_TAG, "Error processing Json Data");
}
公共类getData扩展异步任务{
字符串jsonStr=null;
ProgressDialog进度=新建ProgressDialog(MainActivity.this);
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection-urlConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(“http://host/data.json");
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.connect();
InputStream InputStream=urlConnection.getInputStream();
StringBuffer=新的StringBuffer();
如果(inputStream==null){
返回null;
}
reader=新的BufferedReader(新的InputStreamReader(inputStream));
弦线;
而((line=reader.readLine())!=null){
buffer.append(第+行“\n”);
}
if(buffer.length()==0){
返回null;
}
jsonStr=buffer.toString();
}捕获(IOE异常){
Log.e(“主要”、“错误”,e);
返回null;
}最后{
if(urlConnection!=null){
urlConnection.disconnect();
}
if(读卡器!=null){
试一试{
reader.close();
}捕获(最终IOE例外){
Log.e(“占位符片段”,“错误关闭流”,e);
}
}
}
试一试{
JSONArray itemsArray=新的JSONArray(jsonStr);
字符串itemID=null;
字符串itemType=null;
字符串itemDate=null;
字符串itemData=null;
对于(int i=0;i
最后是我的适配器:
class ListAdapter extends ArrayAdapter<Item> {
private List<Item> items;
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
TextView tn = null;
ImageView img = null;
if (convertView == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list, null);
}
else {
v=convertView;
}
Item p = items.get(position);
tn = (TextView) v.findViewById(R.id.tvText);
img = (ImageView) v.findViewById(R.id.thumbnail);
if (p.getmType().equals("image")) {
img.setVisibility(View.VISIBLE);
Picasso.with(getContext()).load(p.getmData()).error((R.drawable.placeholder_error)).placeholder(R.drawable.placeholder).resize(90,0).into(img);
tn.setText("ID: " + p.getmID()+"\nTYPE: " + p.getmType() +"\nDate: " + p.getmDate()+ "\nDATA: " + p.getmData());
} else {
img.setVisibility(View.INVISIBLE);
tn.setText("ID: " + p.getmID()+"\nTYPE: " + p.getmType() +"\nDate: " + p.getmDate()+ "\nDATA: " + p.getmData());
}
return v;
}
}
class ListAdapter扩展了ArrayAdapter{
私人清单项目;
公共ListAdapter(上下文上下文,int textViewResourceId){
super(上下文,textViewResourceId);
}
公共ListAdapter(上下文上下文、int资源、列表项){
超级(上下文、资源、项目);
这个项目=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=null;
TextView tn=null;
ImageView img=null;
if(convertView==null){
拉平机vi;
vi=LayoutInflater.from(getContext());
v=vi.充气(R.布局列表,空);
}
否则{
v=转换视图;
}
项目p=项目。获取(位置);
tn=(TextView)v.findviewbyd(R.id.tvText);
img=(ImageView)v.findViewById(R.id.缩略图);
if(p.getmType().equals(“image”)){
class ListAdapter extends ArrayAdapter<Item> {
private List<Item> items;
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
TextView tn = null;
ImageView img = null;
if (convertView == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list, null);
}
else {
v=convertView;
}
Item p = items.get(position);
tn = (TextView) v.findViewById(R.id.tvText);
img = (ImageView) v.findViewById(R.id.thumbnail);
if (p.getmType().equals("image")) {
img.setVisibility(View.VISIBLE);
Picasso.with(getContext()).load(p.getmData()).error((R.drawable.placeholder_error)).placeholder(R.drawable.placeholder).resize(90,0).into(img);
tn.setText("ID: " + p.getmID()+"\nTYPE: " + p.getmType() +"\nDate: " + p.getmDate()+ "\nDATA: " + p.getmData());
} else {
img.setVisibility(View.INVISIBLE);
tn.setText("ID: " + p.getmID()+"\nTYPE: " + p.getmType() +"\nDate: " + p.getmDate()+ "\nDATA: " + p.getmData());
}
return v;
}
}
public class ListAdapter extends BaseAdapter {
public enum ListMode {
IMAGES_AND_TEXT,
IMAGES_ONLY,
TEXT_ONLY
}
private ListMode mListMode = ListMode.IMAGES_AND_TEXT;
private List<Item> mItems;
private List<Item> mImages;
private List<Item> mTexts;
@Override
public int getCount() {
switch (mListMode) {
case IMAGES_AND_TEXT:
return mItems == null ? 0 : mItems.size();
case IMAGES_ONLY:
return mImages == null ? 0 : mImages.size();
case TEXT_ONLY:
return mTexts == null ? 0 : mTexts.size();
}
return 0;
}
@Override
public Object getItem(int position) {
switch (mListMode) {
case IMAGES_AND_TEXT:
return mItems == null ? null : mItems.get(position);
case IMAGES_ONLY:
return mImages == null ? null : mImages.get(position);
case TEXT_ONLY:
return mTexts == null ? null : mTexts.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position; // not really used
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
TextView tn = null;
ImageView img = null;
if (convertView == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list, null);
} else {
v=convertView;
}
Item p = items.get(position);
tn = (TextView) v.findViewById(R.id.tvText);
img = (ImageView) v.findViewById(R.id.thumbnail);
if (p.getmType().equals("image")) {
img.setVisibility(View.VISIBLE);
Picasso.with(getContext()).load(p.getmData()).error((R.drawable.placeholder_error)).placeholder(R.drawable.placeholder).resize(90,0).into(img);
tn.setText("ID: " + p.getmID()+"\nTYPE: " + p.getmType() +"\nDate: " + p.getmDate()+ "\nDATA: " + p.getmData());
} else {
img.setVisibility(View.INVISIBLE);
tn.setText("ID: " + p.getmID()+"\nTYPE: " + p.getmType() +"\nDate: " + p.getmDate()+ "\nDATA: " + p.getmData());
}
return v;
}
/**
* Call this when the filter is selected, e.g.
* <code>mAdapter.setListMode(ListMode.IMAGES_AND_TEXT);</code>
* @param listMode
*/
public void setListMode(ListMode listMode) {
mListMode = listMode;
notifyDataSetChanged();
}
/**
* After you receive the JSON, call this method to add the items to the adapter.
* @param items
*/
public void setItems(JSONArray jsonArray) {
mItems = new ArrayList<>();
mImages = new ArrayList<>();
mTexts = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
Item item = new Item(jsonArray.get(i));
mItems.add(item);
if (item.isImage()) {
mImages.add(item);
}
if (item.isText()) {
mTexts.add(item);
}
}
notifyDataSetChanged();
}
}
.
.
.
myListView = (ListView) findViewById(R.id.listViewID);
customAdapter = new ListAdapter();
myListView.setAdapter(customAdapter);
.
.
.
.
.
.
JSONArray itemsArray = new JSONArray(jsonStr);
customAdapter.setItems(itemsArray);
.
.
.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_about:
Intent intent = new Intent(this, About.class);
startActivity(intent);
return true;
case R.id.all:
item.setChecked(true);
customAdapter.setListMode(ListMode.IMAGES_AND_TEXT);
return true;
case R.id.images:
item.setChecked(true);
customAdapter.setListMode(ListMode.IMAGES_ONLY);
return true;
case R.id.text:
item.setChecked(true);
customAdapter.setListMode(ListMode.TEXT_ONLY);
return true;
}
return super.onOptionsItemSelected(item);
}
public Item(JSONObject jsonItem) {
String itemID=null;
String itemType=null;
String itemDate=null;
String itemData=null;
if (jsonItem.has("id")) {
itemID=jsonItem.getString("id");
}
if (jsonItem.has("type")) {
itemType=jsonItem.getString("type");
}
if (jsonItem.has("date")){
itemDate=jsonItem.getString("date");
}
if (jsonItem.has("data")){
itemData=jsonItem.getString("data");
}
this(itemID,itemType,itemDate,itemData);
}