点击片段列表视图上的textview
TextView On category_row.xml textViewCategoryName
onclick SetText这个textViewCategoryName ok!
CategoriesFragment.java
package com.example;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.adapter.CategoryArrayAdapter;
import com.example.model.CategoryDataModel;
import com.example.parser.JSONParser;
import com.example.utils.Keys;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class CategoriesFragment extends Fragment {
private ListView listView;
private ArrayList<CategoryDataModel> list;
private CategoryArrayAdapter adapter;
private TextView categoryCurrent;
public CategoriesFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_categories, null);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/**
* Array List for Binding Data from JSON to this List
*/
list = new ArrayList<>();
adapter = new CategoryArrayAdapter(getActivity(), list);
/**
* Getting List and Setting List Adapter
*/
listView = (ListView) getActivity().findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Snackbar.make(getActivity().findViewById(R.id.parentLayout), list.get(position).getCategoryName(), Snackbar.LENGTH_LONG).show();
}
});
/**
* Check internet connection
*/
//if (InternetConnection.checkConnection(getApplicationContext())) {
new GetDataCategories().execute();
//} else {
//Snackbar.make(getActivity().findViewById(R.id.parentLayout), "اتصال به اینترنت برقرار نیست", Snackbar.LENGTH_LONG).show();
//}
}
/**
* Creating Get Data Task for Getting Data From Web
*/
class GetDataCategories extends AsyncTask<Void, Void, Void> {
ContentLoadingProgressBar progressBar;
@Override
protected void onPreExecute() {
super.onPreExecute();
/**
* Progress Bar for User Interaction
*/
progressBar = (ContentLoadingProgressBar) getActivity().findViewById(R.id.progress);
progressBar.show();
}
@Nullable
@Override
protected Void doInBackground(Void... params) {
/**
* Getting JSON Object from Web Using okHttp
*/
JSONObject jsonObject = JSONParser.getDataFromWeb("http://example.com/api-service/v1/platform_categories/");
try {
/**
* Check Whether Its NULL???
*/
if (jsonObject != null) {
/**
* Check Length...
*/
if(jsonObject.length() > 0) {
/**
* Getting Array named "Categories" From MAIN Json Object
*/
JSONArray array = jsonObject.getJSONArray(Keys.KEY_CATEGORIES);
/**
* Check Length of Array...
*/
int lenArray = array.length();
if(lenArray > 0) {
for(int jIndex = 0; jIndex < lenArray; jIndex++) {
/**
* Creating Every time New Object
* and
* Adding into List
*/
CategoryDataModel model = new CategoryDataModel();
/**
* Getting Inner Object from contacts array...
* and
* From that We will get Name of that Contact
*
*/
JSONObject innerObject = array.getJSONObject(jIndex);
String category_name = innerObject.getString(Keys.KEY_CATEGORY_NAME);
String category_link = innerObject.getString(Keys.KEY_CATEGORY_LINK);
/**
* Getting Object from Object "other"
*/
//JSONObject otherObject = innerObject.getJSONObject(Keys.KEY_NAME);
//String other = otherObject.getString(Keys.KEY_NAME_SUB);
model.setCategoryName(category_name);
model.setCategoryLink(category_link);
/**
* Adding data in List...
*/
list.add(model);
}
}
}
} else {
}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
/**
* Progress Bar for User Interaction
*/
progressBar.hide();
/**
* Checking if List size if more than zero then
* Update ListView
*/
if(list.size() > 0) {
adapter.notifyDataSetChanged();
} else {
Snackbar.make(getActivity().findViewById(R.id.parentLayout), "مشکلی در اتصال به سرورهای سخنک رخ داده است!", Snackbar.LENGTH_LONG).show();
}
}
}
}
CategoryArrayAdapter.java
package com.example.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.R;
import com.example.model.CategoryDataModel;
import java.util.List;
public class CategoryArrayAdapter extends ArrayAdapter<CategoryDataModel> {
List<CategoryDataModel> modelList;
Context context;
private LayoutInflater mInflater;
// Constructors
public CategoryArrayAdapter(Context context, List<CategoryDataModel> objects) {
super(context, 0, objects);
this.context = context;
this.mInflater = LayoutInflater.from(context);
modelList = objects;
}
@Override
public CategoryDataModel getItem(int position) {
return modelList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.category_row, parent, false);
vh = ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
CategoryDataModel item = getItem(position);
vh.textViewCategoryName.setText(item.getCategoryName());
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final TextView textViewCategoryName;
private ViewHolder(RelativeLayout rootView, TextView textViewCategoryName) {
this.rootView = rootView;
this.textViewCategoryName = textViewCategoryName;
}
public static ViewHolder create(RelativeLayout rootView) {
TextView textViewCategoryName = (TextView) rootView.findViewById(R.id.textViewCategoryName);
return new ViewHolder(rootView, textViewCategoryName);
}
}
}
fragment_categories.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/parentLayout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="visible" />
<ListView app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
category_row.xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/imageViewProfilePhoto">
<TextView
android:id="@+id/textViewCategoryName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAppearance="?android:textAppearanceSmall"
android:textIsSelectable="true"
tools:text="Quote Content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
您必须在onClickListener
中为TextView
设置ViewHolder
。
private static class ViewHolder {
public final RelativeLayout rootView;
public final TextView textViewCategoryName;
private ViewHolder(RelativeLayout rootView, TextView textViewCategoryName) {
this.rootView = rootView;
this.textViewCategoryName = textViewCategoryName;
}
public static ViewHolder create(RelativeLayout rootView) {
TextView textViewCategoryName = (TextView) rootView.findViewById(R.id.textViewCategoryName);
textViewCategoryName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Do whatever you want
}
});
return new ViewHolder(rootView, textViewCategoryName);
}
}