在我的android项目中,我有一个ListView
,用于填充用户在运行时输入的数据。除此之外,我想从现有的JSON文件中显示一些数据。所以我解析了这个JSON文件并将其存储在String
中。如何在我的应用程序中显示这两个?我可以同时使用Listview
吗?
这是我的代码
// Reading text file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(getAssets().open(
"jsonshoutdata.txt")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
br.close(); // stop reading
}
catch (IOException e)
{
e.printStackTrace();
}
}
String myjsonstring = sb.toString();
// Try to parse JSON
try {
//urlist = new ArrayList<HashMap<String, String>>();
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("message");
ArrayList<String> messages = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
messages.add(message);
}
这是我的json数据的customList适配器
customtest adapter = new customtest(Single.this,R.layout.list_single_shout_single,messages);
ListView list = (ListView)findViewById(R.id.list_shout_screen);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Single.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
这是我已经拥有的customList适配器
final CustomListSingle adapter1 = new CustomListSingle(Single.this, web1,web,rounded,imageId2,imageId3,result1,result2);
//CustomListSingle adapter = new CustomListSingle(Single.this, web1,web,rounded,imageId2,imageId3,result1,result2);
list_shout_screen=(ListView)findViewById(R.id.list_shout_screen);
list_shout_screen.setAdapter(adapter1);
//list_shout_screen.setAdapter(adapter);
list_shout_screen.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
TextView txtTitle = (TextView)findViewById(R.id.txt);
txtTitle.length();
Toast.makeText(Single.this, "Length is " + txtTitle.getMeasuredWidth(), Toast.LENGTH_SHORT).show();
txtTitle.getTextSize();
}
}
);
//Button click activity......
final EditText et = (EditText)findViewById(R.id.EditText1);
final Button imb=(Button)findViewById(R.id.btn_send);
imb.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String str = et.getText().toString();
web1.add(str);
Toast.makeText(Single.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
adapter1.notifyDataSetChanged();
scrollMyListViewToBottom();
et.setText(" ");
}
这是我的适配器类
public CustomListSingle(Activity context,ArrayList<String> web,ArrayList<String> web1,Bitmap rounded,Integer[] imageId2,Integer[] imageId3,Bitmap image1,Bitmap image)
{
super(context, R.layout.list_single_shout_single, web);
this.context = context;
this.web = web;
this.web1 = web1;
//this.imageId1 = imageId1;
this.rounded=rounded;
this.imageId2 = imageId2;
this.imageId3 = imageId3;
this.image=image;
this.image1=image1;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single_shout_single, null, true);
/*msg*/
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
//pic
ImageView imageView1 = (ImageView) rowView.findViewById(R.id.img1);
ImageView imageView3 = (ImageView) rowView.findViewById(R.id.img);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
txtTitle.setLayoutParams(params);
txtTitle.setText(web.get(position));
txtTitle.setBackgroundResource(R.drawable.test);
imageView1.setImageBitmap(rounded);
imageView3.setImageBitmap(image1);
return rowView;
}
}
制作圆形图像的方法
public static Bitmap getCircularBitmapFrom(Bitmap bitmap)
{
if (bitmap == null) {
return null;
}
float radius = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap
.getHeight()) / 2f : ((float) bitmap.getWidth()) / 2f;
if (radius < 0) {
radius = 0;
}
Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
radius, paint);
return canvasBitmap;
}
这个
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.white);
Bitmap bmp1=BitmapFactory.decodeResource(getResources(), R.drawable.prof_pic_a);
Bitmap images = BitmapFactory.decodeResource(getResources
(),R.drawable.prof_pic_a);
Bitmap rounded;
rounded=getCircularBitmapFrom(images);
result1=getCircularBitmapFrom(bmp);
result2=getCircularBitmapFrom(bmp1);
答案 0 :(得分:1)
假设你有这个arraylist
ArrayList<String> messages = new ArrayList<String>(); //add this line on top so it will be accessed globally
和你的按钮代码
final Button imb=(Button)findViewById(R.id.btn_send);
imb.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String str = et.getText().toString();
messages.add(str);
Toast.makeText(Single.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
adapter1.notifyDataSetChanged();
scrollMyListViewToBottom();
et.setText(" ");
adapter1.notifyDataSetChanged(); //list is the name of your listview
}
每当您想要更新notifyDataSetChanged
时(在您的arraylist中添加数据后),请致电listview